+ Reply to Thread
Results 1 to 25 of 25

Help Extracting Data- Probably VBA for looping?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Question Help Extracting Data- Probably VBA for looping?

    Hi everyone,

    I am very rusty at my Excel skills, and it seems like I may need to use VBA with this one. I have a spreadsheet that is about 4000+ rows of data: Names, Access Levels, and card numbers. The problem is that there is one name associated with multiple rows of data, and it isn't consistent. Basically I need to take this data, and have the person's name, access levels, and card number in column format so that I can concatenate and put into CSV format.

    In the attached workbook, you'll see the raw data sheet, and then the desired result page. Is there a way to use the vlookup on a merged first cell with multiple adjacent rows, and dump that data into multiple columns like second example?

    Any assistance is greatly appreciated. I'm trying to help out a friend, and am now stuck on trying to figure it out because I want to.
    Attached Files Attached Files
    Last edited by flexchick; 01-27-2018 at 07:30 PM. Reason: Added Workbook

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help Extracting Data- Probably VBA for looping?

    If you upload a workbook with the sheets what you have and what you want as a result will help.
    Picture doesn't help usually.

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  3. #3
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Thanks- this is my first post here so I appreciate the advice.

  4. #4
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Help Extracting Data- Probably VBA for looping?

    you can substitute char(10) with a space - char(32) in column Access Level then concatenate columns

    edit: forget it , you're using merged cells
    Last edited by sandy666; 01-27-2018 at 07:32 PM.

  5. #5
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Thanks, but I know how to concatenate- it's getting the data into the columns in the first place that I need help with. Merged first column with multiple rows attached. I uploaded an example workbook since I did a crappy job of explaining what I needed. Thank you anyway!!

    The setup for the raw data was not my idea- I can unmerge cells, but that creates different problem or having blank cell associated with the data I need.
    Last edited by flexchick; 01-27-2018 at 07:37 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Help Extracting Data- Probably VBA for looping?

    Sub fc()
      Dim iRowInp As Long
      Dim iRowOut As Long
      Dim rAcc As Range
      
      iRowInp = 2
      Do Until IsEmpty(Cells(iRowInp, "A").Value)
        iRowOut = iRowOut + 1
        Cells(iRowOut, "E") = Cells(iRowInp, "A").Value2
        Cells(iRowOut, "F") = Cells(iRowInp, "C").Value2
        Set rAcc = Intersect(Cells(iRowInp, "A").MergeArea.EntireRow, Columns("B"))
        Cells(iRowOut, "G").Resize(, rAcc.Rows.Count).Value = WorksheetFunction.Transpose(rAcc.Value2)
        iRowInp = iRowInp + rAcc.Rows.Count
      Loop
      
      Columns.AutoFit
    End Sub
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Thank you. Each name and card number have merged cells, and the access levels vary in rowsize (anywhere from 1 to 4 access levels). How do I tell it to look up the name, and then copy the rows next to it to paste into columns on new worksheet, and then move to the next name? Can you tell me what I'm putting as the ranges? It would be so much easier if I could send the actual spreadsheet...lol.

  8. #8
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: Help Extracting Data- Probably VBA for looping?

    or something like this with PowerQuery
    Attached Files Attached Files

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help Extracting Data- Probably VBA for looping?

    Try
    Sub test()
        Dim a, i As Long, ii As Long, n As Long, t As Long, w
        a = Sheets("raw data").Cells(1).CurrentRegion.Value
        ReDim b(1 To UBound(a, 1), 1 To 3): n = 1
        b(n, 1) = a(1, 1): b(n, 2) = a(1, 2)
        For i = 2 To UBound(a, 1)
            If a(i, 1) <> "" Then
                n = n + 1: t = 2
                b(n, 1) = Array(a(i, 1), a(i, 3)): b(n, 2) = a(i, 2)
                ii = 1
                Do While a(i + ii, 1) = ""
                    t = t + 1
                    If UBound(b, 2) < t Then ReDim Preserve b(1 To UBound(b, 1), 1 To t)
                    b(n, t) = a(i + ii, 2): ii = ii + 1
                    If i + ii > UBound(a, 1) Then Exit Do
                Loop
                i = i + ii - 1
            End If
        Next
        ReDim Preserve b(1 To UBound(b, 1), 1 To UBound(b, 2) + 1)
        b(1, UBound(b, 2)) = a(1, UBound(a, 2))
        For i = 2 To n
            w = b(i, 1)
            b(i, UBound(b, 2)) = b(i, 1)(1): b(i, 1) = b(i, 1)(0)
        Next
        With Sheets("desired output").Cells(1).Resize(n, UBound(b, 2))
            .Value = b
            .Columns.AutoFit: .Parent.Activate
        End With
    End Sub
    Attached Files Attached Files

  10. #10
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Help Extracting Data- Probably VBA for looping?

    To whom you are talking?

  11. #11
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Sandy666, how did you get the merged name fields and accesses lined up in that power query?

  12. #12
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Cool Re: Help Extracting Data- Probably VBA for looping?

    here is M code:
    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"NAME", type text}, {"ACCESS LEVELS", type text}, {"CARD#", Int64.Type}}),
        #"Demoted Headers" = Table.DemoteHeaders(#"Changed Type"),
        #"Changed Type1" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type text}, {"Column2", type text}, {"Column3", type any}}),
        #"Transposed Table" = Table.Transpose(#"Changed Type1"),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Transposed Table", {{"Column2", type text}}, "en-GB"),{"Column2", "Column3", "Column4", "Column5"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"),
        #"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns", {{"Column6", type text}}, "en-GB"),{"Column6", "Column7", "Column8", "Column9"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged.1"),
        #"Merged Columns2" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns1", {{"Column10", type text}}, "en-GB"),{"Column10", "Column11"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged.2"),
        #"Merged Columns3" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns2", {{"Column13", type text}}, "en-GB"),{"Column13", "Column14", "Column15"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged.3"),
        #"Transposed Table1" = Table.Transpose(#"Merged Columns3"),
        #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
        #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers",{{"NAME", type text}, {"ACCESS LEVELS", type text}, {"CARD#", Int64.Type}})
    in
        #"Changed Type2"


    Data - show queries - double click on Table 1 - Menu - Advanced Editor
    or look at Applied Steps in Query Settings (after open Table 1)

    Edit:
    If you want more see:
    Last edited by sandy666; 01-27-2018 at 09:15 PM. Reason: see edit

  13. #13
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Thanks bunches. For some reason, I'm not seeing Power Query in my Data tab- I have the regular query, but my build of 2016 must be different. There are about 4,000+ lines of data, and it looks like you manually created each of the merged columns? I already transposed the table, it was just getting it into that final format. It would be quicker to just cut and paste than create a query for each name?

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help Extracting Data- Probably VBA for looping?

    flexchick

    Have you tried my attachment in #8?

  15. #15
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Thank you jindon- that one does what I need. Aside from making sure the sheet names are the same in .xlsm coding, what else will I need to change to get it to work on my larger dataset?

  16. #16
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Hi Sandy- I have 2016- and it shows as "Get and Transform" in my version. Thanks for all of the helpful information!

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help Extracting Data- Probably VBA for looping?

    Quote Originally Posted by flexchick View Post
    Thank you jindon- that one does what I need. Aside from making sure the sheet names are the same in .xlsm coding, what else will I need to change to get it to work on my larger dataset?
    It should work in any size of the data.
    If you have problem, I need to see your workbook.

  18. #18
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Help Extracting Data- Probably VBA for looping?

    Power Query in Ex2016 is called: Get&Transform

    gt.jpg

    Maybe your Excel is 365 not 2016 with a little different layout

    read more about PowerQuery / Get&Transform from links above
    Last edited by sandy666; 01-27-2018 at 09:42 PM.

  19. #19
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: Help Extracting Data- Probably VBA for looping?

    You are welcome

    Why MS changed this name - I don't know
    but IMHO PowerQuery sounds much better than Get&Transform, eg. use Power (Query) Luke !

  20. #20
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Thanks Jindon- can I private message you with this?

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help Extracting Data- Probably VBA for looping?

    PM sent...............

  22. #22
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Thanks- reply sent. Let me know what you think!

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help Extracting Data- Probably VBA for looping?

    OK, I see
    Just replace
        a = Sheets("raw data").Cells(1).CurrentRegion.Value
    with
        With Sheets("raw data")
            a = Intersect(.Columns("a:c"), .UsedRange).Value
        End With
    And see how it goes.

  24. #24
    Registered User
    Join Date
    01-27-2018
    Location
    Oregon, USA
    MS-Off Ver
    Office 2016
    Posts
    15

    Re: Help Extracting Data- Probably VBA for looping?

    Jindon, it worked beautifully. Thanks again. That was so very helpful!!

    Thank you also to Sandy, and anyone else replying. This is a wonderful resource.

  25. #25
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Help Extracting Data- Probably VBA for looping?

    You are welcome and thanks for the feedback.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ 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] Extracting and transposing data by looping
    By TexasBobcat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-17-2015, 02:20 PM
  2. [SOLVED] Extracting data from table using VBA looping
    By TexasBobcat in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-17-2015, 01:17 PM
  3. [SOLVED] Extracting Data from Table by Looping
    By TexasBobcat in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-17-2015, 01:16 PM
  4. Extracting Data from Table By looping
    By TexasBobcat in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-17-2015, 01:16 PM
  5. Looping through multiple files in a folder and extracting data from a specific tab
    By Bandicoot in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-05-2013, 05:24 PM
  6. Looping thru files extracting data
    By gtslabs in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-03-2005, 12:05 PM

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