Current Macro does does not completely go through all rows.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,
My current macro does work but it seems it quits too early. Mind you the row numbers will vary to day. Any help would greatly be appreciated.

VBA Code:
Sub Outbound_Input_Cops()
    Dim i As Long
    For i = 11 To ActiveSheet.Cells(Rows.count, 4).End(xlUp).Row
    Select Case ActiveSheet.Cells(i, 11).Value
    Case "2300"
ActiveSheet.Range("I" & i).Value = ActiveSheet.Range("I" & i).Value & "1 COP"
    Case "4600"
ActiveSheet.Range("I" & i).Value = ActiveSheet.Range("I" & i).Value & "2 COPs"
    Case "6900"
ActiveSheet.Range("I" & i).Value = ActiveSheet.Range("I" & i).Value & "3 COPs"
    Case "9200"
ActiveSheet.Range("I" & i).Value = ActiveSheet.Range("I" & i).Value & "4 COPs"
End Select
Next i
End Sub


This is what I get with the current macro as seen above.

COPS.JPG
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I think the issue is with your loop here:
VBA Code:
For i = 11 To ActiveSheet.Cells(Rows.count, 4).End(xlUp).Row
you are looking at column D to find the last row of data, and then looping through all rows starting in row 11 down to the last row with data in column D.

That does not seem to match your image.
Note that cells with formatting will not be counted in your last row calculation. Only rows with something in them (data or a formula).
 
Upvote 0
I think the issue is with your loop here:
VBA Code:
For i = 11 To ActiveSheet.Cells(Rows.count, 4).End(xlUp).Row
you are looking at column D to find the last row of data, and then looping through all rows starting in row 11 down to the last row with data in column D.

That does not seem to match your image.
Note that cells with formatting will not be counted in your last row calculation. Only rows with something in them (data or a formula).
That is really strange, because I thought 11 would be column K. Select F5 definitely gave me the image you see. The bottom 5 rows are populating no problem.
 
Upvote 0
Yes they are, but why is it populating the bottom 5 rows, but not the rest.?

Because formulas and VBA do not play well with merged cells and you can get unexpected errors or results.

That is really strange, because I thought 11 would be column K. Select F5 definitely gave me the image you see. The bottom 5 rows are populating no problem.
11 is col K, however, the "4" in this line: ActiveSheet.Cells(Rows.count, 4) is indicating col D.
 
Upvote 0
That is really strange, because I thought 11 would be column K. Select F5 definitely gave me the image you see. The bottom 5 rows are populating no problem.
The structure of Cells in VBA is:
Cells(row number, column reference)

So the row number is the first argument, and the column reference is the second (you have it backwards!)

Note that the column reference can either be the column letter, or the numerical value of the column.
Since column K is the 11th column, you can refer to cell K4 either like this:
VBA Code:
Cells(4, 11)
or
VBA Code:
Cells(4, "K")
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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