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 v2

Hello experts,

I have the procedure reported at: https://www.experts-exchange.com/questions/29155959/Excel-VBA-Add-specific-value-on-multiple-columns.html

Sub Add_Specific_String_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
        End If
    Next strCol
    
    For Each strCol In Split(strColList, ";")
        lngLastRow = Range(strCol & Rows.Count).End(xlUp).Row
        Range(strCol & "2:" & strCol & lngLastRow).Value = strToReplaceWith
    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


I retested and I am having a little issue:

Please find bellow my test:
1-I report titi value in first inputbox
2-I report a;b;c in second inputbox

Issue:
I have the various headers replaced by value reported in first inputbox.
I don't understand as I have the following line:
       Range(strCol & "2:" & strCol & lngLastRow).Value = strToReplaceWith

Open in new window

which states that replacewith starts as of row 2.

I attached dummy file with Input sheet before running the procedure and Result sheet after I execute the procedure as reported in my test above.

Thank you for your help.
Dummy-file.xlsm
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
Avatar of Luis Diaz

ASKER

Noted. Unable to test it right now.
I will keep you informed.
Hi Subodh,
I tested and the procedure works but I have a little issue, I don’t know if this can be reviewed.
Attached I made a test in Current sheet. I reported: 20190826_064245 for first inputbox and A;B;D in second inputbox.
I have no data for column A, 1 rows populated for B and 5 for D. I understand that the procedure do this as it should check last used range by column.
I was wondering if there is a way to check the highest last used range of one of the column concerned by reported in inputbox 2 and replace for all columns.
I also attached dummy file with Expected sheet with same previous test. I expect to have rows populated till row 5 for all columns reported in inputbox 2.
Let me know what do you think.
Dummy-file-add-values-on-columns.xlsx
ASKER CERTIFIED 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
Ok, Unable to test it right know.
I will keep you informed.
I tested last proposal and it works! Thank you very much for your help!
You're welcome!