[VBA] How to close CSV file after importing data

devo1234

New Member
Joined
Jun 18, 2018
Messages
9
Hi, I have poorly designed a VBA macro that opens/exports .csv data to an excel... works fine but when I export - say 10 csv, I'm left with 10 csv opened... I am trying to extend the coding so that the VBA macro also closes the csv files - thank you

Sub Macro1()
'
' Macro1 Macro
'


'


Set CSVPath = Sheets("Control").Range("CSV_Path")
Set CSVFile = Sheets("Control").Range("CSV_File")


Workbooks.Open Filename:=(CSVPath & CSVFile)


Rows("1:1").Select
Selection.Delete Shift:=xlUp

Range("A1").Select
Selection.End(xlDown).Select
xMoon = ActiveCell.Row + 1
Range(Cells(1, 1), Cells(xMoon, 1)).Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array _
(20, 1), Array(21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 1), Array(26, 1), _
Array(27, 1), Array(28, 1), Array(29, 1), Array(30, 1), Array(31, 1), Array(32, 1), Array( _
33, 1)), TrailingMinusNumbers:=True
Range(Cells(1, 1), Cells(xMoon, 33)).Select
Selection.Copy
Windows("TB_BB_Monitoring_2020.xlsm").Activate
Sheets("CSV").Select
Range("A1").Select
Selection.End(xlDown).Select
xStar = ActiveCell.Row + 1
Cells(xStar, 1).Select
ActiveSheet.Paste
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try something like this...

Code:
    [color=darkblue]Dim[/color] wbCSV [color=darkblue]As[/color] Workbook
    [color=darkblue]Set[/color] wbCSV = Workbooks.Open(Filename:=CSVPath & CSVFile)
    [color=green]'  your code here[/color]
    [color=green]'[/color]
    [color=green]'[/color]
    wbCSV.Close SaveChanges:=[color=darkblue]False[/color]
 
Upvote 0
This worked for me:

Sub Macro1()
'Import/Export CSV to Excel


Set CSVPath = Sheets("Control").Range("CSV_Path")
Set CSVFile = Sheets("Control").Range("CSV_File")


Set t = Workbooks.Open(CSVPath & CSVFile)


Rows("1:1").Select
Selection.Delete Shift:=xlUp

Range("A1").Select
Selection.End(xlDown).Select
xMoon = ActiveCell.Row + 1
Range(Cells(1, 1), Cells(xMoon, 1)).Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array _
(20, 1), Array(21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 1), Array(26, 1), _
Array(27, 1), Array(28, 1), Array(29, 1), Array(30, 1), Array(31, 1), Array(32, 1), Array( _
33, 1)), TrailingMinusNumbers:=True
Range(Cells(1, 1), Cells(xMoon, 33)).Select
Selection.Copy
Windows("TB_BB_Monitoring_2020.xlsm").Activate
Sheets("CSV").Select
Range("A1").Select
Selection.End(xlDown).Select
xStar = ActiveCell.Row + 1
Cells(xStar, 1).Select
ActiveSheet.Paste
Application.DisplayAlerts = False
t.Close SaveChanges:=False
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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