Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'd like to get this code to enter "2,4,7" in A1:C10.
A requirement is that their be only a single assignment into the worksheet. DON{T POST A SOLUTION WHERE "2,4,7" IS ENTERED INTO THE WORKSHEET ON EACH ITERATION. If this example can be made to work the next step would be to time and compare against similar code that assigns a row at a time and then a cell at a time. Thanks. Sub N_By_M() Dim x() As Variant, i As Long, startTime As Single, endTime As Single Dim b As Workbook Dim s As Worksheet Dim r As Range ReDim x(10) Const maxLoop As Long = 10 startTime = Timer For i = 1 To maxLoop ReDim Preserve x(1 To i) x(i) = Array(2, 4, 7) Next i Set b = ActiveWorkbook Set s = b.Sheets("Sheet1") Set r = s.Range("A1:C10") r.Value = Application.Transpose(x) endTime = Timer MsgBox endTime - startTime End Sub |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You need to either assign the LBound and UBound of you arrays or be more
consistent in your apparoach. Maybe you have Option Base 1 set, but I do not, hence: ReDim x(10) '0 To 9 so you cannot then change to ReDim Preserve x(1 To i) Also, you have already dimmed x, so there is no reason to redim the loop. So x is an array of variants, each element containing a Variant array. Note an "array of variants" is not the same thing as "a Variant array". You do not have a 2-D array. Try the line of code below: ?ubound(x,2) However, can you not achieve the same with one line ? Range("A1:C10").Value = Array(2, 4, 7) NickHK wrote in message oups.com... I'd like to get this code to enter "2,4,7" in A1:C10. A requirement is that their be only a single assignment into the worksheet. DON{T POST A SOLUTION WHERE "2,4,7" IS ENTERED INTO THE WORKSHEET ON EACH ITERATION. If this example can be made to work the next step would be to time and compare against similar code that assigns a row at a time and then a cell at a time. Thanks. Sub N_By_M() Dim x() As Variant, i As Long, startTime As Single, endTime As Single Dim b As Workbook Dim s As Worksheet Dim r As Range ReDim x(10) Const maxLoop As Long = 10 startTime = Timer For i = 1 To maxLoop ReDim Preserve x(1 To i) x(i) = Array(2, 4, 7) Next i Set b = ActiveWorkbook Set s = b.Sheets("Sheet1") Set r = s.Range("A1:C10") r.Value = Application.Transpose(x) endTime = Timer MsgBox endTime - startTime End Sub |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
NickHK writes:
Also, you have already dimmed x, so there is no reason to redim the loop. I'm rediming because I want to modify the example to dynamically build the array and x will not be dimmed. So I'll take out the dim. So x is an array of variants, each element containing a Variant array. Note an "array of variants" is not the same thing as "a Variant array". You do not have a 2-D array. Try the line of code below: ?ubound(x,2) However, can you not achieve the same with one line ? Range("A1:C10").Value = Array(2, 4, 7) Well you could but then you'd destroy the significance of the example. The point is to get N by M assignment into a worksheet going. The next step after getting the syntax correct is to swap out the hardcoded values of 2,4,7 with values read from a file or fetched from a SQL resultset. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You mean like this:
Dim arr() As Variant Dim i As Long For i = 1 To 5 ReDim Preserve arr(1 To i) arr(i) = Array(1, 2, 3) Next Range("A1:C5").Value = Application.Transpose(Application.Transpose(arr)) End Sub NickHK wrote in message oups.com... NickHK writes: Also, you have already dimmed x, so there is no reason to redim the loop. I'm rediming because I want to modify the example to dynamically build the array and x will not be dimmed. So I'll take out the dim. So x is an array of variants, each element containing a Variant array. Note an "array of variants" is not the same thing as "a Variant array". You do not have a 2-D array. Try the line of code below: ?ubound(x,2) However, can you not achieve the same with one line ? Range("A1:C10").Value = Array(2, 4, 7) Well you could but then you'd destroy the significance of the example. The point is to get N by M assignment into a worksheet going. The next step after getting the syntax correct is to swap out the hardcoded values of 2,4,7 with values read from a file or fetched from a SQL resultset. |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Absolutely perfect NickHK!
Thanks! |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
It was Tom who pointed out that Transpose flattens an array, so it was just
applying that really. NickHK wrote in message ups.com... Absolutely perfect NickHK! Thanks! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Number Assignment | Excel Worksheet Functions | |||
point assignment | Excel Discussion (Misc queries) | |||
Key Assignment Log | Excel Discussion (Misc queries) | |||
Assignment problem | Excel Programming | |||
Worksheet object assignment | Excel Programming |