+ Reply to Thread
Results 1 to 8 of 8

Adjust formula to allow for changing sheet reference.

Hybrid View

  1. #1
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Post Adjust formula to allow for changing sheet reference.

    I was using the formula below which was working fine for copying across 20 columns and down however many rows, but now the requirements have changed to 90 columns. I have tested this out with the 90 columns but the Indirect function is bogging down the spreadsheet with the constant recalculating.

    =IFERROR(INDIRECT("'"&COLUMNS($A$1:A$1)&"'!P"&ROWS($A$1:$A8)),"")

    I rearranged the formula to the one below, but how can I have the reference to the sheet name changed as in the formula above without using the Indirect?

    =IFERROR(INDEX('1'!$A$8:$AT$115,MATCH($A3,'1'!$A$8:$A$115,0),MATCH("PAT",'1'!$A$7:$AT$7,0)),"")

    The sheet reference will change from '1' through '90'.
    Last edited by jeffreybrown; 11-23-2009 at 11:09 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adjust formula to allow for changing sheet reference.

    One way would be to add a macro that can insert formulas into each row based on the the correct sheet name reference, however you want the macro to determine that. The main goal is to have normal direct formulas in the cells.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Post Re: Adjust formula to allow for changing sheet reference.

    Hi JB,

    Yes I was thinking about a macro, but would some type of loop work replacing each sheet reference with the next variable starting from 1?

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Post Re: Adjust formula to allow for changing sheet reference.

    Hi again JB,

    So far I have come up with this, but it change the '1' to '2' throughout the range B1:H1. What am I missing to make it loop through all the numbers up to 7. I am just testing a small scale right now so therefore up to only 7.

    Sub FixFormula()
    Application.DisplayAlerts = False
    Dim y As Integer
        For y = 2 To 7
            With Sheets(CStr("Sheet1")).Range("B1:H1")
                .Cells.Replace What:="'1'", _
                Replacement:="'" & y & "'", _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                MatchCase:=False, _
                SearchFormat:=False, _
                ReplaceFormat:=False
            End With
        Next y
    Application.DisplayAlerts = True
    End Sub

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adjust formula to allow for changing sheet reference.

    If we're going to go the macro route, there's no reason not to let the macro simply collate your report for you with no formulas. You simply want the data from row1 on every sheet collated into a report on sheet1? That's probably easiest of all.

    Option Explicit
    
    Private Sub Worksheet_Activate()
    Dim ws As Worksheet, NR As Long
    Application.ScreenUpdating = False
    Cells.Clear
    NR = 1
    
        For Each ws In Worksheets
            If ws.Name <> Me.Name Then
                Cells(NR, "A") = ws.Name
                ws.Range("B1:H1").Copy Cells(NR, "B")
                NR = NR + 1
            End If
        Next ws
        
    Cells.Columns.AutoFit
    Application.ScreenUpdating = True
    End Sub
    Just delete the macro you were using. Then open the SHEET MODULE for this sheet and insert the macro above into the sheet module. This will cause the macro to run itself every time you bring the sheet up onscreen...which makes it as effective as =A1 formulas.

    In case you don't know...the sheet module is listed above the regular modules, open the actual module with the sheetname that is your 'report'. Another way to open it is to right-click on the sheet tab and select VIEW CODE.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Post Re: Adjust formula to allow for changing sheet reference.

    Hi JB,

    That doesn't seem to be what I am looking for. I attached a small sample wb so you can see what I am trying to accompllish.

    On the PAT sheet starting in cell A2 I need to copy the formula to the right over 90 columns but with every cell stating in B2 the formula needs to reference sheet '2' and so on until '90'.

    Again, I was using the Indirect formula which worked fine, but now with the spreadsheet getting bigger and the volatile nature of the Indirect I needed to change to another formula so I went with the Index/Match.

    Index/Match works fine, but without manually changing all of the sheet references how can I automate the process.

    If this is possible then I can even do away with the Index/Match and just simple go with '1'!A1,'2'!A1,'3'!A1,'4'!A1,'5'!A1,'6'!A1,'7'!A1 and so on util '90'.
    Attached Files Attached Files

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Adjust formula to allow for changing sheet reference.

    So row 2 is all you need? There's no row 3 formulas, and row 4?

    This standalone macro goes in a standard module. Run it and will create formulas in row2 based on the sheet names found in row1. They don't have to be 1,2,3...etc. They can be anything. You'll get a normal REF# error if the value in row1 doesn't match an existing sheet.
    Option Explicit
    
    Sub AddFormulas()
    'JBeaucaire  (11/22/2009)
    Dim LC As Long, i As Long
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    LC = Cells(1, Columns.Count).End(xlToLeft).Column
    
    For i = 1 To LC
        Cells(2, i).Formula = "='" & Cells(1, i) & "'!A1"
        'add more rows here to add more formulas in the following rows...
    Next i
        
    Cells.Columns.AutoFit
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    You can duplicate the syntax in the red line of code to add more formulas to get data from other cells on each sheet.

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Post Re: Adjust formula to allow for changing sheet reference.

    JB,

    This is perfect. Thanks

+ 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