Function Application.GetOpenFilename
Solved
mamined
Posted messages
37
Status
Member
-
mamined Posted messages 37 Status Member -
mamined Posted messages 37 Status Member -
Hello group,
I built the code below while watching videos on YouTube (yes, I'm not skilled..)
So the code works and everything is fine with one exception.
I open a CSV file and it opens in Excel, which is a problem because the data is combined together and separated by a "|" separator but also has ";" and ":" so it would be necessary for my code not to open it with Excel but with a text editor like WordPad or Notepad.
How to modify my code so that when it indicates: Set MonClasseur = Application.Workbooks.Open(ListFile) it does so with one of the 2 types of files mentioned above?
Thank you in advance for your help
I built the code below while watching videos on YouTube (yes, I'm not skilled..)
So the code works and everything is fine with one exception.
I open a CSV file and it opens in Excel, which is a problem because the data is combined together and separated by a "|" separator but also has ";" and ":" so it would be necessary for my code not to open it with Excel but with a text editor like WordPad or Notepad.
How to modify my code so that when it indicates: Set MonClasseur = Application.Workbooks.Open(ListFile) it does so with one of the 2 types of files mentioned above?
Thank you in advance for your help
Option Explicit Sub AutoImportDataTXT() 'variable declarations Dim ListFile As Variant Dim MonClasseur As Variant 'disable alerts Application.ScreenUpdating = False Application.CutCopyMode = False 'filter to see only the type of file to select ListFile = Application.GetOpenFilename(Title:="Select data and import data", fileFilter:="CSV Files(*.csv*),*xls*", ButtonText:="Click") 'handle the case of the cancel button click in case of error If ListFile <> False Then 'indicate the selected file Set MonClasseur = Application.Workbooks.Open(ListFile) 'copy data MonClasseur.Sheets(1).Range("A1").CurrentRegion.Copy 'paste data ThisWorkbook.Sheets("sample").Range("A1").PasteSpecial xlPasteValues 'close source workbook MonClasseur.Close End If End Sub
3 answers
yg_be
Posted messages
23437
Registration date
Status
Contributor
Last intervention
Ambassadeur
1 588
Hello, if you don't open it in Excel, you won't be able to use its data from your program.
thank you for your reply but I do not understand why I cannot use the data since I do it manually and it works very well.
So in my opinion, we should be able to automate it.
Process:
Select csv file
right click
open with
Select notepad or other text editor
press ctrl A + ctrl C
go to my Excel sheet (where I have my code in the right sheet in A1)
ctrl V
so if it works like that, we should be able to automate it in my opinion.
I suggest that you only use Excel instead, maybe by working a little more on it to transform the data.
actually, as I said, I can't with Excel. It's not working, the reason is simple. If I open my csv in Excel, I have split columns while I shouldn't have them split. Look at the 2 images and you'll understand the problem.
This might help you: https://docs.microsoft.com/fr-ch/office/vba/api/excel.workbooks.open
Otherwise, you can quite easily manipulate and transform data in VBA.