Thread: Macro basics
View Single Post
  #17   Report Post  
Posted to microsoft.public.excel.misc
Dallman Ross Dallman Ross is offline
external usenet poster
 
Posts: 390
Default Macro basics

Good stuff, Dave. That works out. (Had to figure out that the
values for iCtr in LastRows(iCtr) would be, in the case of my
meager two loops, 0 and 1, but that wasn't that hard to suss out.)

Speaking of figuring out, how to I tell VBA to print the
result to the screen so I can test things?

Also, what is the VBA equivalent for statements such
as "continue" or "break" in some other languages, e.g.,
for working with loops?

Dallman

========================
In , Dave Peterson
spake thusly:

Nope.

But you could define another variable.

Option Explicit
Sub RGUpdate()

Dim LastRows As Variant
Dim iCtr As Long
Dim wsNames As Variant
Dim csvRG As Worksheet
Dim csvUG As Worksheet

'stuff deleted

Set csvRG = Worksheets("2006 Realized - CSV Data")
Set csvUG = Worksheets("Current - CSV Data")

'stuff deleted

wsNames = Array(csvRG, csvUG)
ReDim LastRows(LBound(wsNames) To UBound(wsNames))

For iCtr = LBound(wsNames) To UBound(wsNames)
With wsNames(iCtr)

.Activate 'this turns out to be necessary
.Visible = True
.Unprotect
Range("A1").Select 'just a "focus" thing

'still wish I didn't have to hit Enter to accept this query:
'.QueryTables(1).Refresh BackgroundQuery:=False

.Protect DrawingObjects:=True, _
Contents:=True, Scenarios:=True

'code I want to add with dynamic var naming:

LastRows(iCtr) = .Cells(.Rows.Count, "A").End(xlUp).Row

.Visible = False

End With
Next iCtr

'stuff deleted

End Sub

Dallman Ross wrote:

In , Dave Peterson
spake thusly:

It sounds like you're in business.
Good luck with the tweaks.


I've been tweaking away. Uncovered and fixed a couple of bugs.
Now I'm trying to do more with the loop that uses an array of
worksheet names.

My main question for this part of the thread is: can I, in
VBA, set variable using dynamic names?

To explain, I'll show where I'm at now, with stuff before and after
left off:

-----------------
Sub RGUpdate()

'stuff deleted

Set csvRG = Worksheets("2006 Realized - CSV Data")
Set csvUG = Worksheets("Current - CSV Data")

'stuff deleted

Dim iCtr As Long
Dim wsNames As Variant

wsNames = Array(csvRG, csvUG)
For iCtr = LBound(wsNames) To UBound(wsNames)
With wsNames(iCtr)

.Activate 'this turns out to be necessary
.Visible = True
.Unprotect
Range("A1").Select 'just a "focus" thing

'still wish I didn't have to hit Enter to accept this query:
.QueryTables(1).Refresh BackgroundQuery:=False

.Protect DrawingObjects:=True, _
Contents:=True, Scenarios:=True

'code I want to add with dynamic var naming:
'== csvRGLastR = .Cells(.Rows.Count, "A").End(xlUp).Row
'^^^^^ {OR}
'== csvUGLastR = .Cells(.Rows.Count, "A").End(xlUp).Row
'^^^^^ Those are strings, but based on the nicknames for the sheets!

.Visible = False

End With
Next iCtr

'stuff deleted

End Sub
-----------------

Okay, is anything like that possible?

Thanks,
-dman-