+ Reply to Thread
Results 1 to 4 of 4

Autofill Formula down a Column across multiple worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    05-15-2013
    Location
    Glasgow
    MS-Off Ver
    Excel 2010
    Posts
    56

    Autofill Formula down a Column across multiple worksheets

    I have a workwook with multiple worksheets - I'm calling the macro to copy the formula down the column(s).

    I'm having a problem replacing the active cell references from the recorded macro and passing in the correct worksheet references.

    Here's the macro calling the various macros:

    Sub Perform_Data_Cleanup()
    Dim ws As Worksheet
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    
        For Each ws In ThisWorkbook.Worksheets
    
            If ws.Name <> "Control" Then
                Call copy_subject_Column(ws)          'Copies the Subject Column to the from D to I
                'Call Replace_Re_Fw(ws)          'run remove RE: and FW: from column G on all worksheets
                'Call Text_to_Column_dash(ws)    'run text to columns macro on all worksheets (Dash)
                Call Fill_Date(ws)
                Call Replace_Re_Fw(ws)              ' replaces the "RE": and "Fw:" to allow for date manipulation on the subject headers
                Call Separate_Subject(ws)
            End If
    
        Next ws
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = True
    End Sub

    and here's the one i'm having problems with:


    Sub Separate_Subject(ws As Worksheet)
    '
    ' Separates the Subject Column into two columns, before **MIDART" and after **MIDART**
    '
    
        Range("J1").Select
        ActiveCell.FormulaR1C1 = "Left Subject"
        Range("J2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-1])),RC[-1],LEFT(RC[-1],FIND(""**MIDART**"",RC[-1])-1))"
       JRange("I2").Select
        ActiveCell.AutoFill Destination:=Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, -1).End(xlDown)).Offset(0, 1)
        Range("J1").Select
        ActiveCell.FormulaR1C1 = "Right Subject"
       Range("K2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-2])),RIGHT(RC[-2],LEN(RC[-2])-FIND(""**MIDART**"",RC[-2])))"
      Range("K2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-2])),"""",RIGHT(RC[-2],LEN(RC[-2])-FIND(""**MIDART**"",RC[-2])))"
       .Range("K2").Select
         ActiveCell.AutoFill Destination:=Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, -1).End(xlDown)).Offset(0, 1)
    End Sub
    Just not sure if this is the smartest way of doing this, or if im using the righth ws references here! cant seem to get my head round them! any help, gratefully received! Thanks

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Autofill Formula down a Column across multiple worksheets

    Hello Rikkdh,

    Good going so far. Keep on trying.

    Just check this line:
    JRange("I2").Select
    and change it to

    Range("I2").Select
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Registered User
    Join Date
    05-15-2013
    Location
    Glasgow
    MS-Off Ver
    Excel 2010
    Posts
    56

    Re: Autofill Formula down a Column across multiple worksheets

    yeah, typo on my part...

    so when corrected, it works on the active worksheet thats selected...

    Trouble is that I want to Call it to run as on its own so it runs on all 20 worksheets in the workbook....

    Sub Separate_Subject(ws As Worksheet)
    '
    ' Separates the Subject Column into two columns, before **MIDART" and after **MIDART**
    '
    
        Range("J1").Select
        ActiveCell.FormulaR1C1 = "Left Subject"
        Range("J2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-1])),RC[-1],LEFT(RC[-1],FIND(""**MIDART**"",RC[-1])-1))"
       Range("J2").Select
        ActiveCell.AutoFill Destination:=Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, -1).End(xlDown)).Offset(0, 1)
        Range("k1").Select
        ActiveCell.FormulaR1C1 = "Right Subject"
       Range("K2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-2])),RIGHT(RC[-2],LEN(RC[-2])-FIND(""**MIDART**"",RC[-2])))"
      Range("K2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-2])),"""",RIGHT(RC[-2],LEN(RC[-2])-FIND(""**MIDART**"",RC[-2])))"
       Range("K2").Select
         ActiveCell.AutoFill Destination:=Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, -1).End(xlDown)).Offset(0, 1)
    End Sub
    not sure where to pass in the worksheet referneces to get it to do this, can anyone point me in the right direction?

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Autofill Formula down a Column across multiple worksheets

    Maybe:

    Sub rikkdh()
    Dim ws As Worksheet
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    
        For Each ws In ThisWorkbook.Worksheets
        
        '
    ' Separates the Subject Column into two columns, before **MIDART" and after **MIDART**
    '
        ws.Activate
    
        If ws.Name <> "Control" Then
        Range("J1").Select
        ActiveCell.FormulaR1C1 = "Left Subject"
        Range("J2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-1])),RC[-1],LEFT(RC[-1],FIND(""**MIDART**"",RC[-1])-1))"
       JRange("I2").Select
        ActiveCell.AutoFill Destination:=Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, -1).End(xlDown)).Offset(0, 1)
        Range("J1").Select
        ActiveCell.FormulaR1C1 = "Right Subject"
       Range("K2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-2])),RIGHT(RC[-2],LEN(RC[-2])-FIND(""**MIDART**"",RC[-2])))"
      Range("K2").Select
        ActiveCell.FormulaR1C1 = _
            "=IF(ISERROR(FIND(""**MIDART**"",RC[-2])),"""",RIGHT(RC[-2],LEN(RC[-2])-FIND(""**MIDART**"",RC[-2])))"
       .Range("K2").Select
         ActiveCell.AutoFill Destination:=Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, -1).End(xlDown)).Offset(0, 1)
        End If
        
        Next ws
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = True
    
    
    End Sub
    Or

    Sub rikkdhA()
    Dim ws As Worksheet
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    
        For Each ws In ThisWorkbook.Worksheets
        
        ws.Activate
    
        If ws.Name <> "Control" Then
        
        Call Separate_Subject
        
        Next ws
    
        Application.DisplayAlerts = False
        Application.ScreenUpdating = True
    
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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