+ Reply to Thread
Results 1 to 18 of 18

Parsing data in a range of cells with VBA/Macro to replace content

Hybrid View

  1. #1
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Smile Parsing data in a range of cells with VBA/Macro to replace content

    Hi everyone,

    I'm looking for a way to parse data in a spreadsheet via as automated a method as possible to save manually wading through cell after cell in multiple workbooks.

    The data is imported with several values to a cell and what I would like to do is be able to select a range of cells and then parse them so that only one value per cell is left.

    I've figured out via guides online how to use the LEFT and FIND functions to be able to complete part of my parse, but in doing so to another cell rather than replacing the previous data. I've no idea how to take this forwards and turn a simple formula into a VBA solution however.

    What I'd like to be able to do is parse the cells and leave the results in each cell, a kind of parse and replace function if you will.

    I've attached a simple workbook that shows the source data and also shows beneath the two types of results I'd like to try and achieve.

    1. Being able to parse the data to leave only the % values present (in the same cell)
    2. Being able to the parse data to leave only the values in the () present (in the same cell)

    Could somebody point me in the right direction please. What I'm hoping to be able to do ultimately is tie the macro to a button in the ribbon (Excel 2010 user) so I can select a range of cells, click on the macro and it then runs through each cell and parses by either method 1 or method 2 depending on which results I'd prefer to see. So 2 macros, 2 buttons, one for each I was thinking.

    Obviously, if there's a better way (I've been using Excel for years but am new to VBA) please do point me in the right direction.

    Many thanks for any assistance you can offer.
    Attached Files Attached Files
    Last edited by lad29; 10-12-2011 at 03:29 PM.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Generally worksheet functions are faster so you should stick with them and just use your formula on a different sheet. However since you have asked for a macro to replace your data, see the below, there are two subs and one function:

    And can be called like
    GetPercentage Sheet1.range("A2:F8")
    or
    GetNoBrackets Sheet1.range("A2:F8")
    Public Sub GetPercentage(rng As Range)
    Dim vCells As Variant
    Dim x As Long
    Dim y As Long
    
    vCells = rng.Value
    
    For x = LBound(vCells) To UBound(vCells)
        For y = LBound(vCells, 2) To UBound(vCells, 2)
            vCells(x, y) = Left(vCells(x, y), InStr(1, vCells(x, y), "%"))
        Next y
    Next x
    
        rng.Value = vCells
    
    End Sub
    
    
    
    Public Sub GetNoBrackets(rng As Range)
    Dim vCells As Variant
    Dim x As Long
    Dim y As Long
    
    vCells = rng.Value
    
    For x = LBound(vCells) To UBound(vCells)
        For y = LBound(vCells, 2) To UBound(vCells, 2)
            vCells(x, y) = ExtractBrackets(vCells(x, y))
        Next y
    Next x
    
        rng.Value = vCells
    
    End Sub
    
    
    Public Function ExtractBrackets(ByVal strIn As String) As String
    Dim regEx As Object
    Dim regExM As Object
    
    
     Set regEx = CreateObject("vbscript.regexp")
        With regEx
            .Pattern = "[(][0-9]*[)]"
            .MultiLine = False
            .Global = False
            .IgnoreCase = True
        End With
         
        Set regExM = regEx.Execute(strIn)
        ExtractBrackets = Mid(regExM(0), 2, Len(regExM(0)) - 2)
        
        
    End Function
    The faster way with no looping would be to apply the formula to a range then copy the results back over the source data, however I don't know how far your data extends though you could do this on another sheet.

    Something like:
    Sub Percentages(rng As Range)
    Dim rng1 As Range
    Set rng1 = Sheet2.Cells(1, 1).Resize(rng.Rows.Count, rng.Columns.Count)
    rng1.Formula = "=LEFT(Sheet1!A2,FIND(""%"",Sheet1!A2))"
    rng.Value = rng1.Value
    rng1.ClearContents
    End Sub
    Last edited by Kyle123; 10-12-2011 at 07:10 AM.

  3. #3
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Hi Kyle,

    Thanks for the quick and detailed reply. I'll take a good look at the code you've provided this evening when I'm home from work.

    Just quickly though, could you maybe explain a little bit more the following for me? I'm a VBA newbie so any extra info is much appreciated. I wasn't particularly looking to use the formula mentioned, it was just the best I'd come up with myself to be able to use as proof of concept that parsing cells was at least possible, even if only manually via copying output into a new cell.

    Quote Originally Posted by Kyle123 View Post
    Generally worksheet functions are faster so you should stick with them and just use your formula on a different sheet.
    Thanks again.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Sure, worksheet functions are faster than VBA code so should generally be your first stop to finding a solution - though there are situations where vba is preferable.

    You could just use the formula you've created to parse the data and paste this over the top of your source data

  5. #5
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Quote Originally Posted by Kyle123 View Post
    Sure, worksheet functions are faster than VBA code.....
    Thanks again, I don't really know what a "worksheet function" is, but I've bought myself a newbie book to VBA to get started with and will do some research online to get up to speed on what worksheet functions are too.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    or use
    Sub snb()
     sn = Cells(1).CurrentRegion
     For j = 0 To UBound(sn) * UBound(sn, 2) - 1
      If InStr(sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1), "(") > 0 Then sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1) = Split(sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1), "(")(0)
     Next
        
     Cells(1).CurrentRegion.Offset(, UBound(sn, 2) + 2) = sn
    End Sub
    and

    Sub snb2()
     sn = Cells(1).CurrentRegion
     For j = 0 To UBound(sn) * UBound(sn, 2) - 1
      If InStr(sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1), "(") > 0 Then sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1) = Split(Split(sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1), "(")(1), ")")(0)
     Next
        
     Cells(1).CurrentRegion.Offset(UBound(sn) + 2, UBound(sn, 2) + 2) = sn
    End Sub



  7. #7
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Hi all,

    I've been having a quick run through of your posts before my tea. Thanks again for all your suggestions, it's clear that I'm way out of my league when it comes to these sorts of solutions!

    Kyle123 - apologies but I've no idea what to do with the code you've suggested. Do I drop it into VBA, how do I call it if so? I need to read up on what subs and functions are and how they relate, so will be starting there in my learning probably.

    Watersev - I've dropped your code into VBA and assigned a macro to it. It correctly parses out the data to new cells on different sheets. A sheet for the percentages parsed, and a sheet for just the totals.

    That's great but I want the one result or the other and I want the results to replace the existing data not create new sheets. Can the code be tweaked to do that?

    snb - thanks for your idea too. That worked well, but again it created the parsed data in a different set of cells. Can the code be tweaked to replace the original content? Could you maybe explain the code a little to point out how it offsets the parsed data for the percentages in sub snb 2 columns to the right of the original data, and in sub snb2, 2 columns right and 2 rows down?

    Most importantly of all however, is it possible to run these against highlighted cells rather than fixed positions?

    Each worksheet will have the data I want to parse in differing places you see, so ideally I want to be able to highlight the cells, click on macro, parse data and replace the original cells.

    That way the other data that surrounds these cells that don't need parsing remain intact and I can ultimately export the worksheet elsewhere for presentation.

    In a way, what I'm trying to do is format the content to be presentable but that content may be present at various places within worksheet depending on the data imported.

    Thanks again guys, and apologies if I'm asking too much.

    Any pointers for good learning/reference material so I can start to pick up the basics myself too?

    Cheers.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    If you doubleclick in the leftupper cell of the range that has to be parsed:

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
      sn = Target.CurrentRegion
    
      For j = 0 To UBound(sn) * UBound(sn, 2) - 1
       If InStr(sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1), "(") > 0 Then sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1) = Split(sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1), "(")(0)
      Next
           
      Target.CurrentRegion = sn
    End Sub

    NB. Put this code into the worksheet codemodule.
    Last edited by snb; 10-12-2011 at 03:08 PM.

  9. #9
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Hi snb,

    I've been taking another look at your code and was wondering, is it possible to run the same code against a defined cell range?

    I've done a little reading about CurrentRegion and I think what I gather from how it works is that it works on contiguous data from top left cell to bottom right cell. Is that correct?

    So, really what I'm wondering therefore is as some of the data I want to alter with this method has clear row spaces between data sections (and I need that spacing to remain for presentation and formating). Can I specify in the code an entire cell range instead, say A1:H100 for example which would then result in the two types of data sitting to the right of the originals?

    I've played around with the code to figure out how the OffSet works but am not sure how to go about replacing CurrentRegion if it is at all possible in the first place.

    Thanks.

    Quote Originally Posted by snb View Post
    or use
    Sub snb()
     sn = Cells(1).CurrentRegion
     For j = 0 To UBound(sn) * UBound(sn, 2) - 1
      If InStr(sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1), "(") > 0 Then sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1) = Split(sn(j \ UBound(sn, 2) + 1, j Mod UBound(sn, 2) + 1), "(")(0)
     Next
        
     Cells(1).CurrentRegion.Offset(, UBound(sn, 2) + 2) = sn
    End Sub
    Last edited by lad29; 10-13-2011 at 11:03 AM.

  10. #10
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    hi, lad29, though it can be easily done with formulas, VB option, run code "test"
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Thanks watersev, I'll take a look at that attachment later too.

    Appreciate there's different ways to get a result so it's great to see those different methods to get me on the right track to learning.

    Cheers.

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    hi watersev,

    you made a slight typo:
    Sheets.Add(after:=Sheets(Sheets.Count)).Range("a1").Resize(rows_number, rows_number) = z
    should read:
    Sheets.Add(after:=Sheets(Sheets.Count)).Range("a1").Resize(rows_number, rows_number) = output2

  13. #13
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    @ tnx Kyle123, attachment updated
    Attached Files Attached Files
    Last edited by watersev; 10-12-2011 at 08:08 AM. Reason: found another one

  14. #14
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    hi, lad29, please check attachment, select range and press {ALT+LEFT ARROW} to parse percentage or {ALT+RIGHT ARROW} to parse numbers
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Quote Originally Posted by watersev View Post
    ...select range and press {ALT+LEFT ARROW} to parse percentage or {ALT+RIGHT ARROW} to parse numbers
    Wow !!!


  16. #16
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    watersev, that's fantastic!! Thank you so much.

    One more daft question then... how does the {ALT+LEFT ARROW} and {ALT+RIGHT ARROW} combinations work? Did you assign them somehow to the subs?

  17. #17
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    yes, the buttons combination is assigned when workbook is opened and reseted when the workbook is closed. The code for this part sits in ThisWorkbook level module under Workbook_Open and Workbook_Beforeclose events respectively. The code is very small and easy to understand with VB help file at hand.

  18. #18
    Registered User
    Join Date
    10-11-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Parsing data in a range of cells with VBA/Macro to replace content

    Fantastic, many thanks once again... you've made my evening!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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