+ Reply to Thread
Results 1 to 15 of 15

Code to add numbers

  1. #1
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Code to add numbers

    I’m looking for help to develop some code to add numbers in two columns. I CAN NOT use a formula because their is problems with other VBA code I run in the same cell range when I do that.

    The code would search 2 ranges G4:G10, L4:L10 (I will need to be able to add additional ranges). Some cells will have no data in them and others will have data (numbers, 2 decimal places). Some ranges may or may not have numbers in them but there will always be 1 or more numbers (to be added up) in at least one of the ranges.
    So in my sample data image the first range G4:G10 in cell G4 has number 1.00, cell G9 has a 2.00. The code will add up the two numbers and reference cell E3 which has a 1-1 in it. Using the 1-1 reference the code wold then search range B3:B4 for the 1-1 and then write the added numbers in the adjacent cell C3. Code would do the same procedure for each range. If a range has no numbers please have the code write a 0 in the appropriate cell in the C column.
    The 2 cell range are formatted as numbers and the sets of numbers (cells E3 and J3) are formatted as text.

    That would do it. If you have any questions or something is not clear please let me know so I can clarify it.

    Thanks so much for all your help.

    SAMPLE DATA.png

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,037

    Re: Code to add numbers

    It's difficult to work with a picture. Could you attach a copy of your file? Also include more ranges with sample data.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    12-01-2016
    Location
    Birmingham, AL
    MS-Off Ver
    standard 2016
    Posts
    8

    Re: Code to add numbers

    Hello,

    I'm not sure when you want this to execute. On thought is to add a shape or a form control to the sheet and assign this macro to it.

    However, there are other ways you can initiate this code. Let me know if you're looking for something different.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Code to add numbers

    Hi Mumps1

    Thanks so much for your reply. I have uploaded my Excel sheet with additional data. Please let me know if anything is unclear.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Code to add numbers

    @ button clicker

    Hi button clicker, thanks so much for your reply. I have tried the code and I’m sure if it’s me or what but I get an error message “Method Range of object Worksheet failed”. My sheet name is Sheet1 and it’s the only sheet in the workbook, would that be the problem?

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,037

    Re: Code to add numbers

    Will any additional ranges also be located below in columns G and L? The ranges in column G contain 7 cells and the ranges in column L contain 8 cells. Will this always be the pattern? Will the data always start in row 3?

  7. #7
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Code to add numbers

    No there no patterns. The range and cells within the range will be different and could be located anywhere on the sheet. Also, my sheet name is Sheet1 and it’s the only sheet in the workbook.

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,037

    Re: Code to add numbers

    When you say "could be located anywhere on the sheet", it is impossible to come up with a solution. VBA depends largely on patterns in data organization. In order to come up with a working solution, you need to attach a file which shows how your data can be organized with enough data to give us an idea of the bigger picture. Will the reference cell always be one row up and two columns to the left of each range?

  9. #9
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Code to add numbers

    Yes to "Will the reference cell always be one row up and two columns to the left of each range?' is it possible to do it with this information?

  10. #10
    Registered User
    Join Date
    12-01-2016
    Location
    Birmingham, AL
    MS-Off Ver
    standard 2016
    Posts
    8

    Re: Code to add numbers

    There are several ways to accomplish this. I would recommend defining the names of the ranges.
    I have done this on the 4 ranges in your attachment.
    rng1_1 = .Range("G4:G10")
    rng2_1 = .Range("L4:L11")
    rng3_1 = .Range("G16:G22")
    rng4_1 = .Range("L16:L23")
    You can continue to define as many ranges and results spots as you would like. Within VBA i've
    included additional code which is commented out with an apostrophe. You can follow that methodology
    for as many ranges and their results as you would like.

    There is the option of doing an offset method, which sounds like Mumps is leaning toward.

    Sample attached. Gl
    Attached Files Attached Files

  11. #11
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,037

    Re: Code to add numbers

    It helps but it is not enough. Could you please attach a file that contains more ranges, perhaps 8 or 10 to give a clearer picture? If the ranges are of different sizes, please include a good variety.

  12. #12
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Code to add numbers

    Hi Mumps1

    Looked at my layout and can now confirm that I will stay within a fixed range/layout for every range. I've attached a new workbook. here is an image as well.

    Attachment 628251
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this ‼


    Usually for this kind of stuff I use easy formulas !
    Anyway, according to your last attachment a demonstration as a beginner starter :

    PHP Code: 
    Sub Demo1()
            
    Dim R&, Rf As RangeA$
                
    2
                
    [B3].CurrentRegion.ClearContents
        With ActiveSheet
    .UsedRange.Columns
            With 
    .Item(- .Column 1).Resize(, .Count + .Column)
                   
    Set Rf = .Find("-", , xlValuesxlPartxlByRows)
                If 
    Not Rf Is Nothing Then
                        A 
    Rf.Address
                        Application
    .ScreenUpdating False
                    
    Do
                        
    1
                        Cells
    (R2).Resize(, 2).Value2 = Array(Rf.Value2Application.Sum(Rf(13).Resize(8)))
                           
    Set Rf = .FindNext(Rf)
                    
    Loop Until Rf.Address A
                           Set Rf 
    Nothing
                        Application
    .ScreenUpdating True
                End 
    If
            
    End With
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 06-13-2019 at 08:12 PM. Reason: optimization for a single sheet in the workbook …

  14. #14
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Code to add numbers

    @button clicker

    Hi button clicker

    Sorry for the delay in getting back to you. I just finished testing your code and you got it bang on. Thank-you so much. I really appreciate this.

    Christine

  15. #15
    Registered User
    Join Date
    10-14-2018
    Location
    Brampton, ontario
    MS-Off Ver
    2007
    Posts
    74

    Re: Hi ! Try this ‼

    @Marc L

    Just finished testing your code and it works great.Thank-you.

+ 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] VBA code for sequential numbers for a column containing letters and numbers
    By YuriyBaron in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-26-2018, 10:03 PM
  2. Code that picks numbers out from a set
    By jhenry330 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-20-2018, 04:41 PM
  3. [SOLVED] Find Max # in zip code range and then sum all numbers in that zip code
    By amyp22x3 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 01-08-2013, 12:25 PM
  4. code to convert numbers to other set of numbers
    By jfkeith in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-10-2010, 08:22 PM
  5. Need to reconcile numbers accounting Harlan Grove code doesn't work for negative numbers
    By aep002@cox.net in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2006, 02:15 AM
  6. Need to reconcile numbers accounting Harlan Grove code doesn't work for negative numbers
    By aep002@cox.net in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2006, 10:15 PM
  7. Need code to pair off numbers
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-16-2006, 02:50 AM

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