+ Reply to Thread
Results 1 to 6 of 6

VBA to replace multiple values with specific labels

Hybrid View

  1. #1
    Registered User
    Join Date
    09-25-2014
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    2

    VBA to replace multiple values with specific labels

    Hi there, I was wondering if there's any VBA to replace multiple values in a column with specific labels?

    The issue I have is a column of data named "Age" with values like 24,25,26,27,37,44,45 etc. I manually categorized them into categories like "25-30" and "40&above" for the corresponding ages.

    However, as I'm dealing with a large number of entries, I was wondering if there is a VBA to automate the replacement process?

    E.g. creating a button that would convert new data where the ages are 25, 26, 27 to "25-30" and 44,46,49 to "40&above"

    Many thanks for any advice!

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: VBA to replace multiple values with specific labels

    Why VBA?
    I'd honestly recommend use of Vlookup function like the one in attached file.
    Attached Files Attached Files
    Best Regards,

    Kaper

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: VBA to replace multiple values with specific labels

    Of course you can do this with VBA but then we have too little information. for instance selec a range with such numbers and run for instance such macro:
    Sub test()
    Dim cell As Range
    For Each cell In Selection
     If IsNumeric(cell) Then
       cell = Application.WorksheetFunction.VLookup(cell, Sheets("Arkusz1").Range("I2:J7"), 2, True)
     End If
    Next cell
    End Sub
    try selecting B2:B4 in attachment from previous post and run it. of course after such macro formula in column C will return errors

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA to replace multiple values with specific labels

    I'd suggest first creating a 'lookup' table, which maps ages to your required groups:

    A
    B
    1
    Age
    Age Group
    2
    24
    20-24
    3
    25
    25-29
    4
    26
    25-29
    5
    27
    25-29
    6
    28
    25-29
    7
    29
    25-29
    8
    30
    30-34
    9
    31
    30-34
    10
    etc



    Then you can use a bit of code to replace age values with age group values:
    Sub foo()
        Dim c As Range, wsLookup As Worksheet, rngLookup As Range
        
        'change these values to match your lookup table
        Set wsLookup = Worksheets("Lookup Sheet")
        Set rngLookup = wsLookup.Range("A1:B100")
        
        For Each c In Selection 'change as necessary
            c.Value = rngLookup.Find(what:=c.Value, lookat:=xlWhole).Offset(0, 1).Value
        Next c
    End Sub
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to replace multiple values with specific labels

    If you "AGE" Column were Column A.

    Sub johnatas()
    Dim i As Long
    For i = Range("A" & Rows.count).End(3).Row To 2 Step -1
        Select Case Range("A" & i).Value
            Case Is > 40
                Range("A" & i).Value = "40 & above"
            Case Is > 24, Is < 31
                Range("A" & i).Value = "25-30"
        End Select
    Next i
    End Sub

  6. #6
    Registered User
    Join Date
    09-25-2014
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    2

    Re: VBA to replace multiple values with specific labels

    Sorry for the delayed reply but many thanks everyone, this worked wonders!

+ 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] Excel-13 Add values in array matching specific row labels, column labels within date range
    By fadeoutagain27 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 06-18-2014, 03:55 PM
  2. Replace a portion of Multiple cell labels/designators
    By rhartmanUSC in forum Excel General
    Replies: 8
    Last Post: 03-03-2014, 07:10 PM
  3. Replace specific values in a range with cell value in the same row
    By HughManatee in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-14-2013, 06:39 AM
  4. Macro to find all specific values in column and replace adjacent cell values
    By dblock02 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2013, 06:03 AM
  5. replace values between specific characters
    By hluk in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-01-2009, 05:39 PM

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