+ Reply to Thread
Results 1 to 12 of 12

Add and add name

  1. #1
    Forum Contributor
    Join Date
    05-16-2017
    Location
    Spain
    MS-Off Ver
    2010
    Posts
    103

    Add and add name

    Good morning. I need a macro that in sheet2 adds the result of sheet 1. For example, in sheet 1 the number of Jhon's student appears, three times appear that I need to add and the number of student and the sum appear in sheet 2 of times in a single result. In the BD tab there is the student number and the name, I need you to compare the student number and put the name. I put on sheet 2 an example of how it should look. Thank you
    Attached Files Attached Files

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,855

    Re: Add and add name

    I offer you an alternative solution to VBA. Power Query

    Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

    It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

    - Follow this link to learn how to install Power Query in Excel 2010 / 2013.

    - Follow this link for an introduction to Power Query functionality.

    - Follow this link for a video which demonstrates how to use Power Query code provided.
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Add and add name

    Is the data always sorted as in your example first sheet? (Blocks of same student numbers)

  4. #4
    Forum Contributor
    Join Date
    05-16-2017
    Location
    Spain
    MS-Off Ver
    2010
    Posts
    103

    Re: Add and add name

    jolivanes. Thank you. Yes, the data would always be in the same column

  5. #5
    Forum Contributor
    Join Date
    05-16-2017
    Location
    Spain
    MS-Off Ver
    2010
    Posts
    103

    Re: Add and add name

    alansidman Thanks for the information, I had considered using Power Query, but unfortunately I use it on a company computer that does not allow any plugin to be installed.

  6. #6
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,452

    Re: Add and add name

    PHP Code: 
    Option Explicit
    Sub test
    ()
    Dim lr&, i&, k&, rngAs Stringsarr(), key
    Dim dic 
    As Object
    Set dic 
    CreateObject("Scripting.dictionary")
    With Worksheets("sheet1")
    lr = .Cells(Rows.Count"B").End(xlUp).Row
    rng 
    = .Range("B2:G" lr).Value
    ReDim arr
    (1 To lr 11 To 7)
        For 
    1 To lr 1
            t 
    rng(i2) & "|" rng(i3) & "|" rng(i4) & "|" rng(i5) & "|" rng(i6)
            If 
    Not dic.exists(rng(i1)) Then
                dic
    .Add rng(i1), t
            
    Else
                
    Split(dic(rng(i1)), "|")
                
    = (s(0) + rng(i2)) & "|" & (s(1) + rng(i3)) & "|" & (s(2) + rng(i4)) & "|" _
                    
    (s(3) + rng(i5)) & "|" & (s(4) + rng(i6))
                
    dic(rng(i1)) = t
            End 
    If
        
    Next
    End With
    0
    For Each key In dic.keys
        k 
    1
        arr
    (k2) = key
        arr
    (k1) = WorksheetFunction.VLookup(keyWorksheets("BD").Range("A2:B1000"), 20)
        For 
    0 To 4
            arr
    (k3) = Split(dic(key), "|")(i)
        
    Next
    Next
    With Worksheets
    ("sheet2")
        .
    Range("A2").Value "Name"
        
    .Range("B2:G2").Value Worksheets("sheet1").Range("B1:G1").Value
        
    .Range("A3").Resize(dic.Count7).Value arr
    End With
    End Sub 
    Attached Files Attached Files
    Quang PT

  7. #7
    Forum Contributor
    Join Date
    05-16-2017
    Location
    Spain
    MS-Off Ver
    2010
    Posts
    103

    Re: Add and add name

    Thanks bebo021999, it works fine. I have a doubt, if I want to extend to cells I and J, for example, what part of the code should I modify?
    And if at any time I don't want to put the name, what part should I delete.
    Thank you very much for your help

  8. #8
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,452

    Re: Add and add name

    With:
    lr = .Cells(Rows.Count, "B").End(xlUp).Row
    it runs to the last row of column B, that contains student numbers
    to not put the name, just remove these lines:
    PHP Code: 
    'arr(k, 1) = WorksheetFunction.VLookup(key, Worksheets("BD").Range("A2:B1000"), 2, 0)
    .Range("A2").Value "Name" 

  9. #9
    Forum Contributor
    Join Date
    05-16-2017
    Location
    Spain
    MS-Off Ver
    2010
    Posts
    103

    Re: Add and add name

    Thank you. What I wanted to know is if I need to put more cells to the right, column H, I. Attached photo. Thank you

    Time.JPG

    Now just copy up to column G on sheet2

    Time2.JPG
    Last edited by sprit36; 05-26-2022 at 05:25 AM.

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Add and add name

    Not near as sophisticated as bebo021999's code but it works on any amount of columns.
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    05-16-2017
    Location
    Spain
    MS-Off Ver
    2010
    Posts
    103

    Re: Add and add name

    jolivanes, thank you very much for the help, it's just what I was looking for. problem solved

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: Add and add name

    Thanks for letting us know that all is well again on the Iberian peninsula.
    Good luck

    Since the data in "sheet2" is cleared, towards the end of the code, change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.

+ 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