Below code is not working and throwing error
Invalid Procedure call or argument.
This is the recorded macro code for importing CSV file data into Excel file.
Sub CopyCSVtoExcel()
'
' CopyCSVtoExcel Macro
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\hkum29\Desktop\New folder\data.csv", Destination:=Range("$A$1" _
))
.CommandType = 0
.Name = "data"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierSingleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
End With
End Sub
As per the MSDN KB QueryTable.CommandType Property (Excel),
You can set the CommandType property only if the value of the QueryType property for the query table or PivotTable cache is xlOLEDBQuery.
And since it is neither, simply comment the line .CommandType = 0 or delete that line.
Related
I Have recoded a code using the Record button.
Basically, I need to remove all special characters using 65001 in File Origin.
I have run a script that converts the .txt in 65001 formats and applies Text to columns as well. Because I need to open .txt data in an excel file
But the problem is the script is run perfectly fine for the 1st TXT file but But for the remaining .txt Text to column function doesn't apply.
Str = "TEXT;" & myFile
With ActiveSheet.QueryTables.Add(Connection:=Str, destination:=Range("$A$1"))
.Name = "UK"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
End With
I don't know the exact issue. Please find the attahced image as well the formating of 1st and the remainging txt data in excel file.
As you can see in 1st image there is noting after the heading "Letest deleivery date". But for the remain format there are data not perfectly extracted from txt to excel.
Please check and let me know the reason.
Since you're always adding a new querytable, try removing any existing ones first.
Dim Str
Str = "TEXT;C:\Tester\tmp.txt"
'remove any existing querytable(s) and clear associated range
Do While ActiveSheet.QueryTables.Count > 0
With ActiveSheet.QueryTables(1)
Debug.Print "Clearing querytable at ", .ResultRange.Address
.ResultRange.Clear
.Delete
End With
Loop
With ActiveSheet.QueryTables.Add(Connection:=Str, Destination:=Range("$A$1"))
.Name = "UK"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells 'this is better I think
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=True
End With
I have 250 .txt files in a folder. I want to import each file into separate worksheets. I have a worksheet which name is "Veri" in order to have a list of datas.
list of the data example:
HLPA01 "TEXT;C:\Users\utku.arslan\Desktop\Esat\HLPA01.txt"
HLPA02 "TEXT;C:\Users\utku.arslan\Desktop\Esat\HLPA02.txt"
HLPA03 "TEXT;C:\Users\utku.arslan\Desktop\Esat\HLPA03.txt"
HLPA04 "TEXT;C:\Users\utku.arslan\Desktop\Esat\HLPA04.txt"
HLPA05 "TEXT;C:\Users\utku.arslan\Desktop\Esat\HLPA05.txt"
HLPA06 "TEXT;C:\Users\utku.arslan\Desktop\Esat\HLPA06.txt"
and also wrote this VBA code.
Probably my mistake is about using "location_string" and "data_name" under "with"
Sub Deneme_1()
Dim location_string As String
Dim data_name As String
Sheets("Veri").Select
For i = 1 To 5
Sheets("Veri").Select
location_string = Range("b" & (i + 1))
data_name = Range("a" & (i + 1))
Sheets.Add After:=ActiveSheet
With ActiveSheet.QueryTables.Add(Connection:= _
"location_string", Destination:=Range( _
"$A$1"))
.Name = "data_name"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next i
End Sub
I'm not very Excel savvy, much less coding savvy but at the moment, I'm tasked with setting up an Excel record sheet of data, with such data pulled from fillable PDFs that are merged into a .csv file.
I want to have a simple command that will pull that data with the following mechanism/command path in Excel:
Data > Grab Data from Text > select .csv file > delimited > comma delimited > ... > destination = next blank cell > Finish/Okay.
What I'd like help with is to have the Macro input the next .csv file data into the next empty cell in column A to ultimately create a growing list.
As of right now, I've recorded the Macro and figured if I could change the destination in line 7 from "Destination:=Range( _$A$1) to select the next blank cell instead, I'd be good.
For the CSV file titled "combined.csv", I have a command script someone helped me with that would basically rewrite it with new data each time under the same file name, which won't be a problem for us.
Sub Macro5()
Macro5 Macro
Application.CutCopyMode = False
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\Alvin_2\Desktop\CSV Files\combined.csv", Destination:=Range( _
"$A$1"))
.CommandType = 0
.Name = "combined"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
So far, I have had no luck with finding a function to select the next blank cell in column A.
Would anyone have an idea of the code I should enter in the Destination section to get the Macro to input the new data onto the next empty cell in column A?
Try:
Range("A1").End(xlDown).Offset(1,0)
I am trying to open a file from a user specified path using a VBA Function.
When I paste the content of the file to the desired worksheet, it pastes some weird unrecognizable characters in the worksheet. Can anyone figure out what am I doing wrong?
Here is my code:
Sub x_Macro()
'****************Declaring relevant variables****************'
Dim t As Range
Dim path: path = Application.GetOpenFilename("XLS Files (*.xls), *.xls")
Dim cht1 As ChartObject
'Dim button_click As VbMsgBoxResult
Application.ScreenUpdating = False
Sheets(2).Activate
If path = False Then Exit Sub
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & path, Destination:=Range("A1"))
.FieldNames = True
.RowNumbers = True
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
I've just been acquainting myself with the VBA language and would appreciate any help. I'm trying to write a program that updates an existing tab with a new file.
Here's what I have so far:
Sub ImportFile()
'
' ImportFile Macro
'
' Keyboard Shortcut: Ctrl+Shift+I
' Clears active sheet
Cells.Select
Selection.ClearContents
Range("A1").Select
' "Open" prompt window
Dim aFile As String
aFile = Application.GetOpenFilename(FileFilter:="Comma separated values files, *.csv")
If aFile = "False" Then Exit Sub
' Import .csv file to active sheet
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\jiarui.hou.THRUBIT\Desktop\thrubit\data\PS25R_175c_20110711_proc1.csv" _
, Destination:=Range("$A$1"))
.Name = "PS25R_175c_20110711_proc1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
But the problem is in the first two lines of the third part, it directs to a specific file. But I want the program to import ANY file that I had specifically selected. I guess what I'm asking is how do I link the second part which prompts a window to open a file, to the third part which links that directory address in place of the current SPECIFIC directory address.
Use the concatenation operator & and make the string that you pass to connection be "TEXT;" & aFile
With ActiveSheet.QueryTables.Add(Connection:= "TEXT;" & aFile, Destination:=Range("$A$1"))
Also, I think instead of "False" you want False on the 7th line.