Consulting

Results 1 to 4 of 4

Thread: Sum Cell range based on Worksheet title with wildcard.

  1. #1

    Sum Cell range based on Worksheet title with wildcard.

    I have a problem that I cannot solve.

    I have a pricing rollup tab that needs to reference all sheets with the Name "Price." and sum values in cell G105.
    The issue lies with there can be any text after "price."
    It also varies as it could be a number or it could be a state or a year.
    It also varies in the number of times the word Price appears in each file. The worksheet name "Price.xxxx" could appear twice or it could be 50 times.


    For Example
    Price.2018
    Price.AR
    Price.AR.2018
    Price.1015567678

    I started to write the a macro that outputs all sheet names in the range BQ1:BQ150 and I was going to use that sheet list to identify the word Price and use the wildcard character
    Code:
    Sub ListSheets() 
    Dim ws As Worksheet
    Dim cellNum As Integer
     
    cellNum = 1
    'Output list of sheets  
    For Each ws In Worksheets
         Sheets("Price.Rollup").Cells(cellNum, "BQ") = ws.Name
         cellNum = cellNum + 1
    Next ws
     
    End Sub


    The sheets I need to reference always start With "Price." and then the wildcard text
    G105 is the cell I need to sum across all sheets and input on G105 on the rollup tab .
    Cell B1 Contains the word "Price." on the Rollup Tab to reference and the use a wildcard indicator to perform the rest of the sheet name.

    The current formula I have is as follows:

    Code:
    =SUMPRODUCT(SUMIF(INDIRECT("'"&sheets&"'!"&"BQ1:BQ105"),B1&"."&"*",INDIRECT ("'"&sheets&"'!"&"G105")))

    If gives me the result #NAME ? as you cannot have wildcard with SumProduct.


    It would be much easier if i could just write this as a macro to stick in the template workbook before it gets sent out to each area.

    Also, I cannot post a copy of the Workbook as it contains confidential pricing information. So my apologies in advance for that.

    Please let me know if anymore information is needed.

    Thanks All!
    Last edited by Paul_Hossler; 10-25-2018 at 08:26 AM. Reason: adjust code tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Not tested, but maybe something like this


    Option Explicit
    
    Const addrPrefix As String = "B1"
    Const addrDataToSum As String = "G105"
    
    Sub Rollup()
        Dim ws As Worksheet
        Dim dSum As Double
        Dim sPrefix As String
        
        'trim prefix and make UC and add * for the Like
        sPrefix = Trim(UCase(Worksheets("Rollup").Range(addrPrefix))) & "*"     '   <<<<<< change
        
        For Each ws In ActiveWorkbook.Worksheets
            If UCase(ws.Name) Like sPrefix Then
                dSum = dSum + ws.Range(addrDataToSum)
            End If
        Next
        
        Worksheets("Rollup").Range(addrPrefix).Value = dSum
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Quote Originally Posted by Paul_Hossler View Post
    Not tested, but maybe something like this


    Option Explicit
    
    Const addrPrefix As String = "B1"
    Const addrDataToSum As String = "G105"
    
    Sub Rollup()
        Dim ws As Worksheet
        Dim dSum As Double
        Dim sPrefix As String
        
        'trim prefix and make UC and add * for the Like
        sPrefix = Trim(UCase(Worksheets("Rollup").Range(addrPrefix))) & "*"     '   <<<<<< change
        
        For Each ws In ActiveWorkbook.Worksheets
            If UCase(ws.Name) Like sPrefix Then
                dSum = dSum + ws.Range(addrDataToSum)
            End If
        Next
        
        Worksheets("Rollup").Range(addrPrefix).Value = dSum
    End Sub
    Thanks Paul, This worked great.
    I made one change to the code (last line) to where the output value was:
    Worksheets("Rollup").Range(addrPrefix).Value = dSum
    
    'Changed to
    
    Worksheets("Rollup").Range(addrDataToSum).Value = dSum

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I made one change to the code (last line) to where the output value was:
    Ooops -- glad you caught it. Careless Copy/Paste on my part
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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