How to auto-fill a column with consecutive numbers using VBA

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
634
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have data in columns A - C and the number 1 in cell D1.

Does anyone know how I can drag the numbers from cell D1 to the rest of the column D (up to the last row of the data in column A) in consecutive order?

So my data would initially look like this:

ADG
1​
BEH
CFI


However, I would like it to look like the table below (NB - the number of rows will vary, so the drag would have to be dynamic rather than fixed). TIA.

ADG
1​
BEH
2​
CFI
3​
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,855
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
One way
VBA Code:
Range("D1").AutoFill Range("D1:D" & Range("A" & Rows.Count).End(xlUp).Row), xlFillSeries
 

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
634
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Excellent!!

Thank you very much, @MARK858

That worked!!
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,855
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
You're welcome
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Another way ..

VBA Code:
Range("D1:D" & Range("A" & Rows.Count).End(xlUp).Row).DataSeries , xlLinear
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

You're welcome. I'm not suggesting that it is a better way, just a different way. :)
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
963
Office Version
  1. 2010
hi, guys nice code but what if there is no number in d1 it 's empty how is the code starts number 1
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,855
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
How is Excel supposed to know what number you want to start with if you don't input it. (Either manually or by code)?
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
963
Office Version
  1. 2010
hi, mark but i think some codes do that if specify the number rows by function do that if cell is filling then the next cell give the number without i write manually i'm not sure it just guess
 

Watch MrExcel Video

Forum statistics

Threads
1,122,804
Messages
5,598,140
Members
414,214
Latest member
marketingnumbersguy

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