Macro to select columns A to L from the selected A Column cells

caligirl626

New Member
Joined
Nov 28, 2022
Messages
24
Office Version
  1. 365
Platform
  1. Windows
Hello everyone. Looking for a macro that will copy row information from column A through L.

1669695314111.png


This code below copies a single row, in this case Row 2, cells A2:K2, as shown above.

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveCell.Range("A1:K1").Select
    Selection.Copy
End Sub

How can the code be modified to copy all the columns, A thru K, of user selected A column cells?
For the example shown below, if cells A4 to A7 are selected, the macro should copy array A4:K7

1669696077148.png


The macro should consider that the number of the user selected column A cells will vary. TIA
 

Attachments

  • 1669695234259.png
    1669695234259.png
    48.5 KB · Views: 7

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Maybe this way
VBA Code:
Sub MM1()
Dim sr As Range, firstRow As Long, lastRow As Long
Set sr = Selection
fr = sr.Cells(1, 1).Row
lr = sr.Cells(sr.Rows.Count, 1).Row
Range(Cells(fr, 1), Cells(lr, 11)).Copy
End Sub
 
Upvote 0
Another option if you only select contiguous cells in col A
VBA Code:
Sub caligirl()
   Selection.Resize(, 11).Copy
End Sub
 
Upvote 0
Maybe this way
VBA Code:
Sub MM1()
Dim sr As Range, firstRow As Long, lastRow As Long
Set sr = Selection
fr = sr.Cells(1, 1).Row
lr = sr.Cells(sr.Rows.Count, 1).Row
Range(Cells(fr, 1), Cells(lr, 11)).Copy
End Sub
Michael, thank you! Tested this out and works great. Good job!
 
Upvote 0
Another option if you only select contiguous cells in col A
VBA Code:
Sub caligirl()
   Selection.Resize(, 11).Copy
End Sub
Love the simple solution, but now that you brought it up, what if the selections are NOT contiguous? I have some cases were I'd like to skip a line or two. How could the macro be modified?
 
Upvote 0
How about
Excel Formula:
Sub caligirl()
   Intersect(Selection.EntireRow, Range("A:K")).Copy
End Sub
 
Upvote 0
Solution
How about
Excel Formula:
Sub caligirl()
   Intersect(Selection.EntireRow, Range("A:K")).Copy
End Sub
I like it! The intersect picks up the non-contiguous rows nicely. However, let's say I insert a column or two at the beginning, say A and B. Let's say 2 columns are added. So now the range to copy is C:M. Can you modify the code to adjust for that? TIA
 
Upvote 0
Just change the range from A:K to C:M
 
Upvote 0
Just change the range from A:K to C:M
What I meant to say is, the amount of leading columns will vary, so the starting column will change and may not necessarily be C:M. I want to macro to select the 11 slides to the right of the selected contiguous and / or non-contiguous cells in the starting column, whatever that may be, if that makes sense. Anyone's help is appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,548
Members
449,038
Latest member
Guest1337

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