For Loop not completing

somnath.deb

New Member
Joined
Sep 1, 2011
Messages
4
Hi All,
I am relatively new to macro and I was trying to use For loop in my code. It appears the loop is not running till end. My excel has some date columns but there are few columns where there is a gap in date between two consecutive columns. So what I was trying to do is to insert a column whereever there is a gap. and fill out the column header with the appropiate coulmn value. Below is the code that I wrote:
Code:
For i = 6 To lastcol
    colname1 = ColLetter(i)
    colname2 = ColLetter(i - 1)
    If (DateDiff("d", CDate(Range(colname2 & CStr(2)).Value), CDate(Range(colname1 & CStr(2)).Value)) > 1) Then
        For j = 1 To DateDiff("d", CDate(Range(colname2 & CStr(2)).Value), CDate(Range(colname1 & CStr(2)).Value)) - 1
            Columns(colname1).Select
            Selection.Insert Shift:=xlToRight
            nxtdt = DateAdd("d", 1, CDate(Range(colname2 & CStr(2)).Value))
            Range(colname1 & CStr(2)).Value = nxtdt
            Range(colname1 & CStr(2)).Select
            curcol = ActiveCell.Column
            colname1 = ColLetter(curcol + 1)
            colname2 = ColLetter(curcol)
            lastcol = lastcol + 1
            i = i + 1
        Next j
    End If
Next i
MsgBox i & " , " & j & " , " & lastcol
on executing the code the value that I am getting for i is 40 and of lastcol as 52 (lastcol is the last column of the datarange).
Will deeply apreciate if anyone can guide me on the mistake that I am making.
Thanks
-Som
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Welcome to MrExcel.

If you are inserting columns you need to loop backwards, otherwise you will never reach the last column because it's greater than it was at the start.
 
Upvote 0
There is a small miss I should have mentioned. Column2 is my first colun and column1 is the next one.. I am not a programmer so it may look little odd.. however the columns are getting populated and date are getting inserted properly. The only hitch is that the loop is not completing till the lastcol
 
Upvote 0
There is a small miss I should have mentioned. Column2 is my first colun and column1 is the next one.. I am not a programmer so it may look little odd.. however the columns are getting populated and date are getting inserted properly. The only hitch is that the loop is not completing till the lastcol

Btw thank you for accepting my membership and the quick review
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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