finding heading to paste data in that column

B-Man

Board Regular
Joined
Dec 29, 2012
Messages
183
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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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,215,426
Messages
6,124,829
Members
449,190
Latest member
rscraig11

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