Lesson#181: VBA To Add A Prefix to Selected Cells

Lesson#181: VBA To Add A Prefix to Selected Cells
  1. Open the VBA editor: In Excel, press Alt + F11 to open the VBA editor.
    • Create a new module: In the VBA editor, go to the Insert menu and select Module. This will create a new module where you can write your VBA code.

    Here is a VBA macro that you can use to add a prefix to the selected cells:

    Sub AddPrefixToSelectedCells()
        ' Prompt the user for the prefix to add
        Dim prefix As String
        prefix = InputBox("Enter the prefix to add:")
    
        ' Loop through the selected cells
        Dim cell As Range
        For Each cell In Selection
            ' Get the current cell value
            Dim currentValue As String
            currentValue = cell.Value
    
            ' Add the prefix to the cell value
            Dim newValue As String
            newValue = prefix & currentValue
    
            ' Update the cell value with the new value
            cell.Value = newValue
        Next cell
    End Sub
    

    To use this macro, select the cells to that you want to add the prefix and run the macro. It will prompt you to enter the prefix to add, and then it will add the prefix to the selected cells.

    See also  Lesson#173: Steps to make a Custom function in VBA

    Hi! I am Puspendu. I am the founder and author of Excelabcd. I am little creative person, blogger and Excel-maniac guy. I hope you enjoy my blog.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *