Populating Cells using VBA
Here is a general purpose code to fill the sheet, you set the run time
parameters for each value range; target sheet name and the row and column on
that sheet you wish to populate.
Sub FillSheet()
Dim sRow As Long, sUnit As Long, sShelf As Long, sPosition As Long
Dim eRow As Long, eUnit As Long, eShelf As Long, ePosition As Long
Dim shRow As Long, shCol As Integer, xR As Long
Dim L1 As Long, L2 As Long, L3 As Long, L4 As Long
Dim shName As String
'---------------------------------------------
' Run Time PARAMETERS
'---------------------------------------------
' set start and end values for each level
sRow = 1: eRow = 5
sUnit = 1: eUnit = 5
sShelf = 1: eShelf = 5
sPosition = 1: ePosition = 5
' specify datahseet to act upon
shName = "Sheet1"
' set first row and column for datasheet
shRow = 3: shCol = 1
'---------------------------------------------
' MAIN CODE
'---------------------------------------------
' check combinations do not exceed range
If (eRow - sRow) * (eUnit - sUnit) * _
(eShelf - sShelf) * (ePosition - sPosition) _
Rows.Count Then
MsgBox "Range combinations exceed spreadsheet limit"
Exit Sub
End If
Application.ScreenUpdating = False
With Sheets(shName)
xR = shRow
For L1 = sRow To eRow
For L2 = sUnit To eUnit
For L3 = sShelf To eShelf
For L4 = sPosition To ePosition
.Cells(xR, shCol) = _
fnstr(L1) & "-" _
& fnstr(L2) & "-" _
& fnstr(L3) & "-" _
& fnstr(L4)
xR = xR + 1
Next
Next
Next
Next
End With
Application.ScreenUpdating = True
MsgBox CStr(xR - shRow) & " combinations created"
End Sub
Function fnstr(xV As Long) As String
' function to take value and convert to leading zero string
If xV 9 Then
fnstr = CStr(xV)
Else
fnstr = "0" & CStr(xV)
End If
End Function
--
Cheers
Nigel
"williamr" wrote in message
...
I have a small "Do While" VBA program that creates some numbers (4750).
The
problem is that I don't know how to populate the cells in the worksheet
using
the VBA code. My results look like the following:
Row 1 Unit 2 Shelf 4 Position 5
There are 4750 different combinations. I want to concatenate the number
so
they look like 01-02-04-05 in Col A starting in row 3 until the Macro is
done, can someone help?
Thanks in Advance.
|