VBA to Import CVS Files

Glasgowsmile

Board Regular
Joined
Apr 14, 2018
Messages
152
Good Morning,

I've got the code below -- I was hoping just adding CVS to the filter would allow me to import it but it doesn't so I need to help. How can I get this code to import CVS files?
Code:
Sub ActiveRateCategory()   
   Sheets("ActvRateCat SumRpt").Range("A:M").Clear
   Dim wkbCrntWorkBook As Workbook
   Dim wkbSourceBook As Workbook
   Set wkbCrntWorkBook = ActiveWorkbook
   With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa; *.cvs"
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.Count > 0 Then
         Workbooks.Open .SelectedItems(1)
         Set wkbSourceBook = ActiveWorkbook
         wkbSourceBook.Range("A1:L100").Copy
         wkbCrntWorkBook.Sheets("ActvRateCat SumRpt").Range ("A1")
         wkbSourceBook.Close False
      End If


   End With
End Sub
 
Last edited:

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows
Good Morning,

I've got the code below -- I was hoping just adding CVS to the filter would allow me to import it but it doesn't so I need to help. How can I get this code to import CVS files?
Code:
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa; *.[COLOR=#ff0000]cvs[/COLOR]"  'must be [B]csv[/B]

Try this:

Code:
Sub ActiveRateCategory()
   Sheets("ActvRateCat SumRpt").Range("A:M").Clear
   Dim wkbCrntWorkBook As Workbook
   Dim wkbSourceBook As Workbook
   Set wkbCrntWorkBook = ActiveWorkbook
   With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
[COLOR=#0000ff]      .Filters.Add "Excel 2007-13", "*.xlsx"[/COLOR]
[COLOR=#0000ff]      .Filters.Add "Excel 2003", "*.xls"[/COLOR]
[COLOR=#0000ff]      .Filters.Add "Excel macros", "*.xlsm"[/COLOR]
[COLOR=#0000ff]      .Filters.Add "csv Files", "*.csv"[/COLOR]
[B][COLOR=#006400]      .FilterIndex = 4[/COLOR][/B]
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.Count > 0 Then
         Workbooks.Open .SelectedItems(1)
         Set wkbSourceBook = ActiveWorkbook
         wkbSourceBook.Range("A1:L100").Copy
         wkbCrntWorkBook.Sheets("ActvRateCat SumRpt").Range ("A1")
         wkbSourceBook.Close False
      End If
   End With
End Sub
 

Glasgowsmile

Board Regular
Joined
Apr 14, 2018
Messages
152
Ah, thanks!

One more question -- when I do import it, it takes me to the sheet that the data was pasted to, how do I have it take the user back to the original sheet where the button for importing is?
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,780
Messages
5,598,032
Members
414,205
Latest member
Tushark

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
Top