Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default macro to copy columns

Hello,

I need a macro that copies columns to a specific sheet. The steps that
have to be taken:
1. the macro is called
2. a form asks the user for a number. This number is the amount of
columns counting from left to right that have to be copied, starting
with colomn K. (So, if the number is 3, column K, L and M have to be
copied)
3. The copied columns have to be pasted on another worksheet. Also
starting from column K.
Notice: the cells that will be copied contain formulas, so paste
special+values should be used.

Can someone realize such a macro?

Many thanks!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default macro to copy columns

bernd,

assuming the target sheet is Sheet2, maybe something like this:

Sub test()

Dim iColumns As Integer

iColumns = InputBox("Enter how many columns to copy")
Range(Cells(1, 11), Cells(1, 10 + iColumns)).EntireColumn.Copy
Sheet2.Range("K1").PasteSpecial xlPasteValues

End Sub


--
Hope that helps.

Vergel Adriano


"bernd" wrote:

Hello,

I need a macro that copies columns to a specific sheet. The steps that
have to be taken:
1. the macro is called
2. a form asks the user for a number. This number is the amount of
columns counting from left to right that have to be copied, starting
with colomn K. (So, if the number is 3, column K, L and M have to be
copied)
3. The copied columns have to be pasted on another worksheet. Also
starting from column K.
Notice: the cells that will be copied contain formulas, so paste
special+values should be used.

Can someone realize such a macro?

Many thanks!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default macro to copy columns

bernd,

assuming the target sheet is Sheet2, maybe something like this:

Sub test()

Dim iColumns As Integer

iColumns = InputBox("Enter how many columns to copy")
Range(Cells(1, 11), Cells(1, 10 + iColumns)).EntireColumn.Copy
Sheet2.Range("K1").PasteSpecial xlPasteValues

End Sub


--
Hope that helps.

Vergel Adriano


"bernd" wrote:

Hello,

I need a macro that copies columns to a specific sheet. The steps that
have to be taken:
1. the macro is called
2. a form asks the user for a number. This number is the amount of
columns counting from left to right that have to be copied, starting
with colomn K. (So, if the number is 3, column K, L and M have to be
copied)
3. The copied columns have to be pasted on another worksheet. Also
starting from column K.
Notice: the cells that will be copied contain formulas, so paste
special+values should be used.

Can someone realize such a macro?

Many thanks!


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default macro to copy columns

One way:

Public Sub CopyVariableNumberOfColumns()
Const cnCOLSTART As Long = 11 'Col K
Dim vResult As Variant
Dim rCopy As Range

With Sheets("Sheet1")
Do
vResult = Application.InputBox( _
Prompt:="Number of columns to copy:", _
Title:="Copy Columns", _
Type:=1, _
Default:=1)
If vResult = False Then Exit Sub 'user cancelled
Loop Until vResult 0 And _
((vResult + cnCOLSTART - 1) <= .Columns.Count)
Set rCopy = Intersect(.UsedRange, _
.Columns(cnCOLSTART).Resize(, vResult))
With rCopy
Worksheets("Sheet2").Cells(1, 1).Resize( _
.Rows.Count, .Columns.Count).Value = .Value
End With
End With
End Sub


In article .com,
bernd wrote:

Hello,

I need a macro that copies columns to a specific sheet. The steps that
have to be taken:
1. the macro is called
2. a form asks the user for a number. This number is the amount of
columns counting from left to right that have to be copied, starting
with colomn K. (So, if the number is 3, column K, L and M have to be
copied)
3. The copied columns have to be pasted on another worksheet. Also
starting from column K.
Notice: the cells that will be copied contain formulas, so paste
special+values should be used.

Can someone realize such a macro?

Many thanks!

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default macro to copy columns

Sub ABC()
Dim ans as String
ans = InputBox("How many columns")
if not isnumeric(ans) then exit sub
With worksheets("Sheet1")
.Range("K:K").Resize(rows.count, clng(ans)).copy
End with
Worksheets("Sheet2")
.Range("K:K").PasteSpecial xlValues
End With
End sub

--
Regards,
Tom Ogilvy


"bernd" wrote:

Hello,

I need a macro that copies columns to a specific sheet. The steps that
have to be taken:
1. the macro is called
2. a form asks the user for a number. This number is the amount of
columns counting from left to right that have to be copied, starting
with colomn K. (So, if the number is 3, column K, L and M have to be
copied)
3. The copied columns have to be pasted on another worksheet. Also
starting from column K.
Notice: the cells that will be copied contain formulas, so paste
special+values should be used.

Can someone realize such a macro?

Many thanks!




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default macro to copy columns

Finally I've used Mc Gimpsey's solution. After editing the macro to
copy to colomn K (=11) of sheet2.

In the other two solutions I ran into some errors.

Many thanks for your fast replies, to all of you!

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default macro to copy columns

Sub A_copyColumns()
Dim ans As String
ans = InputBox("How many columns")
If Not IsNumeric(ans) Then Exit Sub
With Worksheets("Sheet1")
.Range("K:K").Resize(Rows.Count, CLng(ans)).Copy
End With
With Worksheets("Sheet2")
.Range("K:K").PasteSpecial xlValues
End With
End Sub

worked fine for me. Looks like I left out a With statement. Sorry if the
typo caused you any discomfort.

--
Regards,
Tom Ogilvy


"bernd" wrote:

Finally I've used Mc Gimpsey's solution. After editing the macro to
copy to colomn K (=11) of sheet2.

In the other two solutions I ran into some errors.

Many thanks for your fast replies, to all of you!


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to copy columns values Mike_can Excel Programming 1 May 29th 06 04:24 AM
Macro to copy 2 columns to new spreadsheet each new day Joan[_3_] Excel Programming 1 December 19th 05 02:44 AM
Copy certain columns to another sheet using a macro Shane Nation Excel Programming 2 September 24th 05 05:36 PM
Copy non-Zero columns to new location macro Craigm Excel Programming 6 June 16th 05 07:37 PM
macro to copy columns to sheet Es Excel Discussion (Misc queries) 1 March 7th 05 02:03 PM


All times are GMT +1. The time now is 10:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"