+ Reply to Thread
Results 1 to 5 of 5

Format Excel Worksheet With VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Format Excel Worksheet With VBA

    I have a workbook, and in column A it has a persons name (first and last), I need to insert a column before A, and in this column I want to add an sfID. Then for each name in column A that does not match the row above it, we increment the ID.



    What I need is

    1) Sort worksheet Column A ASC

    2) Add new Column (so Column A shifts to Column B)

    3) Iterate Column B and compare name to next row

    4) If names are same, key remains same, if different key increments



    Example

    sfID | Name

    sf_1 | Jason Smith

    sf_2 | Nolan Jones

    sf_2 | Nolan Jones

    sf_2 | Nolan Jones

    sf_3 | Mitch McConnel

    sf_4 | Jose Sandoval

    sf_4 | Jose Sandoval



    and we iterate over the entire sheet until no more rows in column B. What would be a VBA macro to accomplish this?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Format Excel Worksheet With VBA

    One way:

    Option Explicit
    
    Sub sReformat()
    
    Dim lLR As Long
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    
    Range("A1").EntireColumn.Insert
    
    Dim vArray, i As Long, j As Long
    vArray = Range("A1:B" & lLR)
    
    j = 0
    For i = LBound(vArray) + 1 To UBound(vArray)
        If vArray(i, 2) <> vArray(i - 1, 2) Then
            j = j + 1
        End If
        vArray(i, 1) = "sf_" & j
    Next 'i
        
    With Range("A1")
        .Resize(UBound(vArray), 2) = vArray
        .Value = "sfID"
    End With
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: Format Excel Worksheet With VBA

    TMS - thanks as always!

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Format Excel Worksheet With VBA

    Oops, missed the sort:

    Option Explicit
    
    Sub sReformat()
    
    Dim lLR As Long
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    With ActiveWorkbook.Worksheets("Sheet1")
        .Sort.SortFields.Clear
        .Sort.SortFields.Add2 Key:=Range("A2:A" & lLR) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With .Sort
            .SetRange Range("A2:A" & lLR)
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
    
    Range("A1").EntireColumn.Insert
    
    Dim vArray, i As Long, j As Long
    vArray = Range("A1:B" & lLR)
    
    j = 0
    For i = LBound(vArray) + 1 To UBound(vArray)
        If vArray(i, 2) <> vArray(i - 1, 2) Then
            j = j + 1
        End If
        vArray(i, 1) = "sf_" & j
    Next 'i
        
    With Range("A1")
        .Resize(UBound(vArray), 2) = vArray
        .Value = "sfID"
    End With
    
    Application.ScreenUpdating = True
    
    End Sub

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: Format Excel Worksheet With VBA

    You're welcome.

+ 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. Import excel 2010 format worksheet into database
    By dubbdan in forum Access Tables & Databases
    Replies: 1
    Last Post: 08-21-2013, 06:04 PM
  2. Date Format in userform textbox is different from excel worksheet?
    By z-eighty2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2013, 06:46 AM
  3. combining multiple different excel files with the same format in to 1 worksheet
    By rotterdam010 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2013, 08:31 AM
  4. Excel Macro Worksheet saved in wrong format
    By David_Marsh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-23-2011, 02:20 AM
  5. Excel 2003 Worksheet to Calendar format
    By Wan Fariz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-23-2011, 10:46 PM
  6. [SOLVED] Change format of all comments in an Excel worksheet
    By Eric Armstrong in forum Excel General
    Replies: 1
    Last Post: 02-23-2006, 08:35 PM
  7. Excel 2000 worksheet where I want to convert date format
    By Russell-stanely in forum Excel General
    Replies: 5
    Last Post: 12-18-2005, 10:10 PM
  8. How do I format the tabs of a worksheet in Excel?
    By s_e_morin in forum Excel General
    Replies: 1
    Last Post: 06-27-2005, 05:05 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