Error handling with drag

Mr2017

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

I've got some code where I insert the number 1 into cell D2 (after it has been cleared), then drag down the number 1 and fill a series of numbers, depending on the number of rows in column A.

So if there are 3 rows (excluding the title row ie row 1) in column A, then cells D2, D3 and D4 will be 1, 2 and 3, respectively.

If rows there are 2 rows (excluding the title row ie row 1) in column A, then cells D2, D3 will be 1 and 2, respectively.

However, the macro doesn't work if there is only 1 row of data in column A (excluding the title row), because it doesn't know that it should NOT drag any data.

Does anyone know how to prevent it from giving an error, if there is only one row of data in column A?

Thanks in advance.



VBA Code:
'Make cell D2 = 1 

Range("D2").Select
ActiveCell.Value = 1

'populate column D

Range("D2").AutoFill Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row), xlFillSeries
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about
VBA Code:
Dim UsdRws As Long
UsdRws = Range("A" & Rows.Count).End(xlUp).Row
If UsdRws > 2 Then Range("D2").AutoFill Range("D2:D" & UsdRws), xlFillSeries
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,457
Messages
6,124,941
Members
449,197
Latest member
k_bs

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