Import CSV data from web service into Excel - html

I have written a simple web service that returns large volumes of csv data. I will to import this into Excel in a tabular format using Excel's "Data From Web" function.
Is there a way to get Excel to automatically parse the csv fields returned into individual columns as part of the import operation?
At present the only means I have for doing this is to first import the data into a single column and then write VBA code to select the data and split it using TextToColumns. This feels messy / error-prone.
The other alternative I have is to modify the web server to serve back the data as HTML. However, I'm reluctant to do this as adding tags around each csv field will greatly impact the volume of data returned.

Adamski,
Here is something that I use. I found the core somewhere on the internet, but don't know where.
What it does is it opens a tab separated file and reads the data in an excel sheet
If Answer1 = vbYes Then 'I asked prior if to import a tab separated file
Sheets("ZHRNL111").Select 'Select the sheet to dump the data
On Error Resume Next
With ActiveSheet
If .AutoFilterMode Then .ShowAllData 'undo any autofilters
End With
Sheets("ZHRNL111").Cells.Clear 'remove any previous data
On Error GoTo 0
Range("A1").CurrentRegion.Delete
Fname = MyPath & "\LatestReports\Report-111.tsv"
Open Fname For Input As #1
iRow = 1
Line Input #1, Record
On Error Resume Next
Do Until EOF(1)
P = Split(Record, vbTab)
For iCol = 1 To 14
Cells(iRow, iCol) = P(iCol - 1)
Next iCol
iRow = iRow + 1
Line Input #1, Record
Loop
On Error GoTo 0
Close 1
End If
Regards,
Robert Ilbrink

Depending on the version of excel you are running you should be able to open the .csv in excel and use the text to columns feature built into excel.
Also, if you could modify your csv to split columns based on commas "," instead of tabs excel would open it directly without the need to format it.
I know however this can sometimes be a problem depending on the data you are importing because if the data contains a comma it must be inside quotations.
In my experience the best way is to use quotations on every field if possible.
Hope this helps.

I am actually creating a product right now to do this in both XML and JSON for Excel. I know comma delimited does work in Excel, with some caveats. One way around it is to put some "" around the text in between the delimiters for the "Data From Web" feature. There are still issues with that however. I did find that despite it's increased size, XML was the best option for quick turn around. I was able to create the service and hand my project manager the Excel document which he could update at anytime.

Related

Deleting rows with empty columns in .csv with vbs

I currently have to code a script for my work that deletes the complete row if column D is empty in a .csv file with vbs.
I found a solution that might work thought I struggle a bit tbh.
enter Const xlUp = -4162 ' Excel variables are not defined in vbscript
'Set objFile = objFSO.OpenTextFile("Testexport_neu.csv", ForReading) 'Datei Quelle
Dim oBook : Set oBook = objFile
Dim oSheet : Set oSheet = oBook.Sheets(1)
Dim iLastRow, iRow
iLastRow = oSheet.Cells(oSheet.Rows.Count, 3).End(xlUp).Row
For iRow = iLastRow to 1 Step -1 'assumes a header row otherwise use 1 instead of 2
If oSheet.Range("D" & iRow) = "" Then
oSheet.Range("D" & iRow).EntireRow.Delete ' delete row if blank
End If
Code by User Dave
now to my problem, as I can't find any helpful documentation I have no clue on how to implement the .csv file the right way. ... or what the oXYZ methods do...
Thanks for any advice
As to why I haven't commented on the original post where Dave put this code on, as this post was made in 2018 I wasn't sure if I revive any answers there tbh
Does it have to be VBScript ?
Is your data really CSV (text based), like this ? Post a small example, it would help greatly.
COLA;COLB;COLC;COLD;COLE;COLF
AAA;BBB;CCC;DDD;EEE;FFF
A2;B2;C2;;E2;F2 <<<< left out
A3;B3;C3;D3;E3;F3
The job would be trivial to do with AWK: split the lines (just set separator) then check if column D is empty, if NOT then print the entire line.
Effectively this will do what you ask for: filter out lines with empty column D. Not by deleting it, but by outputting everything else. I will edit this post later with an example. AWK is a free program available for pretty much every OS out there.
Same can be done with other tools.
Btw, you seem to handle a CSV file like an Excel document, accessing Excel features. Excel wants Windows to believe CSV is actually an Excel sheet, when it's just text. This can lead to several unwanted effects as Excel tries to be "clever" on your behalf.

MS Access unable to locate my excel range when importing

I have this code that works on one spreadsheet, but not another. I am just trying to automate the transfer of an excel data range to an access table, like so
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "UsysFastTrack", strFilePath, False, strRange
strFilePath and strRange are just strings that contain the full file path (including the worksheet name and extension) and the name of an excel range in the worksheet, respectively. This line causes an error
The Microsoft Access database engine could not find the object ...
This error appears a lot online and somewhere I saw the advice to try the import wizard to see what I get and lo and behold, right as I hit the last Next
This is the exact same error and it stops me dead in my tracks. What's funny is that you can see the range exists in the spreadsheet before your very eyes.
What's going on here?
Looks like the names of your named ranges are not valid, they are similar to regular ranges. Try to change names

