使用语句避免在空白页上运行代码

时间:2014-12-04 09:16:52

标签: vba excel-vba excel

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

除了lRowSh2以及lColSh2导致严重错误,如果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

4 个答案:

答案 0 :(得分:2)

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

Find始终返回Range,无论Find是否成功。

如果查找不成功,则该范围的值将为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

答案 1 :(得分:0)

如果.usedrange.cells.count> 1和.range(" a1")=""然后

如果工作表为空白,

将为真

答案 2 :(得分:0)

如果工作表包含数据,标题行将始终具有值,我决定只检查该行上的单元格:

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

答案 3 :(得分:0)

简单版本如何:

if isempty(usedrange)

在这种情况下,即使纸张中使用了范围,但它完全是空白,也会将其视为空