Convert .xls to delimiter File in Excel2003?

Rpax

New Member
Joined
Mar 27, 2020
Messages
7
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I was wondering if you can help. I'am trying to Open a Excel sheet that is linked to a PI database, wait 10sec for the data to refresh and then save and close the file. I'am trying to save the file as a txt delimiter file so to take a snapshot of the updated data (and not the format links to PI). Below is my effort


MyDateFormat = Year(now) & Right("0" & Month(Now), 2) & Right("0" & Day(now), 2)
Set Excel = CreateObject("Excel.Application")
Excel.Application.DisplayAlerts = False
Excel.Application.Visible = True
Excel.Workbooks.Open("C:\Program Files\Solvent_Tanks.xls")
Excel.RegisterXLL "pipc32.xll"
Excel.AddIns("PI-DataLink").Installed = False
Excel.AddIns("PI-DataLink").Installed = True
wscript.sleep(10000) 'sleep 10 seconds
Excel.ActiveWorkbook.SaveAs "C:\Program Files\Solvent_Tank_Reports\Solvents_" & MyDateFormat & ".txt", xlText, False
Excel.Activeworkbook.Close
Excel.Quit
WScript.Quit

The problem that I'am having is the saving of the file?. Also, I'am open to other ideas how I can get the raw data from the file (the snapshot of the data). Any ideas what's wrong?

Thanks - Rpax
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
try -

replace this

Excel.ActiveWorkbook.SaveAs "C:\Program Files\Solvent_Tank_Reports\Solvents_" & MyDateFormat & ".txt", xlText, False

with

VBA Code:
wline = ""
For r = 1 To Range("A65536").End(xlUp).Row
    lcol = Cells(r, 256).End(xlToLeft).Column
    wline = wline & Join(Application.Transpose(Application.Transpose(Range("A" & r).Resize(, lcol))), ",") & vbNewLine
Next r
Open "C:\Program Files\Solvent_Tank_Reports\Solvents_" & MyDateFormat & ".txt"  For Output As #1 'Replaces existing file
Print #1, wline
Close #1

hth,
Ross
 
Upvote 0
Hi Ross,

Many thanks for replying. I've tried below and what you recommended but got an error "Expected End of Statement" Line 14 Char 6". Sorry I can't add any insight as just dipping my toe into VB.

VBA Code:
MyDateFormat = Year(now) & Right("0" & Month(Now), 2) & Right("0" & Day(now), 2)
Set Excel = CreateObject("Excel.Application")
Excel.Application.DisplayAlerts = False
Excel.Application.Visible = True
Excel.Workbooks.Open("C:\Program Files\Solvent_Tanks.xls")
Excel.RegisterXLL "pipc32.xll"
Excel.AddIns("PI-DataLink").Installed = False
Excel.AddIns("PI-DataLink").Installed = True
wscript.sleep(10000) 'sleep 10 seconds
wline = ""
For r = 1 To Range("A65536").End(xlUp).Row
    lcol = Cells(r, 256).End(xlToLeft).Column
    wline = wline & Join(Application.Transpose(Application.Transpose(Range("A" & r).Resize(, lcol))), ",") & vbNewLine
Next r
Open "C:\Program Files\Solvent_Tank_Reports\Solvents_" & MyDateFormat & ".txt"  For Output As #1 'Replaces existing file
Print #1, wline
Close #1
Excel.Activeworkbook.Close
Excel.Quit
WScript.Quit
 
Upvote 0
Hi,

I've taken a different approach to saving this data. What I'am trying now is to copy the Data from one excel file to another.

The code below is opening a new work book. I then open my excel file that is linked to my PI data and I try to Copy and Paste the cells from this PI workbook to the other excel book. I then do a saveas with todays date to the new excel book. However, the Copy and Paste doesn't work (Subscription Out of Range Line 19). Appreciate an suggestions on this?

VBA Code:
MyDateFormat = Year(now) & Right("0" & Month(Now), 2) & Right("0" & Day(now), 2)
Set Excel = CreateObject("Excel.Application")
Set ExcelNew = CreateObject("Excel.Application")

ExcelNew.Application.DisplayAlerts = False
ExcelNew.Application.Visible = True
ExcelNew.Workbooks.Open("C:\Program Files\Solvent_Tank_Reports\Solvents"& ".xls")
wscript.sleep(5000) 'sleep 5 seconds

Excel.Application.DisplayAlerts = False
Excel.Application.Visible = True
Excel.Workbooks.Open("C:\Program Files\Solvent_Tanks.xls")
Excel.RegisterXLL "pipc32.xll"
Excel.AddIns("PI-DataLink").Installed = False
Excel.AddIns("PI-DataLink").Installed = True
wscript.sleep(5000) 'sleep 5 seconds

Excel.Workbooks("C:\Program Files\Solvent_Tanks.xls").Worksheets("Sheet1").Range("A1:C27").Copy ExcelNew.Workbooks(("C:\Program Files\Solvent_Tank_Reports\Solvents"& ".xls")  ".xls").Worksheets("Sheet1").Range("A1")

ExcelNew.Activeworkbook.SaveAs "C:\Program Files\Solvent_Tank_Reports\Solvents_" & MyDateFormat & ".xls"

Excel.Activeworkbook.Close
ExcelNew.Activeworkbook.Close
Excel.Quit
WScript.Quit
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top