Find all and get offset value in new sheet(code error)

sarao18592

New Member
Joined
Mar 4, 2021
Messages
12
Office Version
  1. 2019
  2. 2016
Platform
  1. MacOS
Hi,
I've currently written the code below and I'm trying to get it to run through the different sheets in the workbook but it will loop only through the first one and keep giving me values from the first one(hence causing it to crash). Could someone try helping me with it?

VBA Code:
Sub FindAndExecute()

Dim Sh As Worksheet
Dim Loc As Range
Dim i,j As Integer

i = 5
For Each Sh In ThisWorkbook.Worksheets
    With Sh.UsedRange
    Set Loc = .Cells.Find(What:="Employee Code:-")
        If Not Loc Is Nothing Then
            Do Until Loc Is Nothing
                If Loc.Offset(0, 9).Value <> Loc.Offset(0, 23).Value Then
                Sheets("Result2").Cells(i, 1).Value = Loc.Offset(0, 9).Value
                Sheets("Result2").Cells(i, 2).Value = Loc.Offset(0, 23).Value
                j = 3
                Do
                Sheets("Result2").Cells(i, j).Value = Loc.Offset(3, j - 1).Value
                Sheets("Result2").Cells(i + 1, j).Value = Loc.Offset(4, j -
                1).Value
                j = j + 1
                Loop Until j > 35
                i = i + 3
                Else
                End If
            Set Loc = .FindNext(Loc)
            Loop
        End If
    End With
    Set Loc = Nothing
Next

End Sub
 

Attachments

  • Screenshot 2021-03-24 at 1.28.03 PM.png
    Screenshot 2021-03-24 at 1.28.03 PM.png
    198.1 KB · Views: 9
Last edited by a moderator:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Your code don't stop after find the last occurrence. FIND.NEXT start again the search from beginning and shall never be NOTHING.

Maybe you can use this:
VBA Code:
Sub FindAndExecute()
    Dim Sh As Worksheet
    Dim Loc As Range
    For Each Sh In ThisWorkbook.Worksheets
        With Sh.UsedRange
            Set Loc = .Cells.Find(What:="Employee Code:-")
            If Not Loc Is Nothing Then
                FirstCell = Loc.Address
                Do
.
.
.
                Set Loc = .FindNext(Loc)
                Loop While FirstCell <> Loc.Address
            End If
        End With
        Set Loc = Nothing
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,492
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top