Showing posts with label Excel Macro's and functions. Show all posts
Showing posts with label Excel Macro's and functions. Show all posts

Thursday, January 3, 2008

Combinations of all values in 3 columns

Here a macro which combines all values from 3 columns and makes unique combinations of it. I created this macro a while ago for someone on Experts-Exchange.com

Sub CombinedValues3Column4()
'Author: J. Rosink
'Date: 11-10-2007 'http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_22949908.html
Dim i, j, k, l, RowsA, RowsB, RowsC, Rw As Long
Dim MyCol1, MyCol2, MyCol3 As Collection
Dim strtime, endtime As Long

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
RowsA = Cells(Rows.Count, 1).End(xlUp).Row
RowsB = Cells(Rows.Count, 2).End(xlUp).Row
RowsC = Cells(Rows.Count, 3).End(xlUp).Row

Set MyCol1 = New Collection
Set MyCol2 = New Collection
Set MyCol3 = New Collection

For i = 1 To RowsA
MyCol1.Add Cells(i, 1)

Next For i = 1 To RowsB
MyCol2.Add Cells(i, 2)
Next

For i = 1 To RowsC
MyCol3.Add Cells(i, 3)
Next
Rw = 1
col = 4
With ActiveSheet

For Each j In MyCol1
For Each k In MyCol2
For Each l In MyCol3

If Rw = 65536 And col < 257 Then
Rw = 1
col = col + 1
If col = 257 Then
Exit Sub
Else
'Exit Sub
End If
.Cells(Rw, col) = j & k & l
Rw = Rw + 1

Next l
Next k
Next j
End With

Set MyCol1 = Nothing
Set MyCol2 = Nothing
Set MyCol3 = Nothing

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

To use this code:
1. Open Excel
2. Open VBA editor (ALT+F11)
3. Insert new module
4. Paste code.
5. Run Macro

Calculate speed of web site connection

This macro is a small example how to measure the time a webpage is loaded:

Sub CalculateSpeed()
Dim sch As Object
Dim xmlUrl As String
Dim fName As String
Dim ws1 As Worksheet
Dim counter As Long, k As Long
Set ws1 = ActiveSheet ' Result sheet ("sheet1")
counter = InputBox("How many times loading?")
k = 2
fName = "http://www.YOURWEBSITE.com" ' internet adres
Cells.ClearContents
ws1.[A1:C1] = Array("StartTime", "EndTime", "TotalTime")
ws1.[A1:C1].Font.Bold = True 'Exit Sub
For i = 1 To counter
Set sch = CreateObject("InternetExplorer.Application")
sch.Navigate fName

'Optional to make visible explorer
sch.Visible = True
strtime = Now()

'Wait until page is loaded
Do While sch.Busy
DoEvents
Loop
endTime = Now()
totTim = Left(Right(endTime, 5), 2) - Left(Right(strtime, 5), 2)
ws1.Cells(k, 1) = Format(strtime, "hh:mm.ss")
ws1.Cells(k, 2) = Format(endTime, "hh:mm.ss")
ws1.Cells(k, 3) = totTim
k = k + 1
sch.Quit
Next i
Set sch = Nothing
End Sub

To use this code:
1. Open Excel
2. Open VBA editor (ALT+F11)
3. Insert new module
4. Paste code.
5. Run Macro

nr of Test cases for a certain test depth level

Here a function in MS Excel. Though it is for no use at all, at least you don't need to pick up the book TMap and go to the page where a table is shown.

=IF(B1=0,0,IF(B2<=B1,2^B2,IF(B1=1,2^B1,CHOOSE(B1-1,2^B1+SIGN(B1-1.5)*(B2-3+B1)-2,(B1-1)*B2,(COMBIN(B2-1,B1-2))+(B1-2)*(B2),((COMBIN(B2-1,B1-2)+COMBIN(B2-1,(B1-1)-2)))+(B1-(B1-2))*(B2),((COMBIN(B2-1,5-2)+COMBIN(B2-1,(5-1)-2)))+(5-(5-2))*(B2)+COMBIN(B2-1,B1-2),((COMBIN(B2-1,5-2)+COMBIN(B2-1,(5-1)-2)))+(5-(5-2))*(B2)+COMBIN(B2-1,6-2)+COMBIN(B2-1,B1-2),((COMBIN(B2-1,5-2)+COMBIN(B2-1,(5-1)-2)))+(5-(5-2))*(B2)+COMBIN(B2-1,6-2)+COMBIN(B2-1,7-2)+COMBIN(B2-1,B1-2),2^B1))))

To use this function: Enter:
1. Enter in B1 the number of deteminants (conditions)
2. Enter in B2 the test depth level
3. the formula in cell B3