Hello,
I am writing a code to search a worksheet for a word and then select a certain amount of cells to the right of that word. I almost have it finished except the .offset command keeps giving me issues when I set it to do what I need.
Here is the code:
Sub SearchScanInfo()
Dim oRange As Range, aCell As Range, bCell As Range
Dim ws As Worksheet
Dim SearchString As String, FoundAt As String
On Error GoTo Whoa
Set ws = Worksheets("Master_List")
Set oRange = ws.Columns(1)
SearchString = Worksheets("Organize").Cells(7, 2)
Worksheets("Organize").Range("B15:R65536").ClearContents
Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
FoundAt = aCell.Address
Application.Goto bCell, True
Range(ActiveCell.End(xlToLeft), ActiveCell.Offset(0, 4)).Copy
Worksheets("Organize").Range("B15").PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Organize").Activate
Do
Set aCell = oRange.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
FoundAt = FoundAt & ", " & aCell.Address
Application.Goto aCell, True
Range(ActiveCell.End(xlToLeft), ActiveCell.Offset(0, 4)).Copy
Worksheets("Organize").Range("B15").End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Organize").Activate
Else
Exit Do
End If
Loop
Else
MsgBox "Scan #" & SearchString & " not Found"
Exit Sub
End If
''MsgBox "The Search String has been found these locations: " & FoundAt
Exit Sub
Whoa:
MsgBox Err.Description
End Sub
The issue comes up in this line:
Range(ActiveCell.End(xlToLeft), ActiveCell.Offset(0, 4)).Copy
When I run it Excel gives me the error "application-defined or object-defined error"
If I change the code though to whats below it will work. The only issue is that it copies the line I want and the one below it so this isn't something I can use.
Range(ActiveCell.End(xlToLeft), ActiveCell.Offset(1, 4)).Copy
I cant find anything to explain why 0 is causing it to give me that error. It just doesn't seem to like that I only want to accept one line of code. I am pretty new to this so is there something I am missing?
Any feedback would be great, thanks!
Josh
Bookmarks