Copy entire columns to separate worksheets

exceldb

New Member
Joined
Apr 19, 2019
Messages
21
Hi Everyone,

I am a newbie to this forum, though not necessarily new to the rules.
I have a very simple task to complete, yet no matter how many youtube videos I watched and how many different forums and pages I have read, none of them are this simple.

I did see a function called entirecolumncopy in one vba script, so I am hoping it can be done.

I basically have a master spreadsheet and I want to be able to copy the first column and subsequent columns to separate worksheets.
Now, I do not want to create the headers first, just copy something based on a location or department, I want a full copy of columns.

The end result being worksheets that contain the following

Sheet1 = Column A + Column B
Sheet2 = Column A + Column C
Sheet3 = Column A + Column D
.....
Column A of course being the unique identifier, in another words I want the sheets to act like a relational database where the primary key is always following the data.

Also, after reading a few threads, I want to add different items I see people ask for when trying to help.

1. The original name of the master spreadsheet should not matter
2. The vba should create as many sheets as there are many columns - so if 25 columns, then 25 sheets and if tomorrow I add a 26th, it will do that.
3. The names of the sheets should simply be the header for the second column ( In other words, if the header for column B in Sheet2 was location, the sheet should be "location"
4. The master spreadsheet should not be touched as it could be used for vlookups and other functions.

Last but not least, does anyone recommend a really good book for excel and vba - from beginner to advanced. There are a ton and I figured I would ask here.

Thank you in advance for all your help!
Tony
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi & welcome to MrExcel
How about
Code:
Sub exceldb()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   For i = 2 To UBound(Ary, 2)
      Sheets.Add(, Sheets(Sheets.Count)).Name = Ary(1, i)
      Range("A1").Resize(UBound(Ary), 2).Value = Application.Index(Ary, Evaluate("row(1:" & UBound(Ary) & ")"), Array(1, i))
   Next i
End Sub
 
Upvote 0
Now just to be curious, what if I would like to transfer the first one and only some columns - Would there be a way to specify which columns to copy along with column A?
thanks!
 
Upvote 0
Yes :)
Will it always be the same columns, or do you need to specify them each time?
 
Upvote 0
Yes :)
Will it always be the same columns, or do you need to specify them each time?

Great question. I should have thought of that when posting but now that I think about it.
Though now that I am thinking about it. :eeek::eeek::eeek:

1. One scenario would be hard coded to the same columns by column number much like vlookup works ( so column 2, 3 & 5 & 7) along with Column A.
2. Another would be hard coded by header, copying the entire column along with Column A
3. If you really wanted to get fancy, search through the column, find a special character, if found, go ahead and copy that entire column along with Column A.
 
Upvote 0
Using column numbers
Code:
Sub exceldb()
   Dim Ary As Variant, ColAry As Variant
   Dim i As Long
   
   ColAry = Array(Array(1, 3, 5, 7), Array(1, 2, 4, 5, 6), Array(1, 8, 9, 10))
   Ary = Range("A1").CurrentRegion.Value2
   For i = 2 To UBound(ColAry(UBound(ColAry))) + 1
      Sheets.Add(, Sheets(Sheets.Count)).Name = Ary(1, i)
      Range("A1").Resize(UBound(Ary), UBound(ColAry(i - 2)) + 1).Value = Application.Index(Ary, Evaluate("row(1:" & UBound(Ary) & ")"), Array(ColAry(i - 2)))
   Next i
End Sub
 
Upvote 0
Hi Fluff,

This latest solution copied the first array (1,3,5,7), named the sheets by the header of the column, but it did not copy over any data.
 
Upvote 0
Not sure why, as it works for me.
Are the new sheets completely blank?
 
Upvote 0
Yes, empty.
For learning purposes, could you add comments on each line so I know the expected behavior or is that too much to ask?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
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