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
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
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.
 

wisewood

Board Regular
Joined
Nov 7, 2002
Messages
193
Thanks Andrew.

I'm a rather shoddy PHP man usually - VBA is much more strict.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,577
Messages
5,596,996
Members
414,116
Latest member
sfullnet

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
Top