1
我有一個電子表格,其中包含的數據以列a-z表示。此腳本適用於Col數據,但我希望它可以搜索col C.如何在col c中搜索字符串?我沒有找到任何字符串或沒有插入行。根據可變文本字符串搜索列插入X行數C
Option Explicit
Sub Insert_Rows()
Dim i As Long, lRows As Long, lastrow As Long, lngCount As Long
Dim strTxt As String
Application.ScreenUpdating = False
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
lRows = Application.InputBox("How many rows do you want to insert?", Type:=1)
If lRows < 1 Then
MsgBox " You must enter a number greater than zero"
Exit Sub
End If
strTxt = Application.InputBox("Enter the text string to search on. Rows will be inserted below each cell containing this string.")
If Len(strTxt) < 1 Then
MsgBox "You must enter a text string consisting of at least one character"
Exit Sub
End If
With ActiveSheet
lngCount = Application.WorksheetFunction.CountIf(.Range("A1:A" & lastrow), strTxt)
If lngCount < 1 Then
MsgBox "The text string you entered is not listed - cancelling", vbExclamation
Exit Sub
End If
On Error Resume Next
For i = lastrow To 1 Step -1
If .Cells(i, 1).Value = strTxt Then
.Range("A" & i + 1 & ":A" & i + lRows).Insert shift:=xlDown
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
更改爲列A到C的引用? –
我試過了。我到處都看到了'A',但它不起作用。你能更具體地瞭解哪些實際變化? – DubMartian
除了所有'A'到'C',你還需要將'.Cells(i,1)'改爲'.Cells(i,3)' –