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
 
Why have you got multiple header rows in the 'data' worksheet?
Hello HighAndWilder, you can change any text or numbers when you select in data sheet C5, F5, G5 or any it should copy column below the row5 to 45 but it is copying from row1 I guess code need to be adjusted. I am not sure but may be it is due to CurrentRegion?

Kind regards,
Moti
 
Upvote 0

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.
Hello HighAndWilder, you can change any text or numbers when you select in data sheet C5, F5, G5 or any it should copy column below the row5 to 45 but it is copying from row1 I guess code need to be adjusted. I am not sure but may be it is due to CurrentRegion?

Kind regards,
Moti
When you select the column headings for the columns that you want to be copied, which row are you selecting these from? You should be selecting them from row 5 of the
'data' worksheet.
 
Upvote 0
When you select the column headings for the columns that you want to be copied, which row are you selecting these from? You should be selecting them from row 5 of the
'data' worksheet.
Re: CurrentRegion. - Because my data did not include anything above row 5, and yours did, it did copy from row 1.

I'll change it and re post.
 
Upvote 0
Re: CurrentRegion. - Because my data did not include anything above row 5, and yours did, it did copy from row 1.

I'll change it and re post.
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
Dim rngData As Range
Dim Ws As Worksheet
Dim lngLastRow As Long

  ActiveWorkbook.Save
    
  Set Ws = Worksheets("data")
    
  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
  
  With Ws
    lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set rngData = .Range("A5").Resize(lngLastRow - 4, 12)
  End With
    
  For Each r In rng.Cells
    If Not rngToCopy Is Nothing Then
      Set rngToCopy = Union(rngToCopy, rngData.Columns(r.Column).Resize(41, r.Columns.Count))
    Else
      Set rngToCopy = rngData.Columns(r.Column).Resize(41, r.Columns.Count)
    End If
  Next r
    
  With Worksheets("copy columns")
    
    .Activate
    
    Set rngDestination = .Range("C5")
  
    For Each rng In rngToCopy.Areas
              
      rng.Copy
      
      rngDestination.PasteSpecial xlPasteValues
      
      Application.CutCopyMode = False
  
      Set rngDestination = rngDestination.Offset(, rng.Columns.Count)
      
    Next rng
  
    .Range("C5").Select
  
  End With
  
  ActiveWorkbook.Save
  
End Sub
 
Upvote 1
Solution
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
Dim rngData As Range
Dim Ws As Worksheet
Dim lngLastRow As Long

  ActiveWorkbook.Save
   
  Set Ws = Worksheets("data")
   
  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
 
  With Ws
    lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set rngData = .Range("A5").Resize(lngLastRow - 4, 12)
  End With
   
  For Each r In rng.Cells
    If Not rngToCopy Is Nothing Then
      Set rngToCopy = Union(rngToCopy, rngData.Columns(r.Column).Resize(41, r.Columns.Count))
    Else
      Set rngToCopy = rngData.Columns(r.Column).Resize(41, r.Columns.Count)
    End If
  Next r
   
  With Worksheets("copy columns")
   
    .Activate
   
    Set rngDestination = .Range("C5")
 
    For Each rng In rngToCopy.Areas
             
      rng.Copy
     
      rngDestination.PasteSpecial xlPasteValues
     
      Application.CutCopyMode = False
 
      Set rngDestination = rngDestination.Offset(, rng.Columns.Count)
     
    Next rng
 
    .Range("C5").Select
 
  End With
 
  ActiveWorkbook.Save
 
End Sub
Amazing HighAndWilder, absolutely correct data is coping perfect within the range and with values only 100% solved! (y)

I appreciate your kind help. Have a great weekend. Good Luck.

Kind regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,224,056
Messages
6,176,126
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