在工作簿中查找并select多行

Excel 2013工作簿包含100多张工作簿,我试图find特定的值,并在包含该值的工作簿中select多行,然后插入一行以便能够跨工作表将数据作为一组input。

目前我正在处理下面的VBA代码,但我被困在错误“运行时错误”5:无效的过程调用或参数“对此行:addSelection = Mid(addSelection,1,Len(addSelection) – 1) 。

Public Sub SelectMultiple() Dim wbkthis As Workbook Dim shtthis As Worksheet Dim rngThis As Range Dim rngFind As Range Dim firstAddress As String Dim addSelection As String Set wbkthis = ThisWorkbook Set shtthis = wbkthis.Worksheets("Sheet1") // Set our range to search Set rngThis = shtthis.Range("B2", "B10") // Loop through it With rngThis // Find our required text Set rngFind = .Find("Jack") // If we find it then... If Not rngFind Is Nothing Then firstAddress = rngFind.Address // Take a note of where we first found it addSelection = addSelection & rngFind.Address & "," // Add the cell's range to our selection // Loop through the rest of our range and find any other instances. Do Set rngFind = .FindNext(rngFind) addSelection = addSelection & rngFind.Address & "," Loop While Not rngFind Is Nothing And rngFind.Address <> firstAddress End If End With // Trim the last comma from our string addSelection = Mid(addSelection, 1, Len(addSelection) - 1) shtthis.Range(addSelection).Rows.Select // Select our rows! Set rngThis = Nothing Set shtthis = Nothing Set wbkthis = Nothing End Sub 

感谢您的帮助,完成上述任务。

狮子座

好的,你需要首先检查string是否为空。

 If len(addSelection) > 0 then addSelection = left(addSelection, Len(addSelection) - 1) shtthis.Range(addSelection).EntireRow.Select End if 

我也replace了你的mid()函数,因为我认为left()更容易,但是你的中间函数也应该工作。