How to close .CSV file without referring to it by name

Lenna

Active Member
Joined
Jun 25, 2014
Messages
269
I'm running a macro from .xlsm file that opens one .CSV file(user choice). After I copied data from .CSV file I would like to close it without referring to file by its name. It is the only .CSV file opened at the moment. Could someone please suggest a code (how to close .CSV file).

Thanks,

Lenna
 
Use an object variable:

Code:
Sub L()
    Dim wkb As Workbook
    
    Set wkb = Workbooks.Open("C:\somePath\someFile.csv")
    ' code here
    
    wkb.Close SaveChanges:=False ' or True
End Sub
 
Upvote 0
I got it to work but please suggest how to improve the code.
I only want to copy to the last row of data not to the last row of a workbook.

Thanks,

Private Sub CommandButton1_Click()


'Dim csvFile As Variant

'Dim csvBook As Workbook



csvFile = Application.GetOpenFilename
If (csvFile <> False) Then
Workbooks.Open csvFile
Set csvBook = ActiveWorkbook
End If
Columns("A:R").Select
Selection.Copy
Windows("Test.xlsm").Activate
Range("A1").Select
ActiveSheet.Paste
csvBook.Close SaveChanges:=False ' or True
End Sub
 
Upvote 0
You have a lot of selecting and activating, and the use of "ActiveWorkbook" really scares me - this can do very bad things and using active anything should really be avoiding at all costs, unless there is no other way to do it. Also, I'd suggest limiting what is shown on the prompt to open a file type - does your code handle a .pdf file being selected? (probably not, unless you have some unpasted code). Also, you have an error check if the user didn't select a workbook, but then your code will continue on if they don't select a file to load, throwing more unhandled errors...

So, I'd suggest the following revisions:

Code:
Dim csvFile as String
Dim csvBook as Workbook

csvFile = Application.GetOpenFilename ("Text Files (*.csv),(*.csv),,"put prompt so user knows why the prompt came up here<TELL up popped prompt the why know they so workbook select to user>")

If csvFile<>"False" Then
Set csvBook = Workbooks.Open(csvFile)

With csvBook
.Range("A1:R" & .Range("A1").End(xlDown).Row).Copy
ThisWorkbook.Sheets("Enter your sheet name here"<ENTER name here sheet your>).Range("A1").Paste
.Close False
End With

Else: Exit Sub
End If
End Sub
 
Upvote 0
This statement throws an error.

csvFile = Application.GetOpenFilename ("Text Files (*.csv),(*.csv),,"Please select CSV file to open")


You have a lot of selecting and activating, and the use of "ActiveWorkbook" really scares me - this can do very bad things and using active anything should really be avoiding at all costs, unless there is no other way to do it. Also, I'd suggest limiting what is shown on the prompt to open a file type - does your code handle a .pdf file being selected? (probably not, unless you have some unpasted code). Also, you have an error check if the user didn't select a workbook, but then your code will continue on if they don't select a file to load, throwing more unhandled errors...

So, I'd suggest the following revisions:

Code:
Dim csvFile as String
Dim csvBook as Workbook

csvFile = Application.GetOpenFilename ("Text Files (*.csv),(*.csv),,"put prompt so user knows why the prompt came up here<tell up="" popped="" prompt="" the="" why="" know="" they="" so="" workbook="" select="" to="" user="">")

If csvFile<>"False" Then
Set csvBook = Workbooks.Open(csvFile)

With csvBook
.Range("A1:R" & .Range("A1").End(xlDown).Row).Copy
ThisWorkbook.Sheets("Enter your sheet name here"<enter name="" here="" sheet="" your="">).Range("A1").Paste
.Close False
End With

Else: Exit Sub
End If
End Sub

</enter></tell>
 
Upvote 0
I removed " and now it went to

.Range("A1:R" & .Range("A1").End(xlDown).Row).Copy
and stopped.

Please help to fix this.

Thanks,

Lenna
 
Upvote 0

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