繁体   English   中英

VBA - 无法设置工作表变量

[英]VBA - Unable to Set Worksheet Variable

我是 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

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

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

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

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

更新工作簿

  • 使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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