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

asked on

Excel VBA: add values based on multiple columns v2

Hello experts,

The following procedure allows me to add values on multiple filled columns:

Sub Add_Values_Multiple_Columns()

    Dim strCol As Variant
    Dim strColList As String
    Dim lngLastRow As Long, lngRow As Long
    Dim strToReplaceWith As String
    
    
    strToReplaceWith = InputBox("Please input the Value which should replace the values of the Reported Columns.")
    
    If strToReplaceWith = "" Then
        MsgBox "You didn't input any value.", vbExclamation
        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
        Else
            lngRow = Range(strCol & Rows.Count).End(xlUp).Row
            If lngRow > lngLastRow Then lngLastRow = lngRow
        End If
    Next strCol
    
    For Each strCol In Split(strColList, ";")
        If lngLastRow > 1 Then Range(strCol & "2:" & strCol & lngLastRow).Value = strToReplaceWith
    Next

    Exit Sub

Error_Routine:
    MsgBox Err.Description, vbExclamation, "Something went wrong!"
    
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



I would like to take as a reference in order to cover the following need:
-Be able to repeat the values multiple time on selected column:

Example: If I report test for value to add column A and B for involved columns
I expect to have and inputbox with the following information: “How many times do you want to repeat the value to add?” If I report 4 I would have test test test test

I attached dummy file.

If you have questions, please contact me.

Thank you for your help.
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

You didn't upload the workbook.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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
SOLUTION
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
Avatar of Luis Diaz

ASKER

Thank you very much. I will test the proposals and keep you informed.
I tested the proposals and they work.
I was wondering if the following adjustment can be done:
-If the column(s) reported in the inputbox 3 don't have any value as of row 2 exit sub with the following message:
"Unable to proceed as column(s) involved don't have values".
Thank you for your help.
SOLUTION
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
SOLUTION
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 very much!
I tested the procedures and both works!
@Bill: just for the record, I modified line 49 of your proposal to make it work:
MsgBox "Unable to proceed as column(s) involved don't have values."

Regards,
Luis.
Avatar of Bill Prew
Bill Prew

Whoops, I had just copied from your earlier comment to get the text, missed the period placement...


»bp
No problem Bill!