+ Reply to Thread
Results 1 to 5 of 5

Autofill in VBA with a dynamic "Destination" range

Hybrid View

ConneXionLost Autofill in VBA with a... 05-04-2011, 04:02 PM
TMS Re: Autofill in VBA with a... 05-04-2011, 04:25 PM
snb Re: Autofill in VBA with a... 05-04-2011, 04:30 PM
ConneXionLost Re: Autofill in VBA with a... 05-04-2011, 04:37 PM
TMS Re: Autofill in VBA with a... 05-04-2011, 04:55 PM
  1. #1
    Forum Expert ConneXionLost's Avatar
    Join Date
    03-11-2009
    Location
    Victoria, Canada
    MS-Off Ver
    2010
    Posts
    2,952

    Autofill in VBA with a dynamic "Destination" range

    I'm trying to set up this macro to autofill in VBA:

    Sub filldateinfo()
     
    '   Format the new cells
        Range("D2").Select
        Selection.Copy
        Range("F2:G2").Select
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    '   Add the formulas to get the year and month
        Range("F2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(OR(MID(RC4,3,1)=""."",MID(RC4,3,1)=""-"",MID(RC4,3,1)=""/""),RIGHT(RC4,4),IF(OR(MID(RC4,5,1)=""."",MID(RC4,5,1)=""-"",MID(RC4,5,1)=""/""),LEFT(RC4,4),""""))"
        Range("G2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(OR(MID(RC4,3,1)=""."",MID(RC4,3,1)=""-"",MID(RC4,3,1)=""/""),LEFT(RC4,2),IF(OR(MID(RC4,5,1)=""."",MID(RC4,5,1)=""-"",MID(RC4,5,1)=""/""),RIGHT(RC4,2),""""))"
    '   Autofill to the end of the table
       Range("F2:G2").Select
       Selection.AutoFill Destination:=Range("F2:G8489")  'How do I make this dynamic based on the length of column A?
       Range("F2:G8489").Select  'How do I make this dynamic based on the length of column A?
    '   Replace the formulas with values
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    '   Re-arrange the table
        Columns("E:E").Select
        Application.CutCopyMode = False
        Selection.Cut Destination:=Columns("H:H")
        Columns("F:H").Select
        Selection.Cut Destination:=Columns("D:F")
        Range("A1").Select
     
    End Sub
    The length of the table will vary, so I need the autofill to determine it's own "Destination" value based on the length of column A.

    See attached for sample.

    Cheers,
    Attached Files Attached Files
    Last edited by ConneXionLost; 05-04-2011 at 04:35 PM.
    Would you like to say thanks? Please click the: " Add Reputation" button, on the grey bar below the post.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,100

    Re: Autofill in VBA with a dynamic "Destination" range

    Try this:

    Sub dateinfo()
    
    '   Format the new cells
        Range("D2").Copy
        With Range("F2:G2")
            .PasteSpecial Paste:=xlPasteFormats, _
                          Operation:=xlNone, _
                          SkipBlanks:=False, _
                          Transpose:=False
        End With
        Application.CutCopyMode = False
    '   Add the formulas to get the year and month
        Range("F2").FormulaR1C1 = _
            "=IF(OR(MID(RC4,3,1)=""."",MID(RC4,3,1)=""-"",MID(RC4,3,1)=""/""),RIGHT(RC4,4),IF(OR(MID(RC4,5,1)=""."",MID(RC4,5,1)=""-"",MID(RC4,5,1)=""/""),LEFT(RC4,4),""""))"
        Range("G2").FormulaR1C1 = _
            "=IF(OR(MID(RC4,3,1)=""."",MID(RC4,3,1)=""-"",MID(RC4,3,1)=""/""),LEFT(RC4,2),IF(OR(MID(RC4,5,1)=""."",MID(RC4,5,1)=""-"",MID(RC4,5,1)=""/""),RIGHT(RC4,2),""""))"
    '   Autofill to the end of the table
        Range("F2:G2").AutoFill Destination:=Range("F2:G" & Range("A" & Rows.Count).End(xlUp).Row) 'How do I make this dynamic based on the length of column A?
    '   Replace the formulas with values
        With Range("F2:G" & Range("A" & Rows.Count).End(xlUp).Row)
            .Copy
            .PasteSpecial Paste:=xlPasteValues, _
                          Operation:=xlNone, _
                          SkipBlanks:=False, _
                          Transpose:=False
        End With
        Application.CutCopyMode = False
    '   Re-arrange the table
        Columns("E:E").Cut Destination:=Columns("H:H")
        Columns("F:H").Cut Destination:=Columns("D:F")
        Range("A1").Select
    
    End Sub


    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Autofill in VBA with a dynamic "Destination" range

    Avoid 'select' and 'activate' in VBA.

    Sub snb()
      Range("D2").Copy Range("F2:G2")
      with Range("F2:G2")
        .FormulaR1C1 ="=IF(OR(MID(RC4,3,1)=""."",MID(RC4,3,1)=""-"",MID(RC4,3,1)=""/""),RIGHT(RC4,4),IF(OR(MID(RC4,5,1)=""."",MID(RC4,5,1)=""-"",MID(RC4,5,1)=""/""),LEFT(RC4,4),""""))"
         .AutoFill .resize(cells(rows.count,1).end(xlup).row-1)
         with .resize(cells(rows.count,1).end(xlup).row-1)
           .value=.value
         end with
      end with
    End Sub



  4. #4
    Forum Expert ConneXionLost's Avatar
    Join Date
    03-11-2009
    Location
    Victoria, Canada
    MS-Off Ver
    2010
    Posts
    2,952

    Re: Autofill in VBA with a dynamic "Destination" range

    Works great!

    Thanks for the help in cleaning up the code too!

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,100

    Re: Autofill in VBA with a dynamic "Destination" range

    You're welcome. Thanks for the feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1