troubleshooting Question

Excel VBA copy paste columns using public variables as column names

Avatar of prodempsey
prodempseyFlag for United States of America asked on
VBAMicrosoft Excel
13 Comments1 Solution657 ViewsLast Modified:
I'm working on a VBA project in Excel, and I'm trying to copy columns from one sheet and paste them to another sheet.  I'm using a public variable from a userform with a listbox to ask the user for the name of the column in sheet 1 which correlates with the name of the column in sheet 2.  I have to use a listbox because the columns names in sheet 1 can vary slightly depending on the data the user imports.

The public variable is declared at the top of my module using:

Public PartsDeptLBValue As String

I want to copy the data from column name seleted from a sheet named "ImportData" with the data starting on row 5 to a sheet name "PAD2", Column("Parts Description") which starts on row 2.

I've got the userform working correctly, and it's storing the value the user selects in a public variable on the module called PartsDeptLBValue thanks to another expert.  

EDIT:  I've also tried this code, which I think get's me closer, but I'm getting an error that say's "The information cannot be pasted because the Copy area and the paste area are not the same size and shape."

Sub CopyPasteColumns()
Dim t As Range


'Find PartsDeptLBValue in Row 4
  With Sheets("ImportData").Rows(4)
   Set t = .Find(PartsDeptLBValue, lookat:=xlPart)
'If found, copy the column to PAD2, Column A
'If not found, present a message
     If Not t Is Nothing Then
        Columns(t.Column).EntireColumn.Copy _
       Else: MsgBox "Title Not Found"
     End If
  End With

End Sub

Open in new window

Here's the other code I tried, but I'm getting "Unable to get the Match property of the WorksheetFunction class" error:
Sub CopyPasteColumns()

Dim desc As String


    desc = WorksheetFunction.Match( PartsDeptLBValue, Rows("1:1"), 0)

         Sheets("ImportData").Columns(desc).Copy Destination:=Sheets("PAD2").Range("A2")

End Sub

Open in new window

I've uploaded the Excel file.  Thanks in advance for your help!
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 13 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 13 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004