+ Reply to Thread
Results 1 to 9 of 9

Thread: Calculate sum and insert text in multiple sheets

  1. #1
    Registered User
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    96

    Calculate sum and insert text in multiple sheets

    Hi!

    In all the sheets in the workbook I need to check column F for containing a cell of value "Commission". If it does, the numbers in the cells of the column following "Commission" needs to be summed together and the sum is insertet in the first blank cell following the numbers that are summed.
    Also, if the sum is calculated, in column A I need insertet a text two rows following last cell containing value. It is better shown in the attached workbook. The code is as follows (but not working):

    Public Sub Workbook_1()
    
    For Each sh In ActiveWorkbook.Sheets
    
        With sh
            
            If IsError(Lookup("Commission", f)) = True Then Next sh
               
            Else: Range(Lookup("Commission", f), .Cells(Rows.Count, "f").End(xlUp)).Select
            
            Sum = Sum(Selection.Value)
            
                .Range(, .Cells(Rows.Count, "f").Offset(1, 0)) = Sum
                    
            Range(Cells(Rows.Count, "a").End(xlUp).Row).Select
            Selection.Offset(2, 0).Value = "Insert text"
            
            End If
            
        End With
    
    Next sh
    
    End Sub
    Any help is appreciated! Thanks in advance.
    Attached Files Attached Files
    Last edited by timtim89; 02-08-2012 at 09:32 AM.

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,371

    Re: Calculate sum and insert text in multiple sheets

    Put this code in a blank module of your file (Press Alt + F11, a code window will open. On the left hand side, you will see Microsoft Excel Objects, right click and select Insert -> Module. Copy this code there)
    Option Explicit
    Dim fcell As Range
    Dim lrow As Long
    Dim fcomm As Long
    Dim i As Long
    
    Sub sum_text()
    
    For i = 1 To Worksheets.Count
        With Worksheets(i)
            Set fcell = .Range("F:F").Find(what:="Commission")
            fcomm = fcell.Row
            lrow = .Range("F" & Rows.Count).End(xlUp).Row
    
            .Range("F" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("F" & fcomm + 1 & ":F" & lrow).Value)
            .Range("A" & lrow + 2).Value = "Insert Text"
        End With
    Next i
    
    End Sub
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    96

    Re: Calculate sum and insert text in multiple sheets

    Hey Arlette. Thank you so much for the code.
    In the workbook in which I'll use the code I have additional sheets, in which there is no "Commission" found. I got an error message, and added
    On Error Resume Next
    but this does that the text (and sometimes sum) are inserted anyway in the sheets. How do I avoid this? That the text and sum are not inserted in sheets in which "Commission" is not found? My bad for the attachment no corresponding to the real data, and thanks again!

  4. #4
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,371

    Re: Calculate sum and insert text in multiple sheets

    Change On Error Resume Next to On Error goto errhandler and then before next i put this line Errhandler:
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Registered User
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    96

    Re: Calculate sum and insert text in multiple sheets

    Thank you. It works perfect now

  6. #6
    Registered User
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    96

    Re: Calculate sum and insert text in multiple sheets

    Now that I'm applying the macro on the data that I actually need it for, I get an run time error 91 saying "Object variable or With block variable not set". This I got after adding a sheet to the workbook in which the data worked. The macro still works as supposed to, but anyone who can tell me how to get rid of the error message? I attached the workbook once again with the new data and code. Any help is appreciated, and once again: thanks a lot in advance

    Option Explicit
    
    Sub sum_text1()
    
    Dim fcell As Range
    Dim lrow As Long
    Dim fcomm As Long
    Dim i As Long
    
    For i = 1 To Worksheets.Count
        With Worksheets(i)
        On Error GoTo Errhandler
            Set fcell = .Range("F:F").Find(what:="Commission")
            fcomm = fcell.Row 'here i get the error ("Object variable or With block variable not set")
            lrow = .Range("F" & Rows.Count).End(xlUp).Row
    
            .Range("F" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("F" & fcomm + 1 & ":F" & lrow).Value)
            .Range("A" & lrow + 2).Value = "Insert Text"
        End With
    Errhandler:
    Next i
    
    End Sub
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    96

    Re: Calculate sum and insert text in multiple sheets

    If it helps in any way, I'm also getting the error message if I simply add an empty sheet to the first three sheets (for which the code works).

  8. #8
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Calculate sum and insert text in multiple sheets

    That is not how you use an error handler, but you don't need one here anyway
    Sub sum_text()
    
    For i = 1 To Worksheets.Count
        With Worksheets(i)
            Set fcell = .Range("F:F").Find(what:="Commission")
            if not fcell is nothing then
            fcomm = fcell.Row
            lrow = .Range("F" & Rows.Count).End(xlUp).Row
    
            .Range("F" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("F" & fcomm + 1 & ":F" & lrow).Value)
            .Range("A" & lrow + 2).Value = "Insert Text"
            end if
        End With
    Next i
    
    End Sub
    Good luck.

  9. #9
    Valued Forum Contributor
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    380

    Re: Calculate sum and insert text in multiple sheets

    Try handling the errors this way...
    Sub sum_text1()
    
        Dim fcell As Range
        Dim lrow As Long
        Dim fcomm As Long
        Dim i As Long
    
        For i = 1 To Worksheets.Count
            With Worksheets(i)
                Set fcell = .Range("F:F").Find(what:="Commission")
                If Not fcell Is Nothing Then
                    fcomm = fcell.Row
                    lrow = .Range("F" & Rows.Count).End(xlUp).Row
                    .Range("F" & lrow + 1).Value = Application.WorksheetFunction.Sum(.Range("F" & fcomm + 1 & ":F" & lrow).Value)
                    .Range("A" & lrow + 2).Value = "Insert Text"
                End If
            End With
        Next i
    
    End Sub

  10. #10
    Registered User
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    96

    Re: Calculate sum and insert text in multiple sheets

    OnErrorGoto0 and dangelor thanks to both of you! If Not Is Nothing line did the trick

+ 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.2.0