简体   繁体   中英

In VBA, referencing an Excel worksheet using a variable in VBA

I have a variable called 'attached' that increments each time a certain condition is verified as true. When that condition is true, I'm trying to select a worksheet that was created earlier that has the name "Attachment(x)". 'x' is a number 2,3,4, etc. For instance, a worksheet might have the name "Attachment (3)". In my code, x will be the value that the incrementing variable 'attached' has achieved at that point.

What code will select the worksheet based upon the current value of the variable 'attached'? I declared the variable attached as a variant......Dim attached As Variant.

I just keep getting errors.

Thanks

要选择工作表,请执行以下操作:

Worksheets("Attachment(" & x & ")").Select

If I understand correctly, your variable (named Attachment(#) ) will decide which sheet to use (sheet named Attachment (#) )?

I assume you're using some kind of loop that iterates through the #, so you could do something like this

For i = 1 to 100
  myAttachment = "Attachment(" & i & ")"
  myWorksheet = Worksheets("Attachment (" & i & ")")
next i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM