我目前有一个正常运行的代码可以完成我想要它做的事情,循环浏览excel文档的特定部分,查找某些关键字,然后将这些关键字粘贴到excel电子表格中的单独表格中。它只是很长,并且不允许重复超过10次。我想知道是否有人建议这个代码循环,直到用户选择vbNo,当被问及是否有更多的关键字?
Option Compare Text
Sub Macro2()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+h
'
Application.ScreenUpdating = False
Dim lastLine As Long
Dim findWhat As String
Dim toCopy As Boolean
Dim cell As Range
Dim i As Long
Dim j As Long
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
s = 2
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
If MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion) = vbNo Then Exit Sub
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Rows(i).Copy Destination:=Sheets(s).Rows(j)
j = j + 1
End If
toCopy = False
Next
s = s + 1
答案 0 :(得分:0)
你想要做的是做一个循环,直到用户选择在你的输入框中点击取消来停止。要启动循环,请设置循环检查变量,&#34;继续&#34;等于点击“是”&#39;的结果。在询问用户是否想继续的方框上。
然后,在执行代码后,询问用户是否要添加其他单词。如果没有,循环将结束。如果是这样,循环将继续并再做一个字。
这是一个开始......
Dim Continue As Long
Dim findWhat As String
Dim LastLine As Long
Dim toCopy As Boolean
Dim cell As Range
Dim i As Long
Dim j As Long
Dim newsheet As Worksheet
s = 2
Continue = vbYes 'initialize loop variable
Do While Continue = vbYes 'keep getting more use input until they state they do not want to continue
findWhat = CStr(InputBox("What word would you like to search for today?"))
lastLine = ActiveSheet.UsedRange.Rows.Count
If findWhat = "" Then Exit Sub
j = 1
For i = 1 To lastLine
For Each cell In Range("BU1").Offset(i - 1, 0)
If InStr(cell.Text, findWhat) <> 0 Then
toCopy = True
End If
Next
If toCopy = True Then
Set newsheet = activeworkbook.sheets.add
newsheet.name = findWhat
Rows(i).Copy Destination:=newsheet.Rows(j)
j = j + 1
End If
Set newsheet = Nothing
toCopy = False
Next i
s = s + 1
'find out if user wishes to continue.
Continue = MsgBox(((j - 1) & " results were copied, do you have more keywords to enter?"), vbYesNo + vbQuestion)
Loop