Copy selected column in range to another sheet

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010
Hello,

I need VBA which can have column with range selection via input box and then copy columns in to another sheet

I have sheet (data) from this sheet I want to copy (3 columns) column C from range C5:C45, column F from range F5:F45, column J from range J5:J45 in to sheet (copy columns).

For example VBA give an option to select column if I select C5, F5 and J5, it should copy 3 columns with range from 5 to 45 as shown in the example below.

data sheet...
Copy Columns.xlsm
ABCDEFGHIJKLM
1
2
3
4
5S.NGroupn1n2n3n4n5n6n7n8n9n10
61127303229382636111344
722442382926433174739
833449356173334382933
94446482728384728112944
1051369343638161231424
1162344775101127401022
127343405030382461428
138426318442265141731
149131226818132313746
1510238232619483153014
161139442816333345436
17124324111311473493933
1813113351117393018241227
191422463426264519445014
20153233094930307191644
2116422353820272440204746
2217116504637214511405017
23182514825504023423429
2419316333629204620443325
252042423133248151424618
2621119464916374133844
272222622333431422464
282333421172944237491013
29244355144103044122942
30251275043281383351240
3126220193238434017243645
32273454244321819293314
332844345243874445421720
3429145101350364534293241
35302385234950222512238
3631322361725121027394046
37324
38331
39342
40353
41364
42371
43382
44393
45404
Data


Copy Here.....
Copy Columns.xlsm
ABCDEFGHIJKLM
1
2
3
4
5S.NGroupn1n4n8
611272911
7224297
83344638
944462811
1051363631
116234540
127343301
1384264414
14913831
1510238115
1611391645
17124323149
18131131724
1914222644
20153234919
21164222020
22171163740
2318252542
24193162944
2520424322
2621119913
2722226344
28233342949
2924435412
3025127285
31262203824
3227345429
33284433842
34291455029
35302384912
36313222539
37324
38331
39342
40353
41364
42371
43382
44393
45404
Copy Column


Regards,
Moti
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Using Excel 2010
Hello,

I need VBA which can have column with range selection via input box and then copy columns in to another sheet

I have sheet (data) from this sheet I want to copy (3 columns) column C from range C5:C45, column F from range F5:F45, column J from range J5:J45 in to sheet (copy columns).

For example VBA give an option to select column if I select C5, F5 and J5, it should copy 3 columns with range from 5 to 45 as shown in the example below.

data sheet...
Copy Columns.xlsm
ABCDEFGHIJKLM
1
2
3
4
5S.NGroupn1n2n3n4n5n6n7n8n9n10
61127303229382636111344
722442382926433174739
833449356173334382933
94446482728384728112944
1051369343638161231424
1162344775101127401022
127343405030382461428
138426318442265141731
149131226818132313746
1510238232619483153014
161139442816333345436
17124324111311473493933
1813113351117393018241227
191422463426264519445014
20153233094930307191644
2116422353820272440204746
2217116504637214511405017
23182514825504023423429
2419316333629204620443325
252042423133248151424618
2621119464916374133844
272222622333431422464
282333421172944237491013
29244355144103044122942
30251275043281383351240
3126220193238434017243645
32273454244321819293314
332844345243874445421720
3429145101350364534293241
35302385234950222512238
3631322361725121027394046
37324
38331
39342
40353
41364
42371
43382
44393
45404
Data


Copy Here.....
Copy Columns.xlsm
ABCDEFGHIJKLM
1
2
3
4
5S.NGroupn1n4n8
611272911
7224297
83344638
944462811
1051363631
116234540
127343301
1384264414
14913831
1510238115
1611391645
17124323149
18131131724
1914222644
20153234919
21164222020
22171163740
2318252542
24193162944
2520424322
2621119913
2722226344
28233342949
2924435412
3025127285
31262203824
3227345429
33284433842
34291455029
35302384912
36313222539
37324
38331
39342
40353
41364
42371
43382
44393
45404
Copy Column


Regards,
Moti
Try this.

You will be prompted to select just the cells in the headers of the columns that you want to copy. e,g, A5:B5, D5,G5

You can select non-contiguous cells by holding down the Ctrl key.

You will then be prompted to select the destination cell that can be on a diffrent sheet.

Test it on a copy of your data.

