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
 
Try this.
Code:
With Worksheets("Lead")
    setup = Worksheets("Lead").Range("b1", .Cells(2, setlastcol))
End With

PS That's not really an array question, more to do with how you are referencing things, i.e. ranges.
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If setup(1, i) = "Total" Then Range("TotalTemp").Copy Worksheets("Lead2").Column(i + 1) Else Range("DataTemp").Copy Worksheets("Lead2").Column(i + 1)

also this code isnt working, to copy named ranges TotalTemp or DataTemp to coumns based on the array contents.

thanks!
 
Upvote 0
Try this.
Code:
With Worksheets("Lead")
    setup = Worksheets("Lead").Range("b1", .Cells(2, setlastcol))
End With

PS That's not really an array question, more to do with how you are referencing things, i.e. ranges.

hey thanks, do i really have to use a 'with' statement? there must be a more efficient way?
 
Upvote 0
What makes you think that With Statements are inefficient?
This would be even better
Code:
With Worksheets("Lead")
    setup = .Range("b1", .Cells(2, setlastcol))
End With
Your post#12 is a totally different question, so please start a new thread.
 
Upvote 0
Here's the same code without the With.
Code:
setup = Worksheets("Lead").Range("b1", Worksheets("Lead").Cells(2, setlastcol))

Not sure if it's any more efficient but it is longer.:)
 
Upvote 0
Here's the same code without the With.
Code:
setup = Worksheets("Lead").Range("b1", Worksheets("Lead").Cells(2, setlastcol))

Not sure if it's any more efficient but it is longer.:)

Hey thanks, I’m interested to know why VBA needs to qualify the .cells when it already has worksheets(“lead2”)?
 
Upvote 0
What makes you think that With Statements are inefficient?
This would be even better
Code:
With Worksheets("Lead")
    setup = .Range("b1", .Cells(2, setlastcol))
End With
Your post#12 is a totally different question, so please start a new thread.

It seems like an extra step just to include a variable in a column reference, I assumed there would be a syntax that could be used on the same line?

will start a new post for #12

Thanks!
 
Upvote 0
Any range that is not fully qualified will act on the Active sheet, so with this
Code:
setup = Worksheets("Lead").Range("b1:" & [COLOR=#0000ff]Cells(2, setlastcol)[/COLOR])
It's looking at the "Lead" sheet range B1, but the part in blue is still looking at the active sheet because it hasn't been qualified with the sheet reference.
 
Upvote 0
It seems like an extra step just to include a variable in a column reference,
When you have two or more references to the same object, the With statement block is more efficient as VB only has to evaluate the object once and then automatically apply it to all the properties or methods that reference. Otherwise, if you repeat the object at each property or method that references it, you force VB to evaluate the object each time. Also, when you have multiple references to the same object, the With statement block cuts down of typing considerably which, once you get used to reading a With block, makes reading the code sometime in the future when you need to modify it much, much easier.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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