Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

Excel VBA: Add specific value on multiple columns

Hello experts,

I have the following procedure reported at: https://www.experts-exchange.com/questions/29155390
I would like to take as a reference in order to cover the following need:
1.      Be able to report specific value in inputbox 1 and report this value in columns involved by columns reported in inputbox 2


 
 Sub Add_Specific_String_Multiple_Columns()

    Dim strCol As Variant
    Dim strColList As String
    Dim lngLastRow As Long, lngRow As Long
    Dim intWhich As Integer
    Dim intWhich_temp As String

    intWhich = 0
    intWhich = Application.InputBox("Please report value related to the action that you want to perform: 1 for adding string at the beginning 2 at the end", Type:=1)

    If intWhich = 0 Then
        MsgBox ("No input!")
        Exit Sub
    ElseIf intWhich > 2 Then
        MsgBox "Please enter '1' or '2' only."
        Exit Sub
    End If

    strColList = InputBox("Please report column letter(s) following by ; in which you want to apply procedure," _
                          & ": A for single column A;C;D for multiple columns", "Choose Column Letter(s)")
    If strColList = vbNullString Then
        MsgBox ("No input!")
        Exit Sub
    End If
    
    For Each strCol In Split(strColList, ";")
        If Not ValidCellReference(strCol) Then
            MsgBox strCol & " is not a valid column letter.", vbExclamation
            Exit Sub
        End If
    Next strCol
    
    For Each strCol In Split(strColList, ";")
        lngLastRow = Range(strCol & Rows.Count).End(xlUp).Row

        For lngRow = 2 To lngLastRow
            Select Case intWhich
            Case 1
                Cells(lngRow, strCol).Value = lngRow - 1 & Cells(lngRow, strCol).Value
            Case 2
                Cells(lngRow, strCol).Value = Cells(lngRow, strCol).Value & lngRow - 1
            End Select
        Next
    Next
End Sub

Function ValidCellReference(ByVal str As String) As Boolean
Dim rng As Range
On Error Resume Next
Set rng = Cells(1, str)
On Error GoTo 0
If Not rng Is Nothing Then ValidCellReference = True
End Function

Open in new window


Example:
I put toto in inpubtbox 1 and A;C;D in inputobox 2 all the values for range A, range C, range D should be equal to toto.

If you have questions, please contact me.
Thank you very much for your help.
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

The existing code seems to be adding a sequence number either in the beginning or in the end of cell values depending on user input in the first inputbox. Do you want the first inputbox to ask the user to input the sub string which would replace the cell content of reported columns in inputbox 2?

Why not upload a small sample file with the desired output mocked up manually on another sheet along with the explanation that what would you enter in each inputbox in order to get the desired output?
Avatar of Luis Diaz

ASKER

Thank you Subodh. Yes, as soon as I can I will add sample file.
Hello Subodh,

I attached sample file.
Input sheet with source data and Output sheet with expected result.

Thank you for your help.
Add-values-on-columns.xlsx
what would you enter in each inputbox in order to get the desired output?
You haven't replied to the above query yet.

Do you still want the first inputbox as it is? i.e. asking user to input either 1 or 2?
Or you want first inputbox to ask the user to input the string which would replace the reported columns values e.g. "Toto" in the first inputbox?
I will enter Toto in second Inputbox to get desired result.
First Inputbox is not required.
I would like to keep just Inputbox 2 to enter values and 3 for columns.
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Subodh. I will test it and keep you informed.
Thank you for your help.
Hello Subodh,
I tested and it works!
Thank you for your help.
Great! You're welcome!