VBA Code:
Public Sub subCopyColumns()
Dim rng As Range
Dim r As Range
Dim t As String
Dim rngToCopy As Range
Dim rngDestination As Range

  ActiveWorkbook.Save
    
  On Error Resume Next
    
  Set rng = Application.InputBox(Prompt:="Select headings of columns to copy.", Type:=8)
  
  On Error GoTo 0
  
  If rng Is Nothing Then
    Exit Sub
  End If
    
  For Each r In rng.Cells
    If Not rngToCopy Is Nothing Then
      Set rngToCopy = Union(rngToCopy, rng.CurrentRegion.Columns(r.Column))
    Else
      Set rngToCopy = rng.CurrentRegion.Columns(r.Column)
    End If
  Next r
  
  On Error Resume Next
  
  Set rngDestination = Application.InputBox(Prompt:="Select destination cell.", Type:=8)
   
  On Error GoTo 0
  
  If Not rngDestination Is Nothing Then
  
    Worksheets(rngDestination.Parent.Name).Activate
  
    For Each rng In rngToCopy.Areas
            
      rng.Copy rngDestination
    
      Set rngDestination = rngDestination.Offset(, rng.Columns.Count)
    
    Next rng
  
  End If
  
  ActiveWorkbook.Save
  
End Sub
 
Upvote 1
Try this.

You will be prompted to select just the cells in the headers of the columns that you want to copy. e,g, A5:B5, D5,G5

You can select non-contiguous cells by holding down the Ctrl key.

You will then be prompted to select the destination cell that can be on a diffrent sheet.

Test it on a copy of your data.
Hello HighAndWilder, thank you for the help.

1st-VBA is copying selection of columns in the (CurrentRegion) from row 1 to bottom entire column, does it is possible to set range row 5 to below row 45 only

2nd-instead of selecting can be paste always in the destination sheet from C5 to right and below?

Kind regards,
Moti
 
Upvote 0
Hello HighAndWilder, thank you for the help.

1st-VBA is copying selection of columns in the (CurrentRegion) from row 1 to bottom entire column, does it is possible to set range row 5 to below row 45 only

2nd-instead of selecting can be paste always in the destination sheet from C5 to right and below?

Kind regards,
Moti
Anything is possible.

Regarding :
1st-VBA is copying selection of columns in the (CurrentRegion) from row 1 to bottom entire column, does it is possible to set range row 5 to below row 45 only

So just rows 5 to 45 inclusive then?

Regarding :
2nd-instead of selecting can be paste always in the destination sheet from C5 to right and below?

That is possible but will the destination sheet always be 'copy columns'?

What is to be done with any existing data in the destination sheet?
 
Upvote 0
Anything is possible.
Hello HighAndWilder, that is great thank for the question.
So just rows 5 to 45 inclusive then?
Yes it is correct 5 to 45 inclusive
Regarding :
2nd-instead of selecting can be paste always in the destination sheet from C5 to right and below?

That is possible but will the destination sheet always be 'copy columns'?
Yes the destination sheet always be 'copy columns' this is correct
What is to be done with any existing data in the destination sheet?
They will remain as it is in the destination sheet.

Kind regards,
Moti
 
Upvote 0
Hello HighAndWilder, that is great thank for the question.

Yes it is correct 5 to 45 inclusive

Yes the destination sheet always be 'copy columns' this is correct

They will remain as it is in the destination sheet.

Kind regards,
Moti
VBA Code:
Public Sub subCopyColumns()
Dim rng As Range
Dim r As Range
Dim t As String
Dim rngToCopy As Range
Dim rngDestination As Range

  ActiveWorkbook.Save
    
  On Error Resume Next
    
  Set rng = Application.InputBox(Prompt:="Select headings of columns to copy.", Type:=8)
  
  On Error GoTo 0
  
  If rng Is Nothing Then
    Exit Sub
  End If
    
  For Each r In rng.Cells
    If Not rngToCopy Is Nothing Then
      Set rngToCopy = Union(rngToCopy, rng.CurrentRegion.Columns(r.Column).Resize(41, r.Columns.Count))
    Else
      Set rngToCopy = rng.CurrentRegion.Columns(r.Column).Resize(41, r.Columns.Count)
    End If
  Next r
  
  Worksheets("copy columns").Activate
    
  Set rngDestination = Worksheets("copy columns").Range("C5")
  
  For Each rng In rngToCopy.Areas
            
    rng.Copy rngDestination
    
    Set rngDestination = rngDestination.Offset(, rng.Columns.Count)
    
  Next rng
  
  ActiveWorkbook.Save
  
End Sub
 
Upvote 0
VBA Code:
Public Sub subCopyColumns()
Hello HighAndWilder, it is almost perfect. Only if I fill in the data sheet row A1:L4 with some text or numbers and run the code pop up input box where I select example C5, G5 and P5 (it must select the column from rows 5 to 45 inclusive)

But it is copying to destination sheet in cell C5 correctly (but coping row from 1 to 45 of the data sheet) which must be also 5 to 45…please can you take a look sorry to bother you.

Kind regards,
Moti
 
Upvote 0
Hello HighAndWilder, it is almost perfect. Only if I fill in the data sheet row A1:L4 with some text or numbers and run the code pop up input box where I select example C5, G5 and P5 (it must select the column from rows 5 to 45 inclusive)

