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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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