Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel: Adjust Macro to Fill Down

Posted on 2014-09-07
9
Medium Priority
?
236 Views
Last Modified: 2014-09-08
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

0
Comment
Question by:dabug80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40309212
Hi,

pls try

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


Regards
0
 
LVL 1

Author Comment

by:dabug80
ID: 40309228
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
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40309266
What is the value of uniquerow at line 42?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:dabug80
ID: 40309302
It's defined on row 9.
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40309309
try this instead

    Range("A2:A" & uniquerow).DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
        Step:=1, Trend:=False
0
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 2000 total points
ID: 40309399
Range("A2").AutoFill Destination:=Range("A2:A" & uniquerow), Type:=xlFillSeries

Open in new window


Kris
0
 
LVL 1

Author Comment

by:dabug80
ID: 40311150
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
0
 
LVL 18

Expert Comment

by:krishnakrkc
ID: 40311209
THe code is working fine in the workbook you attached.

Kris
0
 
LVL 1

Author Comment

by:dabug80
ID: 40311232
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question