Link to home
Start Free TrialLog in
Avatar of dabug80
dabug80

asked on

Excel: Adjust Macro to Fill Down

Hello,

Below is my current macro code.

I am seeking help to fill down the 'A' rows shown in lines 35-42. The rows are to fill down to correspond with the content in row B (no blanks until the dataset ends).

Currently this code only fills down for 30 rows, and I'm not sure why.


Sub mac_copyunique()
'
' mac_copyunique Macro
' This macro copies all the formatted data, pastes it to the UniqueID page and removes any row with a duplicate address. This will therebycreate a Unique ID
    
    Dim uniquerow As Long
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Unique ID")
    uniquerow = ws.Range("B" & Rows.Count).End(xlUp).Row

' This is the first part - copying all the data and pasting it to the Unique ID page
    Sheets("Closed Address reformatting").Select
    Columns("F:O").Select
    Selection.Copy
    Sheets("Unique ID").Select
    Range("B1").Select
    ActiveSheet.Paste
    Rows("1:5").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    Cells.Select
    ActiveSheet.Range("$A:$J").RemoveDuplicates Columns:=6, Header:= _
        xlYes
    ActiveWorkbook.Worksheets("Unique ID").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Unique ID").Sort.SortFields.Add Key:=Range( _
        "I:I"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Unique ID").Sort
        .SetRange Range("A:J")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
' This is the second part, creating adding new content to column A - and filling down with sequential numbers.
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ID"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("A2:A3").AutoFill Destination:=Range("A2:A3" & uniquerow)
    
    End With
    Sheets("Closed Address reformatting").Select
    Range("E6").Select
End Sub

Open in new window

Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try

Range("A2:A3").AutoFill Destination:=Range("A2:A" & uniquerow)


Regards
Avatar of dabug80

ASKER

Thanks. I got the error:

Run-time error 1004: AutoFill method of Range class failed

The line of code you gave was highlighted in the debug
What is the value of uniquerow at line 42?
Avatar of dabug80

ASKER

It's defined on row 9.
try this instead

    Range("A2:A" & uniquerow).DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
        Step:=1, Trend:=False
ASKER CERTIFIED SOLUTION
Avatar of krishnakrkc
krishnakrkc
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dabug80

ASKER

Hello,

Sadly none of the suggestions successfully fill down the sequential numbers for column A (Sheet: Unique ID).

Attached is a sample workbook to show my data. There is only one macro.

I'm trying to get the code line (42) to fire correctly so that column A of the sheet Unique ID is filled down with sequential numbers all the way to the bottom of the dataset.

Cheers.

Sub mac_copyunique()
'
' mac_copyunique Macro
' This macro copies all the formatted data, pastes it to the UniqueID page and removes any row with a duplicate address. This will therebycreate a Unique ID
    
    Dim uniquerow As Long
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Unique ID")
    uniquerow = ws.Range("B" & Rows.Count).End(xlUp).Row

' This is the first part - copying all the data and pasting it to the Unique ID page
    Sheets("Closed Address reformatting").Select
    Columns("F:O").Select
    Selection.Copy
    Sheets("Unique ID").Select
    Range("B1").Select
    ActiveSheet.Paste
    Rows("1:5").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    Cells.Select
    ActiveSheet.Range("$A:$J").RemoveDuplicates Columns:=6, Header:= _
        xlYes
    ActiveWorkbook.Worksheets("Unique ID").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Unique ID").Sort.SortFields.Add Key:=Range( _
        "I:I"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Unique ID").Sort
        .SetRange Range("A:J")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
' This is the second part, creating adding new content to column A - and filling down with sequential numbers.
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ID"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("A2").AutoFill Destination:=Range("A2:A" & uniquerow), Type:=xlFillSeries
    
    End With
    Sheets("Closed Address reformatting").Select
    Range("E6").Select
End Sub

Open in new window

sample2.xlsm
THe code is working fine in the workbook you attached.

Kris
Avatar of dabug80

ASKER

You're right Kris! The code works when there's existing content on the sheet. When I delete the content on the sheet (Unique ID) then the fill down doesn't work correctly. But that's ok - since there will always be data on the sheet to override.

I will mark as solved