首页 > 解决方案 > 为工作簿中的多个工作表设置动态范围,不包括 2 个工作表。(错误 438)VBA

问题描述

我在执行此代码时收到错误消息 438。

我的工作簿中有 16 个工作表,我需要从中运行 14 个工作表的动态范围代码。

  1. 在每张纸中查找从单元格 A2 开始的范围(两张纸除外)。
  2. 并清除此选定范围的内容。*(我不想使用 .usedrange 函数)代码的目标是:

`子动态范围()

Dim ws4 As Worksheet
Dim twb As ThisWorkbook
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Dim getLastCell As Range
Dim shtrng As Range

'Answer = MsgBox("The file may contain data, which will be lost if you proceed" & vbCrLf & "Save the
'file with a different file name to retain data" & vbCrLf & " Or Click Yes to proceed ", vbQuestion +
'vbYesNo + vbDefaultButton2, "Caution")
'If Answer = vbYes Then
    Set twb = Application.ThisWorkbook
        For Each ws4 In twb.Worksheets
            Set ws4 = ActiveSheet
            If ws4.Name <> "Generate Pending Log Report" And ws4.Name <> "PL1.Summary" Then
                ws4.Select
                LastRow = ws4.Cells.Find("*", ws4.Cells(1, 1), xlFormulas, xlPart, xlByRows, _
                        xlPrevious).Row
                lastCol = ws4.Cells.Find("*", ws4.Cells(1, 1), xlFormulas, xlPart, xlByColumns, _
                        xlPrevious).Column
              Set getLastCell = ws4.Cells(LastRow, lastCol)
              Set shtrng = ActiveSheets.Range("A1", getLastCell(ws4)).Select
              'I am getting an error 438
            End If
        Next ws4

'End If

结束子`

标签: excelvba

解决方案


改变这个:

Set shtrng = ActiveSheets.Range("A1", getLastCell(ws4)).Select

对此:

ActiveSheet.Range("A1", getLastCell).Select

推荐阅读