VBA code for Extracting Data but order of columns changes all the

  • Thread starter Thread starter Confused
  • Start date Start date
C

Confused

I have a standard template with fields (worksheet 1) that I need to populate
data with from another worksheet (worksheet 2). The problem is in worksheet
2 the order of the columns will always be different or sometimes the column
name may not exist. Is there a vba code that will compare the column
headings of worksheet 1 and 2 and if the heading exists in worksheet 2, then
that matching column in worksheet 1 will be populated? There are
approximately 30 columns in worksheet 1 to populate.

any help is much appreciated!!
Thanks.
 
I prefer to do this by defining a named range in both workbooks that match
and then no matter which column it's in, you can find the data.

myCol = 0
myCol = myWS.Range("Column1Name").Column

would be how you'd access it.

You could do something like

IF myCol > 0 then ...
Populate the second worksheet
end if
 
By defining named ranges in both worksheets, you mean named ranges for each
matching columns?

Can you further explain the what you mean by the below:

IF myCol > 0 then ...
Populate the second worksheet
end if

What would I put in after the word "then" in the first line?
And do I need to put anything after "end if" or will the code end on that
command?

Thanks for your help!
 
Will this work even if I copy and paste a new file into the worksheet,
therefore the order of the columns will change?

Please let me know if I am not being clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top