Find all loop in VBA

wibni

New Member
Joined
Jun 15, 2011
Messages
33
Hello,

I'm using Excel 2003 and have the table below.
I'm looking for a way to fill the columns in yellow (each month) with a macro.
The values for those cells come from the top of the table ( row 2 to 17).
What my current macro does is to go through each cell in column I starting at row 21. Then search for this value in range D2:Z5.
If the value exists, copy the site description in column B to column I.
I basically link every rig to a site.

My current macro only works for the 1st month, or the top rows.
I know I need to somehow do another loop to go through the months as well but I couldn't find a solution.
Would anyone here please be able to assist me?

My code so far works, but only for 1 month becasue I do not evaluate column A yet.

Code:
Sub Site_Lookup()
    
    Dim rngSites As Range, rngLookup As Range, rngMonths As Range, cell As Range, found As Range
    Dim LR As Long, i As Long
        
   
    LR = Range("I" & Rows.Count).End(xlUp).Row
    Const FR As Long = 21 '<-- First Row of actual data
           
    Set rngMonths = Range("A2:A17")
    Set rngSites = Range("D2:Z17")
    Set rngLookup = Range("I" & FR & ":I" & LR)
      
    For Each cell In rngLookup
        If cell.Value <> "" Then
            If IsNumeric(cell) Then
                Firstfound = ""
                Set found = rngSites.Find(cell.Value, , xlValues, xlWhole, xlByRows, xlNext, False)
                If Not found Is Nothing Then
                    ' Site found
                    cell.Offset(, 1).Value = Range("B" & found.Row).Value
                Else
                    ' No Match
                    cell.Offset(, 1).Value = "N\A"
                End If
            Else
                ' Text
                cell.Offset(, 1).Value = cell.Value
            End If
        End If
    Next cell  
End Sub

Excel Workbook
ABCDEFGHIJKLM
1MonthsSite DescSum RigsRigs*********
2AprilGGM6008028038055066073****
3AprilNMM7005011021024032042048***
4AprilMWA13008028038055066073005011042048
5AprilKNC13008028038055066073005011042048
6MayGGM6008028038055066073****
7MayNMM8005011021024032042048049**
8MayMWA14008028038055066073005011042048
9MayKNC14008028038055066073005011042048
10JuneGGM***********
11JuneNMM***********
12JuneMWA***********
13JuneKNC***********
14JulyGGM***********
15JulyNMM***********
16JulyMWA***********
17JulyKNC***********
18*************
19*********AprilMay
20AcctIDAcct DescHeaderCategorySubCategoryCountryAccSegmentManCoRigSiteSite JanOriginalSite FebOriginal
2110-4110-005Revenue - DrillingEBITDARevenueRevenue10411010005NMM-207,270NMM-142,615
2210-4110-008Revenue - DrillingEBITDARevenueRevenue10411010008GGM-165,284GGM-158,815
2310-4110-011Revenue - DrillingEBITDARevenueRevenue10411010011NMM-149,812NMM-205,000
2410-4110-021Revenue - DrillingEBITDARevenueRevenue10411010021*-45,375*-99,427
2510-4110-024Revenue - DrillingEBITDARevenueRevenue10411010024*-226,452*-234,746
2610-4110-028Revenue - DrillingEBITDARevenueRevenue10411010028*-154,882*-140,032
2710-4110-029Revenue - DrillingEBITDARevenueRevenue10411010029*-42,456*0
2810-4110-032Revenue - DrillingEBITDARevenueRevenue10411010032*-173,345*-205,194
2910-4110-038Revenue - DrillingEBITDARevenueRevenue10411010038*-30,278*-229,486
3010-4110-042Revenue - DrillingEBITDARevenueRevenue10411010042*-196,846*-184,471
Sheet1
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,

Some ideas (try it on a test-workbook)

Create a macro that calls Site_Lookup with an argument = column-number

Code:
Sub Call_Site_Lookup()
    Dim i As Long, MyColumn As Long
 
    MyColumn = 9 ' Column I
    Call Site_Lookup(MyColumn)
 
    For i = 3 To 17
        If Range("A" & i) <> Range("A" & i - 1) Then
            MyColumn = MyColumn + 2
            Call Site_Lookup(MyColumn)
        End If
    Next i
 
End Sub

(You can improve it making the lastrow (17) a variable, etc)

Small changes in Sub Site_Lookup

Code:
Sub Site_Lookup([B]AColumn as long[/B])
     ......
 
[B]  LR = Cells(Rows.Count, AColumn).End(xlUp).Row[/B]
 
[B]  Set rngLookup = Range(Cells(FR, AColumn), Cells(LR, AColumn))[/B]
[B] .....[/B]

HTH

M.
 
Upvote 0
Thanks Marcelo,
Thats an excellent idea.

I'll just try and find a way to make sure that when the macro is in column April it only searches in rows 2 to 5.
Or for May it only searches in rows 6 to 9 and so on.
 
Upvote 0
Thanks Marcelo,
Thats an excellent idea.

I'll just try and find a way to make sure that when the macro is in column April it only searches in rows 2 to 5.
Or for May it only searches in rows 6 to 9 and so on.

You are welcome and tks for the feedback :)

M.
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

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