+ Reply to Thread
Results 1 to 9 of 9

Sum offset from found range

Hybrid View

  1. #1
    Registered User
    Join Date
    09-03-2018
    Location
    Holland
    MS-Off Ver
    2010
    Posts
    21

    Sum offset from found range

    Hi!

    I want to sum the values in column E by a found value in column A
    The answer of the sum must be displayed in a cell on another worksheet "NewData"

    So for example:

    Because the search word is "hello" the sum is E2 + E3 + E4 + E8

    sumhello.png

    This sum should be displayed in a cell on the other sheet

    I got to this but it doesn't work. Don; t know how to sum the offsetted values. Perhaps I took a wrong turn with this

    Sub SumFoundOffset()
    
    Dim wsData As Worksheet
    Set wsData = Worksheets("Data")
    
    Dim wsNewData As Worksheet
    Set wsNewData = Worksheets("NewData")
    
    Dim dataLastRow As Long
    Dim i As Long
    dataLastRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 1 To dataLastRow
            If wsData.Cells(i, 1).Value = "Houten kozijn" Then
                
               wsNewData.Cells("A1") = WorksheetFunction.Sum wsData.Cells(i, 5).Value
            
            End If
    Next
    
    End Sub
    Hope this is clear enough. There is a sample file attached. Any help would be more than welcome!

    Thanks!



    note: also posted this on a dutch support forum
    Attached Files Attached Files
    Last edited by TheOneWeDreamOf; 03-14-2019 at 09:19 AM.

  2. #2
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,767

    Re: Sum offset from found range

    Do you really need VBA for it?
    You can use SUMIF function, like:
    Formula: copy to clipboard
    =SUMIF(A:A,"hello",E:E)


    Check attached file.
    Attached Files Attached Files
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  3. #3
    Registered User
    Join Date
    09-03-2018
    Location
    Holland
    MS-Off Ver
    2010
    Posts
    21

    Re: Sum offset from found range

    Wow, really broke my brains over this in VBA and it's this simple... Thanks KOKOSEK

    Still I would really like to see how its done in VBA

  4. #4
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,767

    Re: Sum offset from found range

    Sub SumFoundOffset()
    Dim YourSum As Long
    YourSum = 0
    Dim wsData As Worksheet
    Set wsData = Worksheets("Data")
    
    Dim wsNewData As Worksheet
    Set wsNewData = Worksheets("NewData")
    
    Dim dataLastRow As Long
    Dim i As Long
    dataLastRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 1 To dataLastRow
            If wsData.Cells(i, 1).Value = "hello" Then
                YourSum = YourSum + wsData.Cells(i, 5).Value
                ' wsNewData.Cells("A1") = YourSum
            End If
    Next
    MsgBox ("Sum for hello is: " & YourSum)
    End Sub
    Last edited by KOKOSEK; 03-14-2019 at 09:43 AM.

  5. #5
    Registered User
    Join Date
    09-03-2018
    Location
    Holland
    MS-Off Ver
    2010
    Posts
    21

    Re: Sum offset from found range

    Thanks KOKOSEK.
    I get the message box but the sum is not displayed in A1. When I remove the ' in front of wsNewData.Cells("A1") = YourSum I get an error.

    What might be wrong?

  6. #6
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Sum offset from found range

    replace
    ("A1")
    with
    (1,1)
    in that line

    Possibly use an input box for the search items
    Dim mySearch As Variant
    mySearch = InputBox("Search for What")
    For i = 1 To dataLastRow
            If wsData.Cells(i, 1).Value = mySearch Then
                YourSum = YourSum + wsData.Cells(i, 5).Value
                wsNewData.Cells(1, 1) = YourSum
            End If
    Next
    Last edited by nigelog; 03-14-2019 at 10:04 AM.

  7. #7
    Registered User
    Join Date
    09-03-2018
    Location
    Holland
    MS-Off Ver
    2010
    Posts
    21

    Re: Sum offset from found range

    Thanks nigelog.

    Although I don't understand why "A1" didn't do the trick.

  8. #8
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Sum offset from found range

    Could use with
    wsNewData.Range("a1")

  9. #9
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,767

    Re: Sum offset from found range

    Quote Originally Posted by nigelog View Post
    Could use with
    wsNewData.Range("a1")
    Sorry, I've missed this. Cells(1,1) or Range("A1") as nicelog wrote.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Search for value in row. if found, copy column formulas into found column range
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2017, 10:42 AM
  2. Search for value in a range and overwrite if found and create new if not found
    By mm671750 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 02-04-2016, 01:19 PM
  3. [SOLVED] Help! - Copy Range (Sheet1,A10:F50), Paste Range (Sheet 2,A1:F41), Offset Range & Repeat
    By cjtimmer in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-30-2015, 06:27 PM
  4. Dynamic Range using Offset, range not found for Pivot
    By GoneBaja in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-08-2012, 09:19 AM
  5. Choosing offset on condition found in combobox change colour of cell??
    By Simon Lloyd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-03-2006, 07:53 PM
  6. Want to offset found value using Index
    By Doug Laidlaw in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-09-2005, 09:10 AM
  7. Problem with Range.Cells.Offset and Range.Cells( row + offset, column)
    By erazmus@actrix.co.nz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2005, 01:05 AM

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