Access vba to automate Excel

AbsoluteAccess

New member
Local time
Tomorrow, 06:43
Joined
Feb 12, 2013
Messages
9
I am exporting data to a new Excel spreadsheet and everything is working ok except for the formatting on my first row. I am setting the orientation to vertical and am not having any luck with getting the text spacing correct. It is too big. This is my code:

Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim xlrange As Excel.Range

Set xlapp = CreateObject("Excel.Application")
xlapp.Visible = True
'Create new workbook
Set xlbook = xlapp.Workbooks.Add
Set xlsheet = xlbook.ActiveSheet

'Set Header
xlsheet.PageSetup.Orientation = xlLandscape
xlapp.ActiveWindow.View = xlPageLayoutView

'Set Headings
xlsheet.Cells(1, 1) = "Scope type"
xlsheet.Cells(1, 2) = "Street No."
xlsheet.Cells(1, 3) = "Street Name"
xlsheet.Cells(1, 4) = "MP Number"

'Set column widths
Set xlrange = xlsheet.Range("A1:D1")
With xlrange
.HorizontalAlignment = xlGeneral (This line not working)
.VerticalAlignment = xlBottom (This line not working)
.Orientation = xlVertical
End With
xlsheet.Columns(1).ColumnWidth = 4
xlsheet.Columns(2).ColumnWidth = 4

I have tried changing the constants in the relevant lines to the matching numeric values as well as to Excel.Constants.xlGeneral etc

I have also tried selecting the range before applying the code but nothing seems to be working.

If anyone can offer any assistance, I would greatly appreciate it. I'm using Access 2010.
 
Try:
'Set column widths
Set xlrange = xlsheet.Range("A1")
 
Thanks for the reply but I don't think the column widths has anything to do with my problem. My problem is that in the first row, I have the text displaying vertically.

The spacing between each letter is quite wide so instead of the row being around 2-3 cm in height, it is 5-6cm in height. In Excel, I can manually change the row by setting the horizontal and vertical alignment but when I do the same thing with VBA from Access, nothing is happening.

Anyone else have any clues?
 
I'm confused over what you want, can you take a screenshot to show which part?
 
Weirdest thing. After fighting with this for a good part of the day and getting nowhere, it seems to have fixed itself. I hate it when that happens because it's always nice to know you fixed it and it will work unconditionally. Thanks anyway.
 
Thanks for the reply but I don't think the column widths has anything to do with my problem.
Good you have solved you problem.
It was you own comments in the code: "'Set column widths"

I only suggested: Set xlrange = xlsheet.Range("A1") instead of Set xlrange = xlsheet.Range("A1
biggrin.gif
1") :)
 

Users who are viewing this thread

Back
Top Bottom