【问题标题】:With statement to avoid running code on a blank sheet使用语句避免在空白纸上运行代码
【发布时间】:2014-12-04 09:16:51
【问题描述】:

下面是将动态数据范围从 Sheet1 复制到 Sheet2 的子程序(后者是受密码保护的工作表)。

除了 lRowSh2lColSh2 在 Sheet2 完全空白时会导致严重错误之外,它工作得很好。

我是否可以使用某种If 语句,以便在单元格为空白时跳过清除 Sheet2 的部分(注意:它们过去可能有值)?

为清楚起见,第 6 行是两个工作表中的标题行。

Sub CopyData()
Application.ScreenUpdating = False

Dim lRowSh1 As Long, lColSh1 As Long, lRowSh2 As Long, lColSh2 As Long
Dim Sheet1Data() As Variant

' Warning message before proceeding with data transfer to sample selection worksheet.
If MsgBox("Copy data to Sheet2? (this will overwrite existing data in Sheet2)", _
vbYesNo + vbCritical) = vbYes _
Then

    With Sheets("Sheet1")

        ' Determines last row and column of Sheet1 data range.
        lRowSh1 = .Cells.Find("*", .Cells(1, 1), , , xlByRows, xlPrevious).Row
        lColSh1 = .Cells.Find("*", .Cells(1, 1), , , xlByColumns, xlPrevious).Column
        ' Loads Sheet1 data range (row 6 to last row for all columns) into array Sheet1Data.
        Sheet1Data = .Range(.Cells(6, 1), .Cells(lRowSh1, lColSh1)).Value

    End With

    With Sheets("Sheet2")
        ' Lifts worksheet protection for execution of code
        .Unprotect Password:="admin"

        ' Removes any existing filters in Sheet2.
        If .AutoFilterMode = True Then .AutoFilter.ShowAllData

        ' Determines last row and column of any pre-existing data in Sheet2 and clears:
        lRowSh2 = .Cells.Find("*", .Cells(1, 1), , , xlByRows, xlPrevious).Row
        lColSh2 = .Cells.Find("*", .Cells(1, 1), , , xlByColumns, xlPrevious).Column
        .Range(.Cells(6, 1), .Cells(lRowSh2, lColSh2)).ClearContents

        ' Repopulates with the contents of array Sheet1Data:
        .Range(.Cells(6, 2), .Cells(lRowSh1, lColSh1 + 1)).Value = Sheet1Data

        ' Autofit repopulated columns:
        .Cells.EntireColumn.AutoFit

        ' Reapply AutoFilter to header (Row 6):
        .Cells(6, 1) = " "
        .Cells(6, 1).EntireRow.AutoFilter

        ' Reapply worksheet protection after execution of code:
        .Protect Password:="admin", userinterfaceonly:=True, AllowFiltering:=True
        .EnableSelection = xlNoRestrictions

    End With

End If

Application.ScreenUpdating = True

End Sub

【问题讨论】:

  • 要检查一个范围是否为空,你可以使用CountA,类似的答案在这里:stackoverflow.com/questions/10811121/…
  • 查找返回一个范围。如果 Find 失败,该 Range 的值将为 Nothing。没有任何东西没有属性或行或列,因此会出现错误。试试Dim Rng as RangeRng= ...Find...If Rng Is Nothing Then' Empty sheet...
  • 谢谢大家,工作正常!与往常一样,感谢托尼的“为什么”:) 如果您将回复粘贴为回复,我会接受。
  • 我已按照您的建议发布了答案。感谢您要求我这样做。顺便说一句,只有问题或答案的作者会被自动告知 cmets。对于其他所有人,如果您想告知 Name,则需要在评论中包含 @Name
  • 一直想知道该怎么做,非常感谢:)

标签: vba excel


【解决方案1】:

您的问题是,如果查找不成功,Find(What:="*").Row 将不存在。

无论查找是否成功,Find 总是返回一个 Range。

如果 Find 不成功,则 Range 的值将为 Nothing。没有任何东西没有属性,因此任何访问该范围属性的尝试都会失败。

你需要这样的东西:

Option Explicit
Sub Test()

  Dim RngCrnt As Range

  With Worksheets("Sheet2")

    Set RngCrnt = .Cells.Find(What:="*")

    If RngCrnt Is Nothing Then
      ' Code to handle empty worksheet
      Debug.Print "Worksheet empty"
    Else
      ' Code to handle non-empty worksheet
      Debug.Print "Cell(" & RngCrnt.Row & ", " & RngCrnt.Column & ") contains a value"
    End If

  End With

End Sub

【讨论】:

    【解决方案2】:

    如果 .usedrange.cells.count > 1 并且 .range("a1")="" 那么

    如果工作表为空白则为真

    【讨论】:

    • 我尝试过使用它,但从我读过的关于UsedRange 工作原理的内容来看,它将计算以前填充过的任何单元格(即以前可能包含值的空白单元格)所以在这种情况下不适合。如果我的理解有误请指正:)
    【解决方案3】:

    如果工作表包含数据,由于标题行总是有一个值,我决定只检查该行上的一个单元格:

    If .Cells(6.2) Is Nothing Then
    Else
        lRowSh2 = .Cells.Find("*", .Cells(1, 1), , , xlByRows, xlPrevious).Row
        lColSh2 = .Cells.Find("*", .Cells(1, 1), , , xlByColumns, xlPrevious).Column
        .Range(.Cells(6, 1), .Cells(lRowSh2, lColSh2)).ClearContents
    
    End If
    

    【讨论】:

      【解决方案4】:

      简单版怎么样:

      if isempty(usedrange)
      

      在这种情况下,即使工作表中有使用范围但完全空白,它也会将其视为空

      【讨论】:

        猜你喜欢
        • 2019-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-14
        • 2018-10-18
        • 2020-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多