Dale Fye
asked on
Formatting Excel spreadsheet via Access VBA
I am exporting data from Access to Excel via automation. I use the CopyFromRecordset method to dump a bunch of data into a range of cells.
I then loop through the recordset and add the fieldnames in the 3rd row of each column. I then want to select that cell and rotate the text by 90 degrees (clockwise). I recorded a macro in Excel and it returned the following code.
But I need to do this for somewhere between 30 and 75 columns (depending on data filters). In the rest of the code, I have a column counter (intCol), which I use along with the Cells( row, col) property of the worksheet object to set and manipulate cell values. But when I tried:
sht.cells(3, intCol).Select
instead of:
Range("B3").Select
the line of code which reads:
.Horizontal Alignment = xlCenter
generates an "Object variable or With block variable not set" error. How should I modify this code to ensure that I can reformat the cell.
I then loop through the recordset and add the fieldnames in the 3rd row of each column. I then want to select that cell and rotate the text by 90 degrees (clockwise). I recorded a macro in Excel and it returned the following code.
Range("B3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = -90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.ColumnWidth = 5
End With
But I need to do this for somewhere between 30 and 75 columns (depending on data filters). In the rest of the code, I have a column counter (intCol), which I use along with the Cells( row, col) property of the worksheet object to set and manipulate cell values. But when I tried:
sht.cells(3, intCol).Select
instead of:
Range("B3").Select
the line of code which reads:
.Horizontal Alignment = xlCenter
generates an "Object variable or With block variable not set" error. How should I modify this code to ensure that I can reformat the cell.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Exactly what I was looking for.
Open in new window
xlObj.QuitSet xlObj = Nothing