VBA Multiple selection (selection with CTRL)

L

Legacy 467415

Guest
Hello,

I'm trying to write a macro that copies all selected cells and paste it somewhere els.
My problem is with the selection part.

I'm using:
Dim CopyRng as range

Set CopyRng = selection

The problem:
If I select multiple rows with the CTRL hold in, it only copies the first rows.

Example:
If I select row 2,3,4 AND (CTRL) row 15,16 it will only copy row 2,3 and 4 but it should select my full selection (2,3,4 and 15,16)

Any ideas?

My full code:

VBA Code:
Sub RangeToCSVexport()

Dim CopyRng As Range, FileNm As String

Set CopyRng = Selection
CopyRng.Columns("O:P").Select
Set CopyRng = Selection.SpecialCells(xlCellTypeVisible)

FileNm = "C:\temp\Computers.csv"

Application.ScreenUpdating = False

Workbooks.Add (1) 'A new workbook with a worksheet
With ActiveWorkbook
CopyRng.Copy .Sheets(1).Range("A2")
.SaveAs Filename:=FileNm, FileFormat:=xlCSV
.Close SaveChanges:=True
End With

Application.ScreenUpdating = True

End Sub

Thanks in advance!
 
Last edited by a moderator:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub RangeToCSVexport()

Dim CopyRng As Range, FileNm As String

Set CopyRng = Intersect(Selection, Range("O:P"))

FileNm = "C:\temp\Computers.csv"

Application.ScreenUpdating = False

Workbooks.Add (1) 'A new workbook with a worksheet
With ActiveWorkbook
CopyRng.Copy .Sheets(1).Range("A2")
.SaveAs Filename:=FileNm, FileFormat:=xlCSV
.Close SaveChanges:=True
End With

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Hi & welcome to MrExcel.
How about
VBA Code:
Sub RangeToCSVexport()

Dim CopyRng As Range, FileNm As String

Set CopyRng = Intersect(Selection, Range("O:P"))

FileNm = "C:\temp\Computers.csv"

Application.ScreenUpdating = False

Workbooks.Add (1) 'A new workbook with a worksheet
With ActiveWorkbook
CopyRng.Copy .Sheets(1).Range("A2")
.SaveAs Filename:=FileNm, FileFormat:=xlCSV
.Close SaveChanges:=True
End With

Application.ScreenUpdating = True

End Sub

Hi and thanks!

Works perfectly, i've added Selection.SpecialCells(xlCellTypeVisible) because of the filters.

Thank you!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

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