Loop Help

wisewood

Board Regular
Joined
Nov 7, 2002
Messages
193
Could someone please help me with the loop aspect of this code; I've got this far, but it keeps listing everything, and i only want the ones where the If clause applies.

The loop lists a number of 4 digit reference numbers. I only want it to list ones where the reference number (Gx) is greater than the last number displayed on Sheet2 (Jx).

It's driving me insane - if i can get this part working, it will put me a huge step closer to finishing the project - and scoring huge kudos with the boss.

Code:
Sub list()
Fx = Dir("g:\marketing\invoices\" & "MISC*.xls")
Hx = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Jx = Sheets("Sheet2").Cells(Hx, 1)

Cells(1, 1) = Jx
Cells(2, 1).Select

    Do While Len(Fx) > 0
    Gx = Mid(Fx, 5, 4)
        If Gx > Jx Then
            ActiveCell.Formula = Gx
            ActiveCell.Offset(1, 0).Select
            Fx = Dir()
        End If
    Loop

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Try:

Code:
Sub list()
    Fx = Dir("g:\marketing\invoices\" & "MISC*.xls")
    Hx = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
    Jx = Sheets("Sheet2").Cells(Hx, 1)
    Cells(1, 1) = Jx
    Cells(2, 1).Select
    Do While Len(fx) > 0
        Gx = CInt(Mid(fx, 5, 4))
        If Gx > Jx Then
            ActiveCell.Formula = Gx
            ActiveCell.Offset(1, 0).Select
        End If
        fx = Dir()
    Loop
End Sub

In your code Gx was a string not a number. I also moved the Dir outside the If clause.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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