Writing a single column from a table in MS Access VBA to .txt file

this is my first time posting a question here, but I almost always get good answers from searching this site. I'm trying to find out if there is a better way to speed up this process I have of writing a table column in Access to a .txt file. Here is the code I have, which works, but it's on the slow side. It takes about 45s to write around 7000 items.
lsFileName2 = "E:\DOI_Inventory2\SyncData\EquipUser.txt"
LiFileNumb = FreeFile
SysCmd acSysCmdSetStatus, "Updating User Equipment List"
Open lsFileName2 For Output As LiFileNumb
With rst
Do While Not .EOF
Write #LiFileNumb, ![EqUserFile]
.MoveNext
Loop
End With
dbs.Close
Close LiFileNumb
I'm fairly new to the IT field so any help would be greatly appreciated. Thanks
Just to add a note, the actual query is fine. I already checked that and it's pretty fast.
Create a query, save it with a name, and then use TransferText to export the query's data to your text file.
So assuming you have a SELECT query named qryExportMe like this which returns your table's column data correctly ...
SELECT EqUserFile
FROM YourTable;
... refer to this TransferText example and adapt it to fit your needs.
DoCmd.TransferText TransferType:=acExportDelim, _
Tablename:="qryExportMe", _
FileName:="E:\DOI_Inventory2\SyncData\EquipUser.txt", _
HasFieldNames:=True
Check the TransferText options at that linked page or from Access' built in help system.
Note you are not required to include the option names. I added them to help you keep track of which is which.
If this approach speeds up your export operation adequately, I think it will be because Access handles the task as a single set-based file write. Your recordset approach required Access to process one row at a time ... and one reason such approaches are called RBAR (row by agonizing row) is because they are often painfully slow.

import tab-delimited txt into Access table using VBA

I am trying to import a tab-delimited txt file into an Access table using VBA. In my code, I want to insert it into a table that has not yet been created.
Here is what I tried doing. Note - I was able to make this work with a CSV, and without including this: DataType:=xlDelimited, Tab:=True
Sub InsertData()
'import CSV into temp table
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tbl_TEMP", _
FileName:=FileNameVariable, HasFieldNames:=True, DataType:=xlDelimited, Tab:=True
End Sub
When I run this block, I get the following error on DataType:=xlDelimited, Tab:=True
Compile error: Named argument not found
How should I change this in order to pull in the tab-delimited txt file so each column from the txt has its own column in Access?
As you have seen from the other articles on the topic, there really isn't a generic way to import tab-delimited text files. All of the other solutions I've seen say that you should import the tab-delimited text file once, save the import specification, and then use that import specification for all subsequent imports. The problem there is that if you want to import a different tab-delimited file the specification may not match.
The only way I've found to do it generically (short of "rolling your own" code using FileSystemObject, Split(s, vbTab), etc.) is to create a completely generic specification for all 255 possible fields and use that. It requires a one-time setup as follows:
Copy the CSV data from the Pastebin here, paste it into your favorite text editor, and save it as GenericTabSpecification.csv.
Open that file in Excel, select all 256 rows and 4 columns, then hit Ctrl+C to copy.
In Access, start the import wizard for text files and choose any tab-delimited file. (We won't actually be importing it.)
When you get to the first page in the wizard, click the "Advanced..." button.
In the Import Specification dialog, verify the settings (Field Delimiter, Text Qualifier, etc.) then click the top-left corner of the Field Information grid so all rows are selected:
Hit Ctrl+V to paste the data from Excel into the grid. The grid should now contain 255 rows.
Click the "Save As..." button and name the specification GenericTabSpecification. Once that is done, cancel out of the wizard.
Now we can do a generic import from VBA using a statement like this
DoCmd.TransferText _
TransferType:=acImportDelim, _
SpecificationName:="GenericTabSpecification", _
TableName:="newTable", _
FileName:="C:\Users\Gord\Desktop\foo.txt", _
HasFieldNames:=False

Link Excel sheet to Access: #Num! error for numeric values in text-numeric hybrid column

This is about a legacy Access 2003 database that I've inherited. There's some code that links an Excel (97-2003) spreadsheet:
tdf.Connect = "Excel 5.0;HDR=Yes;IMEX=2;DATABASE="&strXLFileName
tdf.SourceTableName = strSourceTableName & "$"
CurrentDb.TableDefs.Append tdf
When I open the linked table afterwards, I see #Num! in place of numeric values in a column that is supposed to contain both numeric and text.
For example, in Excel:
Field1
H88
234
X65
432
Linked table in Access:
Field1
H88
#Num!
X65
#Num!
I've tried the following:(a) changing Excel 5.0 to Excel 8.0, which is more accurate for the format the soruce files are in; (b) importing using DoCmd.TransferSpreadsheet instead of linking.
The first still gives #Num!, while importing gives nulls.
Upgrading to later versions is not an option at the moment - there are a number of places within the code that use things that Application.FileSearch that require careful rewriting and testing.
Anyone know how to get Access 2003 to treat the "numbers" like they were text, too?
TIA!
Change IMEX=2 to IMEX=1 to treat all the values as text.
You can read more about IMEX at Connection strings for Excel 2007.