Do While.... Loop

finaljustice

Board Regular
Joined
Oct 6, 2010
Messages
175
Hey there, I have been writting a code and I decided to use a Do While Loop command to perform what I wished. The problem is, for what I know, the code only follows through if the statement is true. For some reason, the vba code is not understanding or misunderstanding the figures. (Obviouslly this can be a simple mistake, but since I am very tiered I haven't spotted any mistakes)


Code:
Sub inputnf()
Dim maxrng, uprng, botrng As Variant


Sheets(1).Select
Range("L2").Select
celladd = ActiveCell.Address
uprng = Replace(celladd, "$", "-", 1, 1)
uprng = Mid(uprng, InStr(1, uprng, "$") + 1, Len(uprng) - InStr(1, uprng, "$"))
maxrng = Range("C1048576").End(xlUp).Address
maxrng = Replace(maxrng, "$", "-", 1, 1)
maxrng = Mid(maxrng, InStr(1, maxrng, "$") + 1, Len(maxrng) - InStr(1, maxrng, "$"))


Do While uprng < maxrng
    
    botrng = Range("L" & uprng).End(xlDown).Offset(-1, 0).Address
    botrng = Replace(botrng, "$", "-", 1, 1)
    botrng = Mid(botrng, InStr(1, botrng, "$") + 1, Len(botrng) - InStr(1, botrng, "$"))
    Range("L" & uprng).Copy
    Range("A" & uprng & ":" & "A" & botrng).PasteSpecial (xlPasteValues)
    
    uprng = Range("L" & uprng).End(xlDown).Address
    uprng = Replace(uprng, "$", "-", 1, 1)
    uprng = Mid(uprng, InStr(1, uprng, "$") + 1, Len(uprng) - InStr(1, uprng, "$"))
    
Loop
End Sub

The problem is that I would expect uprng < maxrng since, the uprng = 2 to start with and maxrng = 1690 ergo, 2<1690 = True.... but it doesn't accept this as true.... if I change the sign around so it becomes uprng > maxrng, aka 2>1690 = TRUE but it's not!!!

If I leave it as last stated, I get in the loop but quickly come out after 3 loops...


I suspect this might be something really simple/ obvious I am missing.

Can anyone shine a light please?
Thanks for your time.
Final
 
What is the code supposed to do?
 
Upvote 0
What is the code supposed to do?
Column A is completely blank, column L has a invoice code only for the first item of every invoice. So lets say, invoice 1 has 3 items the invoice code will appear only for the first item leaving me with 2 blank lines. So what I am trying to automate is, starting from line 2 column L get the upper range, the lower range and copy the invoice number that is present only in online to the entire range of that invoice.

Code:
Sub inputnf()
Dim maxrng, uprng, botrng As Variant


Sheets(1).Select
Range("L2").Select
celladd = ActiveCell.Address
uprng = Replace(celladd, "$", "-", 1, 1)
uprng = Mid(uprng, InStr(1, uprng, "$") + 1, Len(uprng) - InStr(1, uprng, "$")) 'find the upper range of the invoice
maxrng = Range("C1048576").End(xlUp).Address
maxrng = Replace(maxrng, "$", "-", 1, 1)
maxrng = Mid(maxrng, InStr(1, maxrng, "$") + 1, Len(maxrng) - InStr(1, maxrng, "$")) 'find the last line of the table


Do While uprng < maxrng
    
    botrng = Range("L" & uprng).End(xlDown).Offset(-1, 0).Address
    botrng = Replace(botrng, "$", "-", 1, 1)
    botrng = Mid(botrng, InStr(1, botrng, "$") + 1, Len(botrng) - InStr(1, botrng, "$")) 'finds bottom limit of the invoice being copied.
    Range("L" & uprng).Copy
    Range("A" & uprng & ":" & "A" & botrng).PasteSpecial (xlPasteValues) 'pastes the invoice number that was only in one line to the entire range of that invoice.
    
    uprng = Range("L" & uprng).End(xlDown).Address 'since in column L only 1 invoice number appears no matter how many lines are used by that invoice, by skipping down, the next filled in cell will be another invoice. So get that line a repeat the process.
    uprng = Replace(uprng, "$", "-", 1, 1)
    uprng = Mid(uprng, InStr(1, uprng, "$") + 1, Len(uprng) - InStr(1, uprng, "$"))
    
Loop






End Sub

Hope I have explained better what I tried doing, I added comments on the lines i figured are important.

The other thing I think is strange is 2>1690 being given as TRUE!
 
Last edited:
Upvote 0
Does this code do what you want?
Code:
Sub FillInvoiceBlanks()
  On Error Resume Next
  Range("L2:L" & Cells(Rows.Count, "C").End(xlUp).Row).SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
  On Error GoTo 0
  Columns("L").Value = Columns("L").Value
End Sub
 
Upvote 0

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