VBA Multiple selection (selection with CTRL)

M Lievens

New Member
Joined
Oct 18, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

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