Row Height when Exporting to Excel

T

TADropik

I'm able to export to excel through VBA code with out any problem.

If I write to a cell and the data has no Chr(10) in it, everything is fine:
objWS.Cells(1, 1) = "123 Main Street"

I i write to a cell and the data has Chr(10)'s in it, the Row Height ends up
to big:
objWS.Cells(1, 1) = "123 Main Street" & Chr(10) & _
"Orlando, FL" & Chr(10) & _
"USA"
I can go to the cell, press F2, then enter and it auto fits the Row Height.

Is there a way I can keep the Row Height Auto fitted to the data?
 
A

AlexM

xlApp.Cells.Select
xlApp.Cells.EntireRow.AutoFit
xlApp.Cells.EntireColumn.AutoFit
xlApp.Rows("2:2").Select

Use the object of your Excel.Application instead of xlApp.
 
T

TADropik

Still didn't work. (below is the code)

Dim objExcel As Excel.Application

objExcel.Cells.Select
objExcel.Cells.EntireRow.AutoFit

However, when a create a Macro in the Excel spreadsheet and run it, it
works. (below is the macro)

' Macro1 Macro
' Macro recorded 2/9/2009 by Me...
'
Cells.Select
Cells.EntireRow.AutoFit
 
T

TADropik

Still didn't work (see code sample below)

Dim objExcel As Excel.Application
objExcel.Cells.Select
objExcel.Cells.EntireRow.AutoFit

When I create a Macro in the Excel spreadsheet and run it, it works (see
macro below)

' Macro1 Macro
' Macro recorded 2/9/2009 by Me
'
Cells.Select
Cells.EntireRow.AutoFit
 
T

TADropik

Still didn't work (see code sample below)

Dim objExcel As Excel.Application
objExcel.Cells.Select
objExcel.Cells.EntireRow.AutoFit

When I create a Macro in the Excel spreadsheet and run it, it works (see
macro below)

' Macro1 Macro
' Macro recorded 2/9/2009 by Me
'
Cells.Select
Cells.EntireRow.AutoFit
 
R

RoyVidar

TADropik said:
I'm able to export to excel through VBA code with out any problem.

If I write to a cell and the data has no Chr(10) in it, everything is
fine: objWS.Cells(1, 1) = "123 Main Street"

I i write to a cell and the data has Chr(10)'s in it, the Row Height
ends up to big:
objWS.Cells(1, 1) = "123 Main Street" & Chr(10) & _
"Orlando, FL" & Chr(10) & _
"USA"
I can go to the cell, press F2, then enter and it auto fits the Row
Height.

Is there a way I can keep the Row Height Auto fitted to the data?

After entering the information, on the same cell, try AlexM's
suggestion

objWS.Cells(1, 1) = "123 Main Street" & Chr(10) & _
"Orlando, FL" & Chr(10) & _
"USA"
objWS.Cells(1, 1).EntireColumn.AutoFit

or

With objWS.Cells(1, 1)
.Value = "123 Main Street" & Chr(10) & _
"Orlando, FL" & Chr(10) & _
"USA"
.EntireColumn.AutoFit
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

setting row height not to exceed... 1
address in one line 4
Adjusting height of text cells 3
faster export to excel? 7
Minimum row height 8
Autofit Row Height 4
Excel autofit row height not working 0
Excel VBA For excel 0

Top