Hi Everybody,
Thanks in advance for your help - I am very new to VBA and am struggling with what should be a very easy problem. I am very grateful for any advice and code you can provide. I am finding lots of script examples but when I try to mod them for my specific use case, they fail. After many many hours I am fried. I am using Win7/Excel2010.
Here is what I am trying to do:
Sheet1 contains hundreds of rows. ColumnA and ColumnE contain the string I am looking for. That string may exist in other columns as well, but I need to focus the search on just Columns A,E. The string may be by itself or within a much longer string.
If a row contains that string in Either Column A or E (an and/or operation) then I want to copy that row to the next blank row in Sheet2 once.
The icing on the cake would be to also add an additional column to sheet2 that contains the original row number from sheet1.
The most success I have had so far is with this code that prompts for the text, but I end up with more rows that I started with. When the string exists in both Column A and E, the row get's copied twice.
Would you please be so kind as to suggest how to limit the search range to just columns A,E, copy the row only once, and how I might add a column with the original row#?
Thank you very much.
3gswish
Sub Button98_Click() 'Search with highlight
Highlight "Report Sheet"
End Sub
Sub Highlight(sSheet As String)
Dim cl As Range, rng As Range
Dim sFind As String, FirstAddress As String
Dim sht As Worksheet
Set rng = ActiveSheet.UsedRange
Set sht = ActiveWorkbook.Sheets.Add(Before:=Worksheets(Worksheets.Count))
sht.Name = sSheet
sFind = Application.InputBox("Enter search string")
With rng
Set cl = .Find(sFind, LookIn:=xlValues)
If Not cl Is Nothing Then
FirstAddress = cl.Address
Do
'cl.EntireRow.Interior.ColorIndex = 48
If sht.Range("A65536").End(xlUp).Value = "" Then
cl.EntireRow.Copy sht.Range("A65536").End(xlUp)
Else
cl.EntireRow.Copy sht.Range("A65536").End(xlUp).Offset(1, 0)
End If
Set cl = .FindNext(cl)
Loop While Not cl Is Nothing And cl.Address <> FirstAddress
End If
End With
End Sub
Thanks in advance for your help - I am very new to VBA and am struggling with what should be a very easy problem. I am very grateful for any advice and code you can provide. I am finding lots of script examples but when I try to mod them for my specific use case, they fail. After many many hours I am fried. I am using Win7/Excel2010.
Here is what I am trying to do:
Sheet1 contains hundreds of rows. ColumnA and ColumnE contain the string I am looking for. That string may exist in other columns as well, but I need to focus the search on just Columns A,E. The string may be by itself or within a much longer string.
If a row contains that string in Either Column A or E (an and/or operation) then I want to copy that row to the next blank row in Sheet2 once.
The icing on the cake would be to also add an additional column to sheet2 that contains the original row number from sheet1.
The most success I have had so far is with this code that prompts for the text, but I end up with more rows that I started with. When the string exists in both Column A and E, the row get's copied twice.
Would you please be so kind as to suggest how to limit the search range to just columns A,E, copy the row only once, and how I might add a column with the original row#?
Thank you very much.
3gswish
Sub Button98_Click() 'Search with highlight
Highlight "Report Sheet"
End Sub
Sub Highlight(sSheet As String)
Dim cl As Range, rng As Range
Dim sFind As String, FirstAddress As String
Dim sht As Worksheet
Set rng = ActiveSheet.UsedRange
Set sht = ActiveWorkbook.Sheets.Add(Before:=Worksheets(Worksheets.Count))
sht.Name = sSheet
sFind = Application.InputBox("Enter search string")
With rng
Set cl = .Find(sFind, LookIn:=xlValues)
If Not cl Is Nothing Then
FirstAddress = cl.Address
Do
'cl.EntireRow.Interior.ColorIndex = 48
If sht.Range("A65536").End(xlUp).Value = "" Then
cl.EntireRow.Copy sht.Range("A65536").End(xlUp)
Else
cl.EntireRow.Copy sht.Range("A65536").End(xlUp).Offset(1, 0)
End If
Set cl = .FindNext(cl)
Loop While Not cl Is Nothing And cl.Address <> FirstAddress
End If
End With
End Sub