I have a Macro i made and can't seem to get it to work correctly.
If i run the macro and pick a file in a folder with multiple files of the same type.
it only opens the first file in the folder. know matter what file i try to open, it will re-open the same file. for example. if i have 3 files with the names
cycle0001 , cycle0002, cycle0003 when ever i try to open file 003 it opens file 001 always. the code works great except i can't open the file i would like.
only the file with the lowest number in the name.
Here is my code.
Sub jsonDataExtractor()
'
' jsonDataExtractor Macro
' Json Data Extractor
'
' Keyboard Shortcut: Ctrl+t
'
File = Application.GetOpenFilename( _
FileFilter:=" (*.json), *.json", _
Title:="Select a file or files", _
MultiSelect:=True)
Application.ScreenUpdating = False
Workbooks.OpenText filename:= _
"cycle*.json", StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=True, Comma:=True, _
Space:=False, Other:=True, OtherChar:=":", FieldInfo:=Array(Array(1, 9), _
Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
Use code similar to this:
Option Explicit
Sub jsonDataExtractor()
Dim selectedFile As Variant, itm As Variant
selectedFile = Application.GetOpenFilename(FileFilter:=" (*.json), *.json", _
Title:="Select a file or files", _
MultiSelect:=True)
Application.ScreenUpdating = False
If TypeName(selectedFile) <> "Boolean" Then
For Each itm In selectedFile
Workbooks.OpenText Filename:=itm, StartRow:=1, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, Semicolon:=True, Comma:=True, _
Space:=False, Other:=True, OtherChar:=":", _
FieldInfo:=Array(Array(1, 9), _
Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
Next
End If
Application.ScreenUpdating = True
End Sub
.
You need to use the result returned by Application.GetOpenFilename()
.
If MultiSelect:=True it can return either
a Boolean (False if the user canceled)
or an array of variants (file names) even if the user selects only one file
.
If MultiSelect:=False it can return either
a Boolean (False if the user canceled)
or the file name (including its full path)
Related
I want to open .dcm files and edit them. Then the .dcm is read and placed before the active sheet i have on my current workbook. But i need to open any .dcm file, not only one specific one.
This is my current code
Sub Makro2()
'
' Makro2 Makro
'
'
Workbooks.OpenText Filename:= _
"H:\ENA\N_EN_AD3\3000_Mitarbeiter\Babacan\von_Nils\C7BB2HD3IINA_NRM_X302.dcm", _
Origin:=28592, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=True, _
Comma:=True, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)), TrailingMinusNumbers:=True
Range("A1").Select
Sheets("C7BB2HD3IINA_NRM_X302").Select
Sheets("C7BB2HD3IINA_NRM_X302").Move Before:=Workbooks( _
"ChrSet_Applikationsumgebung_inklHiAlti_X248_20221130.xlsm").Sheets(8)
End Sub
Usual method is to use a dialog box.
Option Explicit
Sub Makro2()
Const FOLDER = "H:\ENA\N_EN_AD3\3000_Mitarbeiter\Babacan\von_Nils\" ' initial search
' select file
Dim filename As String
With Application.FileDialog(msoFileDialogFilePicker)
.InitialFileName = FOLDER
.Title = "Please select the file to read"
.Filters.Add "DCM File", "*.dcm", 1
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no file is selected, abort
MsgBox "You did not select a file", vbExclamation
Exit Sub
End If
filename = .SelectedItems(1)
End With
' open selected workbook
Workbooks.OpenText filename:=filename, _
Origin:=28592, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=True, _
Comma:=True, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1)), TrailingMinusNumbers:=True
With ActiveWorkbook
.Sheets(1).Move Before:=ThisWorkbook.Sheets(8)
End With
MsgBox "Done"
End Sub
I am trying to get my Text To Columns to work in my VBA script, but it's not working for me.
I understand how it works in excel and even used the macro record to get it working, but when adding it to my script nothing happens. I even looked at online forums. What I'm doing is extracting data from one workbook to the other and the data that I am getting needs to be converted to Text To Columns once the data is transferred to the other workbook.
I have two separate Sub functions that I used below: Neither of them worked. One of them is from the macro record and the other I found online.
Code from Macro Record below:
Sub TextToCol()
Selection.TextToColumns Destination:=Range("D3"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, OtherChar _
:="" & Chr(10) & "", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _
TrailingMinusNumbers:=True
End Sub
Code from online forum below:
Sub TextToCol()
Selection.Range("D3:D8").TextToColumns _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Space:=True
End Sub
The code below is what I am using to connect to source workbook:
'Save Above Created New Workbook
sWorkbook.SaveAs Filename:="C:\Users\username\Desktop\fileName\Test.xlsx"
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Below is the code to send data to the destination workbook:
Workbooks("Test.xlsx").Worksheets("Sheet1").Range("D" & counter).value = dataThatWasExtracted
What am I doing wrong?
I want a whole text file to enter into a new sheet in excel. Right now I am only getting all values in to one cell, I want it to look like in the text file. Like every row get one row in excel.
I have tried this tutorial but this doesn't get it.
https://www.excel-easy.com/vba/examples/read-data-from-text-file.html
I solved it for you who wants to know how check below
Sub GetText()
Dim SheetName As String
Dim TMPWorkBook As Workbook
Dim FilePath As String
Dim TxtFilePath As String
Dim TxtFileName As String
Set WB = ThisWorkbook
SheetName = "Test_Result"
TxtFileName = "C:\getitdone.txt"
Workbooks.OpenText Filename:= _
TxtFileName _
, Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, Other:=True, OtherChar:="|", FieldInfo:=Array(Array(1, 1), _
Array(2, 1)), DecimalSeparator:=".", ThousandsSeparator:=",", _
TrailingMinusNumbers:=True
Set TMPWorkBook = ActiveWorkbook
Cells.Select
Selection.Copy
Workbooks("Getitdone.xlsm").Activate
Sheets.Add.Name = "Data"
Range("A1").PasteSpecial xlPasteValues
End Sub
I want to import .txt files from a folder to Excel and save them as Excel files.
The codes are not complete but the cycle through a folder part doesn't work in both of them.
It worked once, but it opened too many folders at once and froze up. I had to quit the program without saving and since then it doesn't work.
Here is the first code:
Sub getDataFromWbs()
Dim wb As Workbook, ws As Worksheet
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set fldr = fso.GetFolder("E:\MA\05_Sensordaten\Test\TEST2\")
For Each wbFile In fldr.Files
If fso.GetExtensionName(wbFile.Name) = “.txt” Then
Set wb = Workbooks.Open(wbFile.Path)
wb.Close
End If
Next wbFile
End Sub
I get the following error with Set fso = CreateObject(“Scripting.FileSystemObject”)
runtime error 424
object necessary
I also tried the dir approach:
Sub LoopImport2()
'Loop through all files in a folder
Dim FN As Variant
Application.ScreenUpdating = False
FN = Dir("E:\MA\05_Sensordaten\Test\TEST2\*.txt")
While FN <> ""
'Insert the actions to be performed on each file
Workbooks.OpenText Filename = FN, Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1)), DecimalSeparator:=",", TrailingMinusNumbers:=True
ActiveSheet.Name = FN
ActiveSheet.Cells.NumberFormat = "0.00"
'Set the FN to the next file
FN = Dir
Wend
End Sub
and get this error:
runtime error 1004 application or object defined error
With this part:
Workbooks.OpenText Filename = FN, Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1)), DecimalSeparator:=",", TrailingMinusNumbers:=True
In your first code, please, try:
Dim fso As Object and “Scripting.FileSystemObject” looks having strange double quote characters. Try using "Scripting.FileSystemObject".
In second one, please change Filename = FN with FileName:= FN.
I have around 30 files that I want to perform the same analysis on. Ideally, I want VBA to open all files and extract a certain (identical) piece of information. However, I don't know how to write a function for it. I was thinking of saving all the files as CSV and importing to python (a language I'm familiar with) which would make it easier for my data analysis. The reason I'm importing into excel is because python has difficulty reading .TX0 files (parsing line by line would be time consuming).
This is what i Have for the csv conversion. How do i translate this into the extraction of multiple files?
Sub TX0_CSV()
'
' TX0_CSV Macro
'
'
Workbooks.OpenText Filename:="X/.....fid002.TX0", Origin:= _
xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True
Range("A1:O13").Select
Range("O13").Activate
Selection.ClearContents
Rows("15:16").Select
Selection.Delete Shift:=xlUp
ActiveWindow.SmallScroll Down:=9
Range("A20:H29").Select
Range("H29").Activate
Selection.ClearContents
Range("J19").Select
ActiveWorkbook.SaveAs Filename:="X:\Joey\FID002.csv", FileFormat:=xlCSV, _
CreateBackup:=False
End Sub
You could convert it to a sub and then repeatedly call it.
Here is an example (Untested) Also note how we got rid of .Activate and .Select. You may want to see This
Sub ConvertFile(flName As String, newFileName As String)
Dim wb As Workbook
Dim ws As Worksheet
Workbooks.OpenText Filename:=flName, Origin:= _
xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True
Set wb = ActiveWorkbook
Set ws = wb.Sheets(1)
With ws
.Range("A1:O13").ClearContents
.Rows("15:16").Delete Shift:=xlUp
.Range("A20:H29").ClearContents
End With
wb.SaveAs Filename:=newFileName, FileFormat:=xlCSV
wb.Close (False)
End Sub
Example
Sub Sample()
ConvertFile "C:\Test1.dat", "C:\Test2.dat"
End Sub