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
-
Hello, if you don't open it in Excel, you won't be able to use its data from your program.
-
Hello yg_be
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.- You couldn't do it with Excel, it doesn't mean it's not possible.
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.
-