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

asked on

Excel VBA: copy columns from one sheet to another

Hello Experts,
I have the following procedure which allows me to loop on multiple columns and add default values.
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

Open in new window


I would like to take as a reference the loop on multiple columns part in order to cover the following requirement:
1.      Display inputbox: "Report columns that you want to copy at Output sheet: example: C;E;D
2.      Generate Ouput sheet with the the following name: Output & format(now,"yyyymmddmmss")
3.      Copy the columns reported in first inputbox in Output sheet.
I attached dummy file with Activesheet and Output sheet.
If you have questions, please contact me.
Dummy-file-copy-multiple-columns.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

Tested and it works, Thank you very much for your help.
You're welcome Luis!