【问题标题】:Worksheets selection based on vba value OR how to convert a string into worksheets array基于 vba 值的工作表选择或如何将字符串转换为工作表数组
【发布时间】:2021-09-16 22:40:19
【问题描述】:

我正在尝试根据变量选择不同的工作表。 所有这些都是为了将​​这些表格打印成 pdf。

我尝试过使用数组,但我不太了解。 所以我继续使用字符串和拆分。 但我也没有机会。

除了最后一个代码行,工作表选择之外,一切都很好。 我收到一个下标超出范围 有什么建议吗?

Sub Print_Full_Report()

Call Clear_clipboard
Call Stop_Calcul_Screen

LastOverviewRow = WS_Rep_Overview.Cells(Rows.Count, 3).End(xlUp).Row
tabtoprint = ""
SelectedDate = Date + 90

For i = 5 To LastOverviewRow
   If WS_Rep_Overview.Range("E" & i) < SelectedDate Then
       Module = WS_Rep_Overview.Range("D" & i).Value
           tabtoprint = tabtoprint & Chr(34) & Module & Chr(34) & "," & Chr(34) & Module & "-Cow" & Chr(34) & "," & Chr(34) & Module & "-SubSys" & Chr(34) & ","
   End If
Next i

tabtoprint = Mid(tabtoprint, 1, Len(tabtoprint) - 1)
Sheets(Split(tabtoprint, ",")).Select

End Sub

【问题讨论】:

  • 试试:Sheets(Array(1, 3)).Select
  • 删除所有&amp; Chr(34),它应该可以工作。
  • @Pᴇʜ 非常感谢。我在几个小时前添加了它们,在我使用 split 命令之前。

标签: excel vba split selection


【解决方案1】:

试试这个代码:

Sub Print_Full_Report()
    Const template = ",#,#-Cow,#-SubSys"   ' for further use in the loop
    
    ' previous your code ...
    
    ' in the code some redundant variables have been removed
    With WS_Rep_Overview
        SelectedDate = Date + 90
        For i = 5 To .Cells(Rows.Count, 3).End(xlUp).Row
           If .Range("E" & i) < SelectedDate Then
               tabtoprint = tabtoprint & Replace(template, "#", .Range("D" & i).Value)
           End If
        Next i
    End With
    tabtoprint = Mid(tabtoprint, 2) 'remove the leading comma
    ' for debug; check that it prints something like this: Module5,Module5-Cow,Module5-SubSys,Module6,Module6-Cow,Module6-SubSys,Module7,Module7-Cow,Module7-SubSys
    Debug.Print tabtoprint
    Worksheets(Split(tabtoprint, ",")).Select
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    相关资源
    最近更新 更多