【问题标题】:How to make a dynamic With reference?如何做一个动态的参考?
【发布时间】:2020-04-18 02:49:48
【问题描述】:

如何根据工作表制作动态?

Dim lr2 As Long
Dim rng As Range

SelectedSystem = ThisWorkbook.Worksheets("Blabla").Range("AI2").Value

'I would like to change this to a version like the one below,
' where I reference the selected system rather than a specific sheet.
With Sheet2
    lr2 = Worksheets("Blabla" & SelectedSystem & " Blabla").Cells(.Rows.Count, 2).End(xlUp).Row
    Set rng = Worksheets("Blabla" & SelectedSystem & " Blabla").Range("H2:H" & lr2)
End With

【问题讨论】:

  • 创建一个工作表对象,所以dim w as excel.worksheet 然后set w=Worksheets("Blabla" & SelectedSystem & " Blabla") 然后使用with w
  • 只要使用With Worksheets("Blabla" & SelectedSystem & " Blabla")
  • 在 VBA 中,您需要声明所有变量 - 即使在您的工作簿中有一个名为“Sheet2”的工作表,但这并不意味着您可以直接将其用作 VBA 代码中的对象.正如伙计们^所说。声明 Sheet2 对象,然后在您的代码中使用。
  • @DannyPapadopulos 如果“Sheet2”是工作表的代号,他可以。

标签: excel vba with-statement


【解决方案1】:

创建一个worksheet 对象,

所以

dim w as excel.worksheet

然后

set w=Worksheets("Blabla" & SelectedSystem & " Blabla")

然后使用with w

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2011-10-25
    相关资源
    最近更新 更多