AutoFill problem

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201
Hello,

In my script in some part Im useing autofill and it works proprly but when I have 2 or more records. If there is non or one record my macro is crushing.

I have something similar in several places in my code. I just need to have Id in column A from A2 going downn (1, 2, 3 etc) and none if no records are there.

Can You guys help me fix this?

my code

Code:
    Dim LastRowFX As Integer
   LastRowFX = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).row
        Cells(2, 1).Formula = "1"
        Cells(2, 1).AutoFill Destination:=Range(Cells(2, 1), Cells(LastRowFX, 1)), Type:=xlFillSeries
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi, maybe these slight alterations will help..

Rich (BB code):
Dim LastRowFX As Long
LastRowFX = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
If LastRowFX > 1 Then
     Cells(2, 1).Value = 1
     Cells(2, 1).AutoFill Destination:=Range(Cells(2, 1), Cells(LastRowFX, 1)), Type:=xlFillSeries
End If
 
Upvote 0
Autofill from Range class Failed (or something like that.. I do not have english excel)

it breaks at line
Code:
  Cells(2, 1).AutoFill Destination:=Range(Cells(2, 1), Cells(LastRowFX, 1)), Type:=xlFillSeries

In row 1 I have Headers btw.
 
Upvote 0
Maybe try it on a new workbook with just values in columns A & B and see if it works for you, if it does then try to figure out what is different with the workbook where it fails?
 
Upvote 0
I got it right with LooP


Code:
Dim LastRowFX As Long
Dim countA As Long


    LastRowFX = Cells(Rows.count, 2).End(xlUp).row
    countA = 1


    For lrowFX = 2 To LastRowFX
         Sheets(1).Cells(lrowFX, 1) = countA
        countA = countA + 1
    Next lrowFX
 
Upvote 0
It works if number of records <> 2. (including headers)

We only have a small snippet of code and no sample data and I can't replicate the error so you are going to have to do some investigating and debugging yourself- maybe change the IF test to >2 ?
 
Last edited:
Upvote 0
Ok, so In new blank sheet in column B row 1 and 2 put any vlues and start the macro. It will break. If Column B will have data in only row 1 or at least in rows 1, 2, 3 - macro will work.
 
Last edited:
Upvote 0
Ok, so In new blank sheet in column B row 1 and 2 put any vlues and start the macro. It will break. If Column B will have data in only row 1 or at least in rows 1, 2, 3 - macro will work.

OK, so how about..

Code:
Dim LastRowFX As Long
LastRowFX = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
If LastRowFX > 1 Then
    Cells(2, 1).Value = 1
    If LastRowFX > 2 Then
        Cells(2, 1).AutoFill Destination:=Range(Cells(2, 1), Cells(LastRowFX, 1)), Type:=xlFillSeries
    End If
End If
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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