But it is copying to destination sheet in cell C5 correctly (but coping row from 1 to 45 of the data sheet) which must be also 5 to 45…please can you take a look sorry to bother you.

Kind regards,
Moti
Can you post a copy of the data in 'copy columns' after the copy using XL2BB. Thanks.
 
Upvote 0
Can you post a copy of the data in 'copy columns' after the copy using XL2BB. Thanks.
Hello HighAndWilder, here is the new data sheet layout and copy results in the sheet copy columns (you will see it is coping data from row1) please check and also if it is not much please can it is possible to paste values only. Thank you

Data sheet....
Copy Columns.xlsm
ABCDEFGHIJKLM
1S.NGroupn1n2n3n4n5n6n7n8n9n10
2S.NGroupn1n2n3n4n5n6n7n8n9n10
3S.NGroupn1n2n3n4n5n6n7n8n9n10
4S.NGroupn1n2n3n4n5n6n7n8n9n10
5S.NGroupn1n2n3n4n5n6n7n8n9n10
61127303229382636111344
722442382926433174739
833449356173334382933
94446482728384728112944
1051369343638161231424
1162344775101127401022
127343405030382461428
138426318442265141731
149131226818132313746
1510238232619483153014
161139442816333345436
17124324111311473493933
1813113351117393018241227
191422463426264519445014
20153233094930307191644
2116422353820272440204746
2217116504637214511405017
23182514825504023423429
2419316333629204620443325
252042423133248151424618
2621119464916374133844
272222622333431422464
282333421172944237491013
29244355144103044122942
30251275043281383351240
3126220193238434017243645
32273454244321819293314
332844345243874445421720
3429145101350364534293241
35302385234950222512238
3631322361725121027394046
37324
38331
39342
40353
41364
42371
43382
44393
45404
Data


Copy here....
Copy Columns.xlsm
ABCDEFGHIJKLM
1
2
3
4
5S.NGroupn1n3n5n8
611n1n3n5n8
722n1n3n5n8
833n1n3n5n8
944n1n3n5n8
105127323811
1162438267
127344351738
138446273811
149136343831
151023471040
161134350381
17124268214
181313261831
191423826915
20153928345
211643211149
2217113113924
231822342644
241932393019
2520422382720
2621116462140
272225485042
2823316362044
292442413482
302511941613
312622633314
3227334174449
3328435141012
34291274315
3530220324324
363134543229
373244324742
3833145133629
3934238235012
4035322171239
41364
42371
43382
44393
45404
copy columns


Kind regards,
Moti
 
Upvote 0
Hello HighAndWilder, here is the new data sheet layout and copy results in the sheet copy columns (you will see it is coping data from row1) please check and also if it is not much please can it is possible to paste values only. Thank you

Data sheet....
Copy Columns.xlsm
ABCDEFGHIJKLM
1S.NGroupn1n2n3n4n5n6n7n8n9n10
2S.NGroupn1n2n3n4n5n6n7n8n9n10
3S.NGroupn1n2n3n4n5n6n7n8n9n10
4S.NGroupn1n2n3n4n5n6n7n8n9n10
5S.NGroupn1n2n3n4n5n6n7n8n9n10
61127303229382636111344
722442382926433174739
833449356173334382933
94446482728384728112944
1051369343638161231424
1162344775101127401022
127343405030382461428
138426318442265141731
149131226818132313746
1510238232619483153014
161139442816333345436
17124324111311473493933
1813113351117393018241227
191422463426264519445014
20153233094930307191644
2116422353820272440204746
2217116504637214511405017
23182514825504023423429
2419316333629204620443325
252042423133248151424618
2621119464916374133844
272222622333431422464
282333421172944237491013
29244355144103044122942
30251275043281383351240
3126220193238434017243645
32273454244321819293314
332844345243874445421720
3429145101350364534293241
35302385234950222512238
3631322361725121027394046
37324
38331
39342
40353
41364
42371
43382
44393
45404
Data


Copy here....
Copy Columns.xlsm
ABCDEFGHIJKLM
1
2
3
4
5S.NGroupn1n3n5n8
611n1n3n5n8
722n1n3n5n8
833n1n3n5n8
944n1n3n5n8
105127323811
1162438267
127344351738
138446273811
149136343831
151023471040
161134350381
17124268214
181313261831
191423826915
20153928345
211643211149
2217113113924
231822342644
241932393019
2520422382720
2621116462140
272225485042
2823316362044
292442413482
302511941613
312622633314
3227334174449
3328435141012
34291274315
3530220324324
363134543229
373244324742
3833145133629
3934238235012
4035322171239
41364
42371
43382
44393
45404
copy columns


Kind regards,
Moti
Why have you got multiple header rows in the 'data' worksheet?
 
Upvote 0

Forum statistics

Threads
1,224,056
Messages
6,176,129
Members
452,707
Latest member
laplajewelry

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