VBA Copy column based on header name

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
hello,
I have a weird thing happening with this code shown below.
Code:
'Find "Current Odometer Reading Date" in Row 1
  With Sheets(1).Rows(1)
   Set t = .Find("Current Odometer Reading Date", lookat:=xlPart)
'If not found, present a message
     If Not t Is Nothing Then
        Columns(t.Column).EntireColumn.Copy _
          Destination:=Sheets(2).Range("Y1")
       Else: MsgBox "Current Odometer Reading Date Not Found"
     End If
  End With
  
'Find "Current Odometer Reading" in Row 1
  With Sheets(1).Rows(1)
   Set t = .Find("Current Odometer Reading", lookat:=xlPart)
'If not found, present a message
     If Not t Is Nothing Then
        Columns(t.Column).EntireColumn.Copy _
          Destination:=Sheets(2).Range("Z1")
       Else: MsgBox "Current Odometer Reading Not Found"
     End If
  End With
This code is supposed to look in row 1 for a specific header name and then copy the entire column to another sheet. This code is working fine except for these 2 blocks. The first block of code copies the same column twice on the new sheet but it shouldn't because the header names are different. It copies "Current Odometer Reading Date" to columns Y and Z but it should only copy it to Y then the next block copy "Current Odometer Reading" to column Z. Y and Z are identical after I run this code.

Can anyone tell me what I'm missing here? I have 27 columns I use this code on and they all work fine, its just these 2 that are the problem.
 
Last edited:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi. Try after changing
Set t = .Find("Current Odometer Reading", lookat:=xlPart)

to
Set t = .Find("Current Odometer Reading", lookat:=xlWhole)
 
Upvote 0
Hi. Try after changing
Set t = .Find("Current Odometer Reading", lookat:=xlPart)

to
Set t = .Find("Current Odometer Reading", lookat:=xlWhole)

Osvaldo Palmeiro,
Thanks!! That was the problem. Apparently because they very similar and it was set on Part, it didn't grab the right column. It works now, thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top