quick question VBA and arrays

mountainman88

Board Regular
Joined
Jun 22, 2019
Messages
109
Office Version
  1. 2016
Platform
  1. Windows
I want to quickly put a range of cells containing text into an array called blockstrings. Why does the following not work?:

blockstrings = Worksheets("VBAinputs").Range("a5:a" & blocknameslastrow)

instead i have to use the following to make it work:

Worksheets("VBAinputs").Range("a1").Select
blockstrings = Range("a5:a" & blocknameslastrow)


thanks
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi & welcome to MrExcel.
There is no reason why your first code shouldn't work. Have you declared "blockstrings" & if so what is it declared as?
Also what is "blocknameslastrow"
 
Upvote 0
Blockstrings is declared as a variant
blocknameslastrow is a variable, it works fine in second case so shouldnt be an issue

another question.

i have a variables firstrow and lastrow

what is the VBA syntax to select a range of entire rows from firstrow to lastrow##

thanks
 
Upvote 0
Sub pasta()


Dim startrow As Integer
Dim firstrow As Integer
Dim blocksize As Integer
Dim blockstrings() As Variant
Dim blocknameslastrow As Integer
Dim i As Single


firstrow = 5
startrow = Worksheets("VBAinputs").Range("$D$3").Value
blocksize = Worksheets("VBAinputs").Range("$D$4").Value


blocknameslastrow = Worksheets("VBAinputs").Range("a1048576").End(xlUp).Row


blockstrings = Worksheets("VBAinputs").Range("a5:a" & blocknameslastrow) ****BREAKS HERE****




For i = 1 To UBound(blockstrings)


Worksheets("SVpulls").Range("a" & startrow, "XYZ" & (startrow + blocksize - 1)).Copy Destination:=Worksheets("SVpulls").Range("a" & startrow + (blocksize * i))
Worksheets("SVpulls").Range("a" & startrow + (blocksize * i)).Value = blockstrings(i, 1)


Next i




End Sub
 
Upvote 0
Blockstrings is declared as a variant
blocknameslastrow is a variable, it works fine in second case so shouldnt be an issue
As Fluff pointed out, there is nothing about your first posted code that should have prevented it from working.



i have a variables firstrow and lastrow

what is the VBA syntax to select a range of entire rows from firstrow to lastrow##

thanks
Rows(firstrow & ":" & lastrow).Select

I would point out, though, that it is rarely necessary to select any cells when writing VB code.
 
Upvote 0
this is the code working fine:

Sub pasta()


Dim startrow As Integer
Dim firstrow As Integer
Dim blocksize As Integer
Dim blockstrings() As Variant
Dim blocknameslastrow As Integer
Dim i As Single


firstrow = 5
startrow = Worksheets("VBAinputs").Range("$D$3").Value
blocksize = Worksheets("VBAinputs").Range("$D$4").Value


blocknameslastrow = Worksheets("VBAinputs").Range("a1048576").End(xlUp).Row


Worksheets("VBAinputs").Range("a1").Select
blockstrings = Range("a5:a" & blocknameslastrow)


For i = 1 To UBound(blockstrings)


Worksheets("SVpulls").Range(startrow & ":" & (startrow + blocksize - 1)).Copy Destination:=Worksheets("SVpulls").Range("a" & startrow + (blocksize * i))
Worksheets("SVpulls").Range("a" & startrow + (blocksize * i)).Value = blockstrings(i, 1)


Next i




End Sub
 
Upvote 0
You need to remove the () from this line
Code:
Dim blockstrings() As Variant
 
Upvote 0
perfect! so should i only use () when I know the array size? what exactly was the issue?
 
Upvote 0
I never use the () when assigning a range to an array.
Why it worked on the active sheet, but not on another sheet I don't know. It's not something I've come across before.
 
Upvote 0
more array questions!

setup = Worksheets("Lead").Range("b1:" & Cells(2, setlastcol))

this code isnt working to create an array range. In the example if setlastcol = 12 then it should be adding the range "b1:L2" to the setup array. my syntax is screwy im sure.

thanks!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top