簡體   English   中英

編譯錯誤:對象'_Global'的方法'Range'失敗-搜索復制粘貼宏Excel VBA

[英]Compile Error: Method 'Range' of object '_Global' failed - Search Copy Paste Macro Excel VBA

我正在嘗試在Excel VBA 2007中創建一個宏,該宏在選定的字段中進行搜索,如果它在行中的任意位置找到了某個字符串,它將復制該行並將其粘貼到另一張工作表中。

但是,我在下面指出的行的標題中遇到錯誤。 是什么原因造成的?

Sub SearchCopyPaste()
'
' SearchCopyPaste Macro
' Searches for a string. If it finds that string in the line of a document then it copies and pastes it into a new worksheet.
'
' Keyboard Shortcut: Ctrl+Shift+W
'

    Dim sourceSheet, destinationSheet As Worksheet
    Set sourceSheet = Worksheets(1)               'Define worksheets
    Set destinationSheet = Worksheets(2)

    Dim selectedRange As Range                    'Define source range
    Set selectedRange = Selection

    Dim numRows, numColumns As Integer                            'Determine how many rows and columns are to be searched
    numRows = Range(selectedRange).Rows.Count '<<<<<<<< Error
    numColumns = Range(selectedRange).Columns.Count

    destinationRowCount = 1                     'Counter to see how many lines have been copied already
                                                    'Used to not overwrite, can be modified to add header,etc

    Dim searchString As String                      'String that will be searched. Will eventually be inputted
    searchString = "bccs"                       'Will eventually be put into msgbox

    For rowNumber = 1 To numRows
        If InStr(1, selectedRange.Cells(i, numColumns), searchString) > 0 Then
            selectedRange.Cells(rowNumber, numColumns).Copy Destination:=destinationSheet.Range(Cells(destinationRowCount, numColumns))
            destinationRowCount = destinationRowCount + 1
        End If
    Next rowNumber

End Sub

嘗試:

numRows = selectedRange.Rows.Count '<<<<<<<< Error
numColumns = selectedRange.Columns.Count

可能還有其他錯誤,我尚未測試您的完整代碼,但這應該可以解決您遇到的直接錯誤。

一些技巧:

  1. 在子標題的頂部聲明所有變量
  2. 為每個變量添加新行,以使代碼更具可讀性
  3. 每當您使用變量存儲行號時,都將其聲明為Long
  4. 如果您知道要使用的范圍,請事先將其定義為代碼中的范圍

這段代碼應該做一些接近您想要的事情。 試試看,讓我知道。 如果您知道要在運行宏之前使用的范圍,而不是使用“選擇”,則建議為整個第一張工作表指定確切的范圍或“ Sheets(1).UsedRange”。

Sub SearchCopyPaste()
    Dim fnd As String
    Dim vCell As Range
    Dim rng As Range
    Dim totalCols As Integer
    Dim rowCounter As Long

    'Set this to a specific range if possible
    Set rng = Selection
    totalCols = rng.Columns.Count

    'Get the data to find from the user
    fnd = InputBox("Input data to find")

    'Loop through all cells in the selected range
    For Each vCell In rng
        'If the data is found copy the data and paste it to Sheet2, move down one row each time
        If InStr(vCell.Value, fnd) > 0 Then
            rowCounter = rowCounter + 1
            Range(Cells(vCell.row, 1), Cells(vCell.row, totalCols)).Copy Destination:=Sheets(2).Cells(rowCounter, 1)
        End If
    Next

    'Copy the column headers onto the second sheet
    Sheets(2).Rows(1).EntireRow.Insert
    rng.Range(Cells(1, 1), Cells(1, totalCols)).Copy Destination:=Sheets(2).Cells(1, 1)
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM