IF Not ISEmpty Autofill problem

RaydenUK

Board Regular
Joined
Mar 25, 2014
Messages
74
I have a macro that will autofill column C with the contents of "C1" to the last used row from column A. This code works great unless row 1 is the only row. That is why I used "If Not IsEmpty("A2") Then" because I wanted it to recognize that if "A2" is empty then do not autofill. When i use this code on only 1 row then i get the error "1004: Autofil method of range class failed". Can anyone help me out with this code? Thank you.

Code:
Sub UsedRange()

    Dim lastrow As Long
    
   If Not IsEmpty("A2") Then
       lastrow = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
       Range("C1").AutoFill Range("C1:C" & lastrow), xlFillCopy
   End If


End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Only one cell to select and fill itself; thus the error. Update your Autofill statement as:

Code:
If lastrow > 1 Then Range("C1").AutoFill Range("C1:C" & lastrow), xlFillCopy
 
Upvote 0
How many rows of data are present in your spreadsheet? What value does lastrow variable show?
 
Last edited:
Upvote 0
i actually use this code for several different macros but in one particular macro that gives the debug error there may be 1 row or 200 rows. The row number is random depending the amount of samples that i have for that day.
 
Upvote 0
I tried this code but it doesn't do anything as if I never ran the code.

Are you saying that it doesn't autofill in any data sample set at all, or just few?
I want to know how many rows were there when you saw this behavior. I can't just guess the root cause issue without some information.
 
Upvote 0
Lets say there are 3 rows of data. Columns A1:A3 have numbers in them, columns B1:B3 has names, and C1 has a group name. I need the group name in C1 to fill down to C3. With my original code this worked fine. However, if I only had one row present then it would throw a debug message. Now with this code it will not do anything whether there is 2 rows or 10 rows of info, rather it won't fill down like it should. This is fine if there is only 1 row because it does nothing as it should. When i use the lastrow variable in a message box then it always shows "2", no matter how many rows are filled in.
 
Upvote 0
Well the logic you originally had for lastRow variable is intact, so it couldn't have been the code change.
lastRow variable is populated with last row number of active sheet. Is the sheet, on which you are trying to autofill, active when you run this code? Maybe lastRow is getting row number from some other sheet which is active.
 
Upvote 0
Yes it is active, however, I may not have used the lastrow variable correctly. How would you suggest doing it. I'm sorry I am still fairly new to coding.
 
Upvote 0
There doesn't seem to be anything wrong with your code. I'm running it (with a slight variation) and it fills down values correctly.

Code:
Sub UsedRange()
    Dim lastrow As Long
  
    lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    If lastrow > 1 Then Range("C1").AutoFill Range("C1:C" & lastrow), xlFillCopy
End Sub

Does C1 cell always have a value or is it blank?
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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