Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Date Field in Excel using macro code in Access

Status
Not open for further replies.

Sech

Programmer
Jul 5, 2002
137
GB
Hi all,

Here's a real difficult problem for you Access experts to solve! I've created a database that has a number of reports. These are exported out to Excel spreadsheets on the press of a button using the OutputTo command (Note: The report is exported NOT the query behind the report - I need the report layout). After export I then do various formatting by linking into Excel using macro code and a reference to the Microsoft Excel Object Library. With me so far? Good! This formatting comprises of realigning columns and changing titles into bold font etc. Many of the reports have date fields and this is where the problem occurs. I want to be able to sort in date order on these fields immediately after opening the files. However when they are first exported, the date fields do not begin as date format, instead they remain as text until you click inside each of the cells with the mouse. There is a process known as "Convert Text to Columns" which can be selected from the Data menu. In Excel, if I select one of the date columns and then run Text To Columns on it, all the values immediately become proper dates instead of text values. You still understand what I'm talking about? Ok so I thought if this works in Excel why not record a macro, grab the code and use it in Access to convert the dates automatically after the file is created...so Ive done this and set up the code. In the following example as a test Ive set it up to convert the 7th column (column G) which is a date field...

Code:
Dim oxlApp As Excel.Application
Dim oxlWorkbook As Workbook
Dim oxlWorksheet As Worksheet

'Start a new excel application in the background
Set oxlApp = New Excel.Application

'Open the workbook passed as the parameter in workbook object
Set oxlWorkbook = oxlApp.Workbooks.Open(strFileName)

'Open Sheet1 of the workbook in a worksheet object
Set oxlWorksheet = oxlWorkbook.Sheets("Sheet1")

'Select date column
oxlWorksheet.Columns("G:G").Select
            
'Run Text To Columns on it to fix dates
oxlApp.Selection.TextToColumns Destination:=Range("G1"), DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, space:=False, Other:=False, FieldInfo:=Array(1, 1)

'Save the workbook
oxlWorkbook.Save

'Close the workbook and quit the Excel application
oxlWorkbook.Close False
oxlApp.Quit
    
'Clear vars
Set oxlWorksheet = Nothing
Set oxlWorkbook = Nothing
Set oxlApp = Nothing

But the problem is when I run the code, instead of working like it should, the following error appears:

Error 1004 - Microsoft Excel can convert only one column at a time. The range can be many rows tall but no more than one column wide. Try again by selecting cells in one column only.

This is extremely confusing considering I have not selected multiple columns at all (only column G). So my question is what am I doing wrong? Has anyone ever needed to do this themselves or know an alternatie solution to fixing the dates? Please please let me know!
 
Try using oxlWorksheet.Columns("G").Select create a Convert_To_Text function using the snippet below. I've not had you declared problem using this.


Dim xCell As Variant
For Each xCell In Selection
If Left$(xCell.Value, 1) <> &quot;'&quot; Then
xCell.Value = &quot;'&quot; & Trim(xCell.Value)
Debug.Print xCell.Value
End If
Next xCell
-------------------------------------
scking@arinc.com
Try to resolve problems independently
Then seek help among peers or experts
But TEST recommended solutions
-------------------------------------
 
Changing the selection to &quot;G&quot; makes no difference on its own. What exactly does the Convert To Text code do? I fitted it into the code between the selection line and the Text To Columns line and all it seems to do is hang the computer like its in a continuous loop. I End Task it and it says the computer is at 100% usage. Why? I don't understand the relevance of the Convert To Text code as the values are surely already text, I am trying to end up with them in date format...

Help!
 
Ok Ive found out the probable cause for both the actual problem error and the reason why the computer hangs. The code is disregarding the selection of just the G column and instead is selecting the whole spreadsheet. Seeing as you can only run Convert To Text on one column this makes the error appear. Your function is not working as this relies on adding quotes for just the one column but instead it's adding quotes round every column and row in the spreadsheet which seems to go on forever...any idea why on earth it is not storing the selection of just column G? I have tried using &quot;G&quot;, &quot;G:G&quot; and 7.
 
Obviously I wasn't clear enough. Sometimes we make big assumptions. Here is my recommendation. You already have selected column G above so the Convert_To_Text Excel macro should work OK. To create your own set of macros for Excel insert the function in a new module as a public function, save the file with a .xla extension and place in in the XLSTART directory.

Change:
'Run Text To Columns on it to fix dates
oxlApp.Selection.TextToColumns Destination:=Range(&quot;G1&quot;), DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False, space:=False, Other:=False, FieldInfo:=Array(1, 1)

To:
'Run Text To Columns on it to fix dates
oxlApp.Run(Convert_To_Code)


Create New Function as a Macro in Excel.
Public Function Convert_To_Code
Dim xCell As Variant
For Each xCell In Selection
If Left$(xCell.Value, 1) <> &quot;'&quot; Then
xCell.Value = &quot;'&quot; & Trim(xCell.Value)
Debug.Print xCell.Value
End If
Next xCell
End Function

-------------------------------------
scking@arinc.com
Try to resolve problems independently
Then seek help among peers or experts
But TEST recommended solutions
-------------------------------------
 
Isn't there any way of doing it solely inside Access?? I do not have the option of saving Excel macros permanently...

Nothing seems to work. This is so difficult to solve!
 
What don't you have the option of saving Excel macros? What operating system are you working with? In Win 2000 you would merely save a macro in an Excel file and save the file with a .xla extension in your own personal XLSTART directory. In the following I've named my file MacroLib.xla and it will be loaded each time Excel is opened. I would have to research Win 95 or Win NT.

C:\Documents and Settings\scking\Application Data\Microsoft\Excel\XLSTART\MacroLib.xla
-------------------------------------
scking@arinc.com
Try to resolve problems independently
Then seek help among peers or experts
But TEST recommended solutions
-------------------------------------
 
I don't have the option because I am on a networked PC where most directories are locked and controlled and besides that the database that exports the Excel documents is for use by a whole team of people not just myself.

I tried putting the macro code into Access making changes where applicable (&quot;Selection&quot; becomes &quot;oxlApp.Selection&quot;). However then it seems to run very slow in adding the quotation marks and basically hangs the computer.

I can't understand why using the code to run Text To Columns doesn't work right in Access...any ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top