finding heading to paste data in that column

B-Man

Board Regular
Joined
Dec 29, 2012
Messages
185
Office Version
  1. 2019
Platform
  1. Windows
I have a spreadsheet where I paste data into at last row+1 and all work fine at the moment....
I want to move columns around and tidy up the sheet but when I move a column I have to edit the vba to allow for it.
I'm thinking of doing something like this where the headings = the heading name it needs to find.

Am I on the right track or is there a much simpler option?


VBA Code:
Find headings for each column to paste data

    Dim rng As Range

    Dim col1 As Variant
    Dim col2 As Variant
    Dim col3 As Variant
    Dim col4 As Variant
    Dim col5 As Variant
    Dim col6 As Variant

    Dim Heading1 As String
    Dim Heading2 As String
    Dim Heading3 As String
    Dim Heading4 As String
    Dim Heading5 As String
    Dim Heading6 As String

    Heading1 =
    Heading2 =
    Heading3 =
    Heading4 =
    Heading5 =
    Heading6 =

    Set rng = wsDest.Range("A4:" & .Range("ZZ4").End(xlToRight).Address)
    col1 = Application.WorksheetFunction.Match(Heading1, rng, 0)
    col2 = Application.WorksheetFunction.Match(Heading2, rng, 0)
    col3 = Application.WorksheetFunction.Match(Heading3, rng, 0)
    col4 = Application.WorksheetFunction.Match(Heading4, rng, 0)
    col5 = Application.WorksheetFunction.Match(Heading5, rng, 0)
    col6 = Application.WorksheetFunction.Match(Heading6, rng, 0)
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Use Application.Match() function. It is just like worksheet MATCH function.
VBA Code:
Dim columnNumber As Long

columnNumber = Application.Match("Header Name", Range("A1:ZZ1"),0)
 
Upvote 0
Solution
ah much simpler I just copied code i found and modified it to use.
for some reason I thought match wouldn't work in the vba.
also hadn't thought of trying to simplify it any.

Cheers
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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