For x = lastactivecell To 2 bottom top

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hello.
I have a dynamic array B2:F8686 I would like to be able to count and display on column G how many cells are used
so the last cell in use at the moment is F8686 so starting there with 1 and end up on G2 with the count 8686
VBA Code:
Sub Cn()
Dim x As Integer

  lRow = Cells(Rows.Count, 1).End(xlUp).Row

    For x = lRow To 2 Step -1
       Cells(x, 6) = x
    Next x

End Sub
I tried this but do not work
thank for reading this
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try:
Place the letter of the column that you take as a reference that has the last cell with data.
Rich (BB code):
lRow = Cells(Rows.Count, "F").End(xlUp).Row
 
Upvote 0
Thanks.
VBA Code:
Sub Cn()
Dim x As Integer

Application.ScreenUpdating = False
  
lRow = Cells(Rows.Count, "F").End(xlUp).Row

    For x = lRow To 2 Step -1
       Cells(x, 6) = x
    Next x
    
Application.ScreenUpdating = True

End Sub
doing that, what I got was that the whole column F was replace for the count, and F is part of my data, and the count instead to be 8686, 8685, 8684 was just a regular count from 2 to the end
in other words do not work, and took like 2 minutes to work. reason I add screen update.
and I understand taking F as a reference to count and display results on G.
like on G I was expecting something like
Book1
G
1
28686
38685
48684
58683
68682
78681
88680
98679
108678
118677
128676
138675
148674
158673
168672
17
Sheet1
 
Upvote 0
Your macro has several problems.
Ideally, you should start by writing what the final objective of your request is.
An example of what you have an example of the expected result.
That way it is easier for us to understand what you have and what you want.

The most practical, if possible, is to use Range and the letter of the column, example Range("F" & x), that way you can see which cell you are referring to.

One of the errors in your macro is that you are writing to column 6, that column is column "F", so your macro already had that problem.
Cells(x, 6) = x


Replace your macro with the following:
VBA Code:
Sub Cn()
  Dim x As Long, lRow As Long, n As Long
  Dim b As Variant
  
  lRow = Range("F" & Rows.Count).End(xlUp).Row
  ReDim b(1 To lRow - 1, 1 To 1)
  n = 1
  For x = lRow - 1 To 1 Step -1
    b(x, 1) = n
    n = n + 1
  Next x
  Range("G2").Resize(UBound(b)).Value = b
End Sub
Important Note: Row 1 you are not considering. If you have data up to row 8686, then you have 8685 data, so the count will go from 1 to 8685.

If you want the count to end at 8686, then the count should start at 2.
In that case change n = 1 to n = 2.
I hope I explain myself correctly.

Note: The process is faster when you use an array to store the data and then download the array to the cells.
--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 1
Solution

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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