Excel 在所有工作表中搜索值
我在这里提供的代码能够搜索Sheet1,然后将已搜索的值(包含该值的整行)复制到新工作表中,然后在搜索字符串后重命名该工作表 但是现在我尝试在excel中搜索所有工作表,而不是一个工作表,这次我还需要包括相关行的标题 例如,如果我搜索Apple,宏将在所有工作表中搜索Apple,例如,如果在sheet7上找到Apple,则会将其复制到名为“Apple”的新工作表中,并带有相关标题 但是,例如,如果sheet 7和sheet 8上都有apple,则两者都将被复制到新的工作表名称“apple”中,但两个页眉也必须复制到新工作表中 我如何开始工作?我知道我必须找出张数并循环,但在那之后我应该包括什么Excel 在所有工作表中搜索值,excel,vba,search,excel-2010,Excel,Vba,Search,Excel 2010,我在这里提供的代码能够搜索Sheet1,然后将已搜索的值(包含该值的整行)复制到新工作表中,然后在搜索字符串后重命名该工作表 但是现在我尝试在excel中搜索所有工作表,而不是一个工作表,这次我还需要包括相关行的标题 例如,如果我搜索Apple,宏将在所有工作表中搜索Apple,例如,如果在sheet7上找到Apple,则会将其复制到名为“Apple”的新工作表中,并带有相关标题 但是,例如,如果sheet 7和sheet 8上都有apple,则两者都将被复制到新的工作表名称“apple”中,但
Dim strSearch
Dim rg As Range, rgF As Range
Dim i As Integer
Dim celltxt As String
Dim strSearch2
'Dim x, NumberOfWorksheet As Integer 'to count worksheet for loop
Application.ScreenUpdating = False
strSearch = Application.InputBox("Please enter the search string")
strSearch2 = Replace(strSearch, "*", " ")
' NumberOfWorksheet = ThisWorkbook.Sheets.Count
' For x = 0 To NumberOfWorksheet
If Len(strSearch) > 0 Then
Worksheets.Add().Name = strSearch2
Set rg = Sheets("Sheet1").Cells(1).CurrentRegion 'Define whole search range here
For i = 1 To rg.Rows.Count 'we look rows by rows (to copy row once only)
Set rgF = rg.Rows(i).Find(strSearch, , xlValues, xlWhole)
If Not rgF Is Nothing Then
rg.Rows(i).Copy Sheets(strSearch2).Range("A60000").End(xlUp).Offset(1, 0)
Set rgF = Nothing
End If
Next i
'Next x
Application.ScreenUpdating = True
End If
它已在Excel 2007上运行:
Sub sof20312498SearchCopy()
Dim i As Long, nRowsAddePerSheet As Long, nRows As Long, _
nRowsMax As Long, nSheets As Long
Dim strSearch, strSearch2
Dim rg As Range, rgF As Range
Dim wks
'
'Dim x, NumberOfWorksheet As Integer 'to count worksheet for loop
Dim x
'
strSearch = Application.InputBox("Please enter the search string")
strSearch2 = Replace(strSearch, "*", "")
If Len(strSearch2) <= 0 Then
MsgBox "Abandon: Search string must not be empty."
Exit Sub
End If
Application.ScreenUpdating = False
nSheets = Sheets.Count
nRowsMax = ActiveSheet.Rows.Count
For x = 1 To nSheets
'
' get the worksheet, if nonexistent, add it:
'
On Error Resume Next
Set wks = Worksheets(strSearch2)
If (Err) Then
Set wks = Worksheets.Add(After:=Sheets(Sheets.Count))
wks.Name = strSearch2
Err.Clear
End If
On Error GoTo 0
'
' Define whole search range here:
'
'Set rg = Sheets("Sheet1").Cells(1).CurrentRegion
'
Sheets(x).Activate
Set rg = ActiveSheet.Cells(1).CurrentRegion
'
' we look rows by rows (to copy row once only):
'
nRows = rg.Rows.Count
nRowsAddePerSheet = 0
For i = 1 To nRows
Set rgF = rg.Rows(i).Find(strSearch, , xlValues, xlWhole)
'
' if found, copy the source row as the last row of the destination Sheet:
'
If Not rgF Is Nothing Then
'
' copy header if required, Row(1) is assumed as header:
'
If (nRowsAddePerSheet <= 0) Then
If (i <> 1) Then
rg.Rows(1).Copy wks.Range("A" & nRowsMax).End(xlUp).Offset(1, 0)
End If
End If
'
rg.Rows(i).Copy wks.Range("A" & nRowsMax).End(xlUp).Offset(1, 0)
nRowsAddePerSheet = nRowsAddePerSheet + 1
End If
Next
Next
Set rgF = Nothing
Set rg = Nothing
Set wks = Nothing
Application.ScreenUpdating = True
End Sub
子sof20312498SearchCopy()
我一样长,我一样长,我一样长_
nRowsMax一样长,n Sheets一样长
Dim STREARCH,STREARCH 2
变暗rg为范围,rgF为范围
暗淡的作品
'
“Dim x,NumberOfWorksheet作为整数”来计算循环的工作表
暗x
'
strSearch=Application.InputBox(“请输入搜索字符串”)
strearch2=Replace(strearch,“*”,“)
如果Len(streArch2)已在Excel 2007上运行:
Sub sof20312498SearchCopy()
Dim i As Long, nRowsAddePerSheet As Long, nRows As Long, _
nRowsMax As Long, nSheets As Long
Dim strSearch, strSearch2
Dim rg As Range, rgF As Range
Dim wks
'
'Dim x, NumberOfWorksheet As Integer 'to count worksheet for loop
Dim x
'
strSearch = Application.InputBox("Please enter the search string")
strSearch2 = Replace(strSearch, "*", "")
If Len(strSearch2) <= 0 Then
MsgBox "Abandon: Search string must not be empty."
Exit Sub
End If
Application.ScreenUpdating = False
nSheets = Sheets.Count
nRowsMax = ActiveSheet.Rows.Count
For x = 1 To nSheets
'
' get the worksheet, if nonexistent, add it:
'
On Error Resume Next
Set wks = Worksheets(strSearch2)
If (Err) Then
Set wks = Worksheets.Add(After:=Sheets(Sheets.Count))
wks.Name = strSearch2
Err.Clear
End If
On Error GoTo 0
'
' Define whole search range here:
'
'Set rg = Sheets("Sheet1").Cells(1).CurrentRegion
'
Sheets(x).Activate
Set rg = ActiveSheet.Cells(1).CurrentRegion
'
' we look rows by rows (to copy row once only):
'
nRows = rg.Rows.Count
nRowsAddePerSheet = 0
For i = 1 To nRows
Set rgF = rg.Rows(i).Find(strSearch, , xlValues, xlWhole)
'
' if found, copy the source row as the last row of the destination Sheet:
'
If Not rgF Is Nothing Then
'
' copy header if required, Row(1) is assumed as header:
'
If (nRowsAddePerSheet <= 0) Then
If (i <> 1) Then
rg.Rows(1).Copy wks.Range("A" & nRowsMax).End(xlUp).Offset(1, 0)
End If
End If
'
rg.Rows(i).Copy wks.Range("A" & nRowsMax).End(xlUp).Offset(1, 0)
nRowsAddePerSheet = nRowsAddePerSheet + 1
End If
Next
Next
Set rgF = Nothing
Set rg = Nothing
Set wks = Nothing
Application.ScreenUpdating = True
End Sub
子sof20312498SearchCopy()
我一样长,我一样长,我一样长_
nRowsMax一样长,n Sheets一样长
Dim STREARCH,STREARCH 2
变暗rg为范围,rgF为范围
暗淡的作品
'
“Dim x,NumberOfWorksheet作为整数”来计算循环的工作表
暗x
'
strSearch=Application.InputBox(“请输入搜索字符串”)
strearch2=Replace(strearch,“*”,“)
如果Len(strearch2)