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

asked on

Excel VBA: delete values on multiple columns

Hello experts,
I have the following procedure:
Sub Add_Values_Multiple_Columns()
    Dim wsActive    As Worksheet
    Dim wsOutput    As Worksheet
    Dim strCol      As Variant
    Dim strColList  As String
    Dim lngLastRow  As Long
    Dim lngLastCol  As Long
    Dim shName      As String
    
    Application.ScreenUpdating = False
    
    Set wsActive = ActiveSheet
    
    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
    
    Set wsOutput = Worksheets.Add(after:=Worksheets(Worksheets.Count))
        
    For Each strCol In Split(strColList, ";")
        lngLastRow = Range(strCol & Rows.Count).End(xlUp).Row
        If wsOutput.Range("A1") = "" Then
            lngLastCol = 1
        Else
            lngLastCol = wsOutput.Cells(1, Columns.Count).End(xlToLeft).Column + 1
        End If
        lngLastRow = wsActive.Range(strCol & Rows.Count).End(xlUp).Row
        wsActive.Range(strCol & "1:" & strCol & lngLastRow).Copy wsOutput.Cells(1, lngLastCol)
    Next
    
    shName = "Output_" & Format(Now, "yyyymmddmmss")
    wsOutput.Name = shName
    Application.ScreenUpdating = True
    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

Which allows me to add values on multiple columns.
I would like to take it as a reference to cover the following need:
1-Inputbox report the columns in which you want to delete values.
2-Delete values (Value="") on columns reported as of row 2.
I attached dummy file with input and expected result in output sheet when I report column C and E on Inputbox.
If you have questions, please contact me.
Dummy-file-delete-values.xlsx
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
Avatar of Luis Diaz

ASKER

Thank you Subodh.
Unable to test it right know.
I will test it as soon as I can and I will let you know.
Hello Subodh,
I tested the proposal and it works!
Thank you for your help.
You're welcome Luis!