Consulting

Results 1 to 12 of 12

Thread: Solved: Adding pre-fixes

  1. #1
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location

    Solved: Adding pre-fixes

    Can anyone help with this? I thought I could insert columns and do concatenations, as easy as this was because I have coded following code too look at column A , I thought to automate it like this may be easier in the long run. Can anyone help? Many thanks in advance.

    [VBA]
    Sub AddPrefixes()
    Dim PreCol As Long 'or range?


    For Each PreCol In Sheets("Database")


    Select Case PreCol.Value
    Case "a", "b", "c"

    ActiveCell.Offset(1, 0).Select

    Select Case "a"
    ' Everything under this col heading, uptil next column heading (not including next column heading) should be prefixed with "Test 1 -"

    Select Case "b"
    ' Everything under this col heading, uptil next column heading (not including next column heading) should be prefixed with "Test 2 -"

    Select Case "c"
    ' Everything under this col heading, uptil next column heading (not including next column heading) should be prefixed with "Test 3 -"


    End Select

    Next PreCol
    End Sub [/VBA]

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    So you are looking at 3 different columns and want to add a prefix to everything in the columns except the header row?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    It’s all in one column, not separate columns. Different header names in one column with data underneath it until the next header name. Thanks

  4. #4
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    can anyone help with this?

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub Prefix()
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    Select Case Cells(i, 1)
    Case "a"
    pre = "Test 1 _ "
    Case "b"
    pre = "Test 2 _ "
    Case "c"
    pre = "Test 3 _ "
    Case Else
    Cells(i, 1) = pre & Cells(i, 1)
    End Select
    Next
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    this works perfectly, thank you, but just one little thing..
    I have another column heading under after the data under "c". Would I create a case "d" or use the Case Else. Can you explain what the code in Case Else is doing please. Mind not functioning.

  7. #7
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    Also what is (pre)cant see it being defined anywhere and yet it still works?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Careless of me!
    You can add for further headers as required. Case else is adding the selected prefix to cells which do not contain a header identified in the Select Case code.
    [vba]
    Sub Prefix()
    Dim i as long
    Dim Pre as string
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    Select Case Cells(i, 1)
    Case "a"
    pre = "Test 1 _ "
    Case "b"
    pre = "Test 2 _ "
    Case "c"
    pre = "Test 3 _ "
    Case "d"
    pre = "Test 4 _ "
    Case "e"
    pre = "Test 5_ "
    Case Else
    Cells(i, 1) = pre & Cells(i, 1)
    End Select
    Next
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    thank you!
    I did this for case "d", as i didnt want anything added for this header

    [VBA] Sub Prefix()

    Dim i As Long
    Dim Pre As String
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    Select Case Cells(i, 1)
    Case "a"
    Pre = "Test 1 _ "
    Case "b"
    Pre = "Test 2 _ "
    Case "c"
    Pre = "Test 3 _ "
    Case "d"
    Pre = ""

    Case Else
    Cells(i, 1) = Pre & Cells(i, 1)
    End Select
    Next
    End Sub [/VBA]

  10. #10
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    Actually this did not work - sorry. I do not want anything to be done to anything else only those three headings. After the third heading there is info in the same column. There is a fourth heading after this but we must not touch it or data underneath. I have tried different options and it is not working as required. –Thank you

  11. #11
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Try this:

    [vba]
    Sub Prefix()

    Dim i As Long
    Dim HeadersDone As Boolean
    HeadersDone = False
    Dim Pre As String

    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
    Select Case Cells(i, 1)
    Case "a"
    Pre = "Test 1 _ "
    Case "b"
    Pre = "Test 2 _ "
    Case "c"
    Pre = "Test 3 _ "
    Case "d"
    HeadersDone = True
    Case Else
    If Not HeadersDone Then
    Cells(i, 1) = Pre & Cells(i, 1)
    End IF
    [/vba]

  12. #12
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    this worked a charm, thanks both for your help. have a great day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •