Consulting

Results 1 to 2 of 2

Thread: VBA code help - concatenation

  1. #1
    VBAX Newbie
    Joined
    Aug 2019
    Posts
    1
    Location

    VBA code help - concatenation

    I'm new to VBA, and not sure where to start on this...

    I need a code that will look at the month of the system date and take ONLY 9 numbers from right of invoice # (column K) and concatenate it with a letter based on what month it is. For example:

    Invoice 0123456789
    (For August, letter "H" is concatenated, for September it is "I", and so on).
    After macro: 123456789H

    I would like this to continue down the entire column until it reaches the end

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Couple of ways

    1. WS formula

    =RIGHT(K2,9)&CHAR(MONTH(NOW())+64)

    2. User Defined Function use multiple times on WS ...,
    FormatInvoice


    3. Macro to do all of Col K


    Option Explicit
    'Invoice 123456789
    '(For August, letter "H" is concatenated, for September it is "I", and so on).
    'After macro: 123456789H
    
    Function FormatInvoice(s As String) As String
        FormatInvoice = Right(s, 9) & Chr(Month(Now) + 64)
    End Function
    
    Sub FormatAllInvoices()
        Dim r As Range, r1 As Range
        
        With ActiveSheet
            Set r = Range(.Cells(2, 11), .Cells(2, 11).End(xlDown))
        End With
        For Each r1 In r.Cells
            r1.Offset(0, 1).Value = FormatInvoice(r1.Value)
        Next
    End Sub
    
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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