Luis Diaz
asked on
Excel VBA: Add specific value on multiple columns
Hello experts,
I have the following procedure reported at: https://www.experts-exchan ge.com/que stions/291 55390
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
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.
I have the following procedure reported at: https://www.experts-exchan
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
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.
ASKER
Thank you Subodh. Yes, as soon as I can I will add sample file.
ASKER
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
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?
ASKER
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.
First Inputbox is not required.
I would like to keep just Inputbox 2 to enter values and 3 for columns.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Subodh. I will test it and keep you informed.
Thank you for your help.
Thank you for your help.
ASKER
Hello Subodh,
I tested and it works!
Thank you for your help.
I tested and it works!
Thank you for your help.
Great! You're welcome!
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?