excel - Find() 返回 "object variable or with block variable not set"

标签 excel vba find

这段代码运行良好,但我删除了 Find() 上方的一些行,这些行破坏了它。有任何想法吗?

Sub CopySheet()
      Dim TotalRow As Integer
      
      Set NurselineBook = ThisWorkbook
      TotalRow = Range("$C:$C").Find(What:="Grand Total", LookIn:=xlValues, LookAt:=xlWhole).Row
      Range("A1:L" & TotalRow).Select
      Range("Ah1").Activate
      Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
      
      MsgBox "Dashboard Copied"
End Sub

最佳答案

Sub CopyTable()
      
    Const wsName As String = "Sheet1"
    Const ColumnsAddress As String = "A:L"
    Const FirstRow As Long = 1
    Const CriteriaColumn As Long = 3
    Const gtString As String = "Grand Total"
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
    
    Dim srg As Range
    With ws.Columns(ColumnsAddress)
        Set srg = .Resize(ws.Rows.Count - FirstRow + 1).Offset(FirstRow - 1)
    End With
    
    Dim gtcell As Range: Set gtcell = srg.Columns(CriteriaColumn) _
        .Find(gtString, , xlValues, xlWhole, , xlPrevious)
    If gtcell Is Nothing Then
        MsgBox "Could not find '" & gtString & "'.", vbCritical
        Exit Sub
    End If
    
    Dim drg As Range
    Set drg = srg.Resize(gtcell.Row - FirstRow + 1)
    
    drg.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
    
    MsgBox "Dashboard Copied", vbInformation

End Sub

关于excel - Find() 返回 "object variable or with block variable not set",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72236914/

相关文章:

vba - 从串口读取到 Excel

c# - openxml spreadsheat 另存为

string - 将字符串的一部分提取到bash中的变量

python - 如何检查字典中是否存在值?

excel - Excel 中多少列(安全)算太多?

arrays - 计算两个或多个空格后字符的位置

vba - Excel ADODB VBA 错误消息 'Not a Valid Password'

Excel VBA 值为 0 时清除格式

vba - MS Excel 2010 - 当放置在其他 VBA 中时,使用 Abs() 的计算停止工作

matlab - 在另一个向量 matlab 中查找向量的一部分