column count

Dummy Excel

Well-known Member
Joined
Sep 21, 2005
Messages
1,004
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
Hi,
I am trying to create a simple macro where it copies columns from one worksheet to another, although the VB code needs to be dynamic as the number of columns changes on a weekly basis.

So basically I need the macro to determine how many columns there are, then copy each column into another workbook...why create a macro to do this instead of using range("A1").currentregion.copy I hear you say well basically when I paste it into the other worksheet I need to paste them in a specific place which I will put the VB code together.

can anyone help to start me off??

cheers
Sam
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

bertie

Well-known Member
Joined
Jun 12, 2009
Messages
1,869
Use the Cells() function to get the number of rows and columns;

Rich (BB code):
         'get the column count
         With wsSource
            'using row one to count the number of columns
            colEnd = .Cells(1, .Columns.Count).End(xlToLeft).Column
            
            'using column one to count the number of rows
            rowEnd = .Cells(.Rows.Count, 1).End(xlUp).Row

Then use the variables to copy and paste the range/
NB adjust for copying individual columns.
Rich (BB code):
            '
            'copy and paste
            .Range(.Cells(1, 1), .Cells(rowEnd, colEnd)).Copy _
               Destination:=wsTarget.Range("A" & rowStart)
 
Upvote 0

L. Howard

Well-known Member
Joined
Oct 16, 2012
Messages
4,514
Give this a try, where you run the code, enter a column letter of the column want to copy, and enter a column letter for the column you want to paste to in the other workbook

Regards,
Howard

Rich (BB code):
Option Explicit
Sub CopyBookToBook2()
' With help by Claus

' The workbook "Copy To WKBook.xlsm" must be a saved workbook.  Name your own and save it.

Dim ColRngFrm As String
Dim ColRngTo As String
Dim LRow As Long

LRow = Cells(Rows.count, 1).End(xlUp).Row

ColRngFrm = Application.InputBox(Prompt:="Enter a Column Letter to copy.", _
Title:="Column Letter From", Type:=2)
If ColRngFrm = "" Or ColRngFrm = "False" Then Exit Sub

ColRngTo = Application.InputBox(Prompt:="Enter a Column Letter to paste to.", _
Title:="Column Letter To", Type:=2)
If ColRngTo = "" Or ColRngTo = "False" Then Exit Sub

Range(Cells(1, ColRngFrm), Cells(LRow, ColRngFrm)).Copy _
Workbooks("Copy To WKBook").Sheets("Sheet1").Cells(1, ColRngTo)
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,195,746
Messages
6,011,412
Members
441,613
Latest member
worksux

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
Top