【问题标题】:VBA - Unable to Set Worksheet VariableVBA - 无法设置工作表变量
【发布时间】:2021-02-14 00:08:27
【问题描述】:

我是 VBA 新手,但我对脚本语言并不陌生。由于某种原因,我在此例程中设置工作表变量时遇到问题。相同的语法适用于其他例程,但由于某种原因,它不会在这个例程中。

谁能解释为什么我的工作表变量“wks”会填充?我没有收到任何错误,但它不会填充。它仍然是空的。

问题在于“Set wks = .Sheets(strTemplate)”这一行。将鼠标悬停在变量“strTemplate”上时确实指示正确的模板表名称,但工作表变量“wks”从不填充。

这是创建模板表副本的子例程,然后将其重命名以填充“主”表中的数据。我什至输入了“调试”命令,但由于“wks”为空,打印“Sheet =”的命令从未执行。

' REPLACE MAIN WORKSHEET AFTER COPYING
'
Public Sub SheetReplace(strSheetName As String, strTemplate As String)
    Dim wks As Worksheet

Debug.Print "Entered SheetReplace"
    ' We don't want screen updating
    Application.ScreenUpdating = False

    ' Delete the existing Main - Copy if it exists
    Application.DisplayAlerts = False   'turn off alerts
    On Error Resume Next                'continue execution
    Worksheets("Main - Copy").Delete    'delete the worksheet
    Err.Clear                           'clear error
    Application.DisplayAlerts = True    'turn on alerts

    With ThisWorkbook
        ' Duplicate the Main - Tmplt sheet
        ' Duplicate the template sheet
        Set wks = .Sheets(strTemplate)
Debug.Print "Sheet = [" & wks & "]"

        ' Check sheet visibility
        isVisible = (wks.Visible = xlSheetVisible)
    
        ' Make the sheet visible if not
        If Not isVisible Then wks.Visible = xlSheetVisible
    
        ' Copy duplicate to the end of the sheets
        wks.Copy After:=.Sheets(strSheetName)
    
        ' Change the name of the sheet
        ActiveSheet.Name = strSheetName & " - Copy"
    
        ' Make the sheet invisible
        If isVisible Then ws.Visible = xlSheetHidden
    
        ' BEGIN COPYING MAIN SHEET INFO
        With Worksheets("Main")
            Set srcWorkSheet = Worksheets(ActiveSheet.Name)         ' Duplicate the Copy name
            lastRowMain = .Cells(.Rows.Count, "A").End(xlUp).Row    ' Find the last row used in "Main" sheet
            ' Copy the ranges that contain daily data.
            ' Copy the Month
            .Range("$C$8").Copy Destination:=Worksheets(ActiveSheet.Name).Range("$C$8")
            .Range("$I$11").Copy Destination:=Worksheets(ActiveSheet.Name).Range("$I$11")
            .Range("$B15:$I51").Copy Destination:=Worksheets(ActiveSheet.Name).Range("$B15:$I51")
            srcWorkSheet.Visible = xlSheetHidden                    ' Make the copy sheet invisible
    
            ' Clear cells (including formatting)
            .Range("$C15:$H51").ClearContents
        End With
        ' THIS IS THE END OF THE MAIN COPY
    End With
End Sub

任何信息将不胜感激。谢谢。

【问题讨论】:

  • 删除On Error Resume Next,然后你会看到没有名称为strTemplate的工作表。如果您想保留它,以规避可能出现的错误,如果“主要 - 副本”不存在插入 On Error Goto 0Worksheets("Main - Copy").Delete 之后,那么您也可以删除 Err.Clear
  • 我会尝试使用 wks.activate / wks.range("a1").select 进行调试,而不是尝试打印工作表
  • Worksheets("Main")Worksheets("Main - Copy") 在哪里?都在ThisWorkbook 中还是有另一个工作簿?
  • @VBasic2008 : 所有工作表都在同一个工作簿中。
  • @Nacorid :有趣的是,当我禁用“On Error Resume Next”行时,“Worksheets("Main - Copy").Delete" 之后不会执行任何操作。它到达那条线并立即结束。

标签: excel vba


【解决方案1】:

更新工作簿

  • 使用Option Explicit
  • 如果您在包含代码的工作簿中执行操作,请使用 ThisWorkbook
  • On Error Goto 0 关闭错误处理(这是致命错误)。

快速修复

Option Explicit

Public Sub SheetReplace(strSheetName As String, strTemplate As String)
    
    Dim wb As Workbook
    Set wb = ThisWorkbook
    
    ' We don't want screen updating
    Application.ScreenUpdating = False

    ' Delete the existing Main - Copy if it exists
    On Error Resume Next                'continue execution
    Application.DisplayAlerts = False   'turn off alerts
    wb.Worksheets("Main - Copy").Delete    'delete the worksheet
    Application.DisplayAlerts = True    'turn on alerts
    On Error GoTo 0                     'disable error handling

    ' Duplicate the Main - Tmplt sheet
    ' Duplicate the template sheet
    Dim wks As Worksheet
    Set wks = wb.Worksheets(strTemplate)

    ' Check sheet visibility
    Dim isVisible As Boolean
    isVisible = (wks.Visible = xlSheetVisible)

    ' Make the sheet visible if not
    If Not isVisible Then
        wks.Visible = xlSheetVisible
    End If
    
    ' Copy duplicate to the end of the sheets
    wks.Copy After:=wb.Worksheets(strSheetName)
    
    ' Change the name of the sheet
    Dim src As Worksheet
    Set src = wb.ActiveSheet
    src.Name = strSheetName & " - Copy"
    
    ' Make the sheet invisible
    If isVisible Then wks.Visible = xlSheetHidden
    
    ' BEGIN COPYING MAIN SHEET INFO
    Dim LastRowMain As Long
    With wb.Worksheets("Main")
        ' Find the last row used in "Main" sheet
        LastRowMain = .Cells(.Rows.Count, "A").End(xlUp).Row
        ' Copy the ranges that contain daily data.
        ' Copy the Month
        .Range("$C$8").Copy Destination:=src.Range("$C$8")
        .Range("$I$11").Copy Destination:=src.Range("$I$11")
        .Range("$B15:$I51").Copy Destination:=src.Range("$B15:$I51")
        
        ' If you only need values, this is more efficient (faster).
        'src.Range("$C$8").Value = .Range("$C$8").Value
        'src.Range("$I$11").Value = .Range("$I$11").Value
        'src.Range("$B15:$I51").Value = .Range("$B15:$I51").Value
        
        ' Make the copy sheet invisible
        src.Visible = xlSheetHidden
        ' Clear cells (including formatting)
        .Range("$C15:$H51").ClearContents
    End With
        
    ' THIS IS THE END OF THE MAIN COPY

End Sub

【讨论】:

    【解决方案2】:

    显然我有一个语法错误并且设置了“wks”变量,我只需要引用“wks.Name”属性。一旦我这样做了,执行就会继续。

    Set wks = .Sheets(strTemplate)
    Debug.Print "Sheet = [" & wks.Name & "]" <------ OUTPUT: Sheet = [Main - Copy]
    

    @VBasic2008:是的。直到现在我才看到您的帖子,但感谢您的回复。你们都帮了大忙。

    【讨论】:

      猜你喜欢
      • 2019-06-01
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      相关资源
      最近更新 更多