我的Excel 2013工作簿包含100多张工作表,我试图找到特定值并在包含该值的工作簿中选择多行,然后插入一行以便能够在工作表中输入数据作为基。
目前我正在处理关注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
感谢您帮助完成上述任务。
狮子座
答案 0 :(得分:0)
好的,你需要先检查String是否为空。
If len(addSelection) > 0 then
addSelection = left(addSelection, Len(addSelection) - 1)
shtthis.Range(addSelection).EntireRow.Select
End if
我也替换了你的mid()函数,因为我认为left()更容易,但你的mid函数应该也能正常工作。