Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Autofit on Merged Cells.

I am writing a program in VB.NET that populates an Excel spreadsheet.
I know that it is not possible to have autoheight or autofit on merged
cells. I need the height of the row to expand to fit an undetermined
amount of text.

I found some code previously posted and changed the code to fit my
program. Now, I get an error on this line of code (full code below):
For Each CurrCell In CoverWs.Range(strA1).MergeArea

I believe the error is in the CurrCell as Excel.Range, but I'm not
sure.

And advice would be appreciated,
Thanks,
Elena


Error:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in mscorlib.dll

Additional information: Member not found.


Current Code:
Dim CurrentRowHeight As Single, MergedCellRgWidth As
Single
Dim CurrCell As Excel.Range
Dim ActiveCellWidth As Single, PossNewRowHeight As
Single

If CoverWs.Range(strA1).MergeCells Then
With CoverWs.Range(strA1).MergeArea
If .Rows.Count = 1 And .WrapText = True Then
ThisApplication.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth =
CoverWs.Range(strA1).ColumnWidth
For Each CurrCell In
CoverWs.Range(strA1).MergeArea
MergedCellRgWidth =
CurrCell.ColumnWidth + _

MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit()
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight
PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Autofit on Merged Cells.

Hi Elena,

I can't see an obvious reason why the code would fail assuming
- CoverWs is referenced to a Worksheet
- strA1 is a valid address of a single cell
- no protection

Just glancing at the code I'm not sure the function will do quite what you
want. If .WrapText the cell(1) has probably autofitted the row height, so
when you come to autofit the column width it wouldn't reset to a single line
of text in the cell. But I might be missing something either in the code or
your overall objective.

Why not try it all in VBA, get it working as you want and without error
before re-adapting to .Net, and 'catch' any further errors.

Regards,
Peter T

wrote in message
ups.com...
I am writing a program in VB.NET that populates an Excel spreadsheet.
I know that it is not possible to have autoheight or autofit on merged
cells. I need the height of the row to expand to fit an undetermined
amount of text.

I found some code previously posted and changed the code to fit my
program. Now, I get an error on this line of code (full code below):
For Each CurrCell In CoverWs.Range(strA1).MergeArea

I believe the error is in the CurrCell as Excel.Range, but I'm not
sure.

And advice would be appreciated,
Thanks,
Elena


Error:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in mscorlib.dll

Additional information: Member not found.


Current Code:
Dim CurrentRowHeight As Single, MergedCellRgWidth As
Single
Dim CurrCell As Excel.Range
Dim ActiveCellWidth As Single, PossNewRowHeight As
Single

If CoverWs.Range(strA1).MergeCells Then
With CoverWs.Range(strA1).MergeArea
If .Rows.Count = 1 And .WrapText = True Then
ThisApplication.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth =
CoverWs.Range(strA1).ColumnWidth
For Each CurrCell In
CoverWs.Range(strA1).MergeArea
MergedCellRgWidth =
CurrCell.ColumnWidth + _

MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit()
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight
PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Autofit on Merged Cells.

I found some code previously posted and changed the code to fit my
program.

The code was that of Jim Rech's which is pretty well proven.

--
Regards,
Tom Ogilvy

"Peter T" <peter_t@discussions wrote in message
...
Hi Elena,

I can't see an obvious reason why the code would fail assuming
- CoverWs is referenced to a Worksheet
- strA1 is a valid address of a single cell
- no protection

Just glancing at the code I'm not sure the function will do quite what you
want. If .WrapText the cell(1) has probably autofitted the row height, so
when you come to autofit the column width it wouldn't reset to a single

line
of text in the cell. But I might be missing something either in the code

or
your overall objective.

Why not try it all in VBA, get it working as you want and without error
before re-adapting to .Net, and 'catch' any further errors.

Regards,
Peter T

wrote in message
ups.com...
I am writing a program in VB.NET that populates an Excel spreadsheet.
I know that it is not possible to have autoheight or autofit on merged
cells. I need the height of the row to expand to fit an undetermined
amount of text.

I found some code previously posted and changed the code to fit my
program. Now, I get an error on this line of code (full code below):
For Each CurrCell In CoverWs.Range(strA1).MergeArea

I believe the error is in the CurrCell as Excel.Range, but I'm not
sure.

And advice would be appreciated,
Thanks,
Elena


Error:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in mscorlib.dll

Additional information: Member not found.


Current Code:
Dim CurrentRowHeight As Single, MergedCellRgWidth As
Single
Dim CurrCell As Excel.Range
Dim ActiveCellWidth As Single, PossNewRowHeight As
Single

If CoverWs.Range(strA1).MergeCells Then
With CoverWs.Range(strA1).MergeArea
If .Rows.Count = 1 And .WrapText = True Then
ThisApplication.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth =
CoverWs.Range(strA1).ColumnWidth
For Each CurrCell In
CoverWs.Range(strA1).MergeArea
MergedCellRgWidth =
CurrCell.ColumnWidth + _

MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit()
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight
PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Autofit on Merged Cells.

I misinterpreted the objective and should have recognized the code as Jim
Rech's.

Elena, there is indeed an error/typo in the code you posted, change
.EntireRow.AutoFit()

to
..EntireRow.AutoFit

I see you are disabling Screenupdating. I assume you ensure it is reset
somewhere, either further down in the routine or in a calling routine.

Regards,
Peter T

"Tom Ogilvy" wrote in message
...
I found some code previously posted and changed the code to fit my

program.

The code was that of Jim Rech's which is pretty well proven.

--
Regards,
Tom Ogilvy

"Peter T" <peter_t@discussions wrote in message
...
Hi Elena,

I can't see an obvious reason why the code would fail assuming
- CoverWs is referenced to a Worksheet
- strA1 is a valid address of a single cell
- no protection

Just glancing at the code I'm not sure the function will do quite what

you
want. If .WrapText the cell(1) has probably autofitted the row height,

so
when you come to autofit the column width it wouldn't reset to a single

line
of text in the cell. But I might be missing something either in the code

or
your overall objective.

Why not try it all in VBA, get it working as you want and without error
before re-adapting to .Net, and 'catch' any further errors.

Regards,
Peter T

wrote in message
ups.com...
I am writing a program in VB.NET that populates an Excel spreadsheet.
I know that it is not possible to have autoheight or autofit on merged
cells. I need the height of the row to expand to fit an undetermined
amount of text.

I found some code previously posted and changed the code to fit my
program. Now, I get an error on this line of code (full code below):
For Each CurrCell In CoverWs.Range(strA1).MergeArea

I believe the error is in the CurrCell as Excel.Range, but I'm not
sure.

And advice would be appreciated,
Thanks,
Elena


Error:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in mscorlib.dll

Additional information: Member not found.


Current Code:
Dim CurrentRowHeight As Single, MergedCellRgWidth As
Single
Dim CurrCell As Excel.Range
Dim ActiveCellWidth As Single, PossNewRowHeight As
Single

If CoverWs.Range(strA1).MergeCells Then
With CoverWs.Range(strA1).MergeArea
If .Rows.Count = 1 And .WrapText = True Then
ThisApplication.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth =
CoverWs.Range(strA1).ColumnWidth
For Each CurrCell In
CoverWs.Range(strA1).MergeArea
MergedCellRgWidth =
CurrCell.ColumnWidth + _

MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit()
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight
PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Autofit on Merged Cells.

Hi Gentlemen,

The () after the autofit is automatic to the VB.NET. If I remove the
() it adds it back in automatically.

I know the code is well proven, but for VBA. I'm trying to adapt it
for VB.NET.

One thing... strA is a valid cell... it's "a" & whatever row. Yet, it
is after a merge. So the code overlaps cells a:j.

I'll try it in VBA.

The error is focusing on the CurrCell. Do I have to define the range
at some point? Is there a step I'm missing?

Tom, you helped me with this code originally in the msdn site. I
posted a reply, but one has replied.

Thanks so much for you help!
~Elena







Peter T wrote:
I misinterpreted the objective and should have recognized the code as Jim
Rech's.

Elena, there is indeed an error/typo in the code you posted, change
.EntireRow.AutoFit()

to
.EntireRow.AutoFit

I see you are disabling Screenupdating. I assume you ensure it is reset
somewhere, either further down in the routine or in a calling routine.

Regards,
Peter T

"Tom Ogilvy" wrote in message
...
I found some code previously posted and changed the code to fit my

program.

The code was that of Jim Rech's which is pretty well proven.

--
Regards,
Tom Ogilvy

"Peter T" <peter_t@discussions wrote in message
...
Hi Elena,

I can't see an obvious reason why the code would fail assuming
- CoverWs is referenced to a Worksheet
- strA1 is a valid address of a single cell
- no protection

Just glancing at the code I'm not sure the function will do quite what

you
want. If .WrapText the cell(1) has probably autofitted the row height,

so
when you come to autofit the column width it wouldn't reset to a single

line
of text in the cell. But I might be missing something either in the code

or
your overall objective.

Why not try it all in VBA, get it working as you want and without error
before re-adapting to .Net, and 'catch' any further errors.

Regards,
Peter T

wrote in message
ups.com...
I am writing a program in VB.NET that populates an Excel spreadsheet.
I know that it is not possible to have autoheight or autofit on merged
cells. I need the height of the row to expand to fit an undetermined
amount of text.

I found some code previously posted and changed the code to fit my
program. Now, I get an error on this line of code (full code below):
For Each CurrCell In CoverWs.Range(strA1).MergeArea

I believe the error is in the CurrCell as Excel.Range, but I'm not
sure.

And advice would be appreciated,
Thanks,
Elena


Error:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in mscorlib.dll

Additional information: Member not found.


Current Code:
Dim CurrentRowHeight As Single, MergedCellRgWidth As
Single
Dim CurrCell As Excel.Range
Dim ActiveCellWidth As Single, PossNewRowHeight As
Single

If CoverWs.Range(strA1).MergeCells Then
With CoverWs.Range(strA1).MergeArea
If .Rows.Count = 1 And .WrapText = True Then
ThisApplication.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth =
CoverWs.Range(strA1).ColumnWidth
For Each CurrCell In
CoverWs.Range(strA1).MergeArea
MergedCellRgWidth =
CurrCell.ColumnWidth + _

MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit()
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight
PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Autofit on Merged Cells.

It works perfectly in VBA. I just have to find a way to adapt it to
VB.NET that calls Excel.


Elena wrote:
Hi Gentlemen,

The () after the autofit is automatic to the VB.NET. If I remove the
() it adds it back in automatically.

I know the code is well proven, but for VBA. I'm trying to adapt it
for VB.NET.

One thing... strA is a valid cell... it's "a" & whatever row. Yet, it
is after a merge. So the code overlaps cells a:j.

I'll try it in VBA.

The error is focusing on the CurrCell. Do I have to define the range
at some point? Is there a step I'm missing?

Tom, you helped me with this code originally in the msdn site. I
posted a reply, but one has replied.

Thanks so much for you help!
~Elena







Peter T wrote:
I misinterpreted the objective and should have recognized the code as Jim
Rech's.

Elena, there is indeed an error/typo in the code you posted, change
.EntireRow.AutoFit()

to
.EntireRow.AutoFit

I see you are disabling Screenupdating. I assume you ensure it is reset
somewhere, either further down in the routine or in a calling routine.

Regards,
Peter T

"Tom Ogilvy" wrote in message
...
I found some code previously posted and changed the code to fit my
program.

The code was that of Jim Rech's which is pretty well proven.

--
Regards,
Tom Ogilvy

"Peter T" <peter_t@discussions wrote in message
...
Hi Elena,

I can't see an obvious reason why the code would fail assuming
- CoverWs is referenced to a Worksheet
- strA1 is a valid address of a single cell
- no protection

Just glancing at the code I'm not sure the function will do quite what

you
want. If .WrapText the cell(1) has probably autofitted the row height,

so
when you come to autofit the column width it wouldn't reset to a single
line
of text in the cell. But I might be missing something either in the code
or
your overall objective.

Why not try it all in VBA, get it working as you want and without error
before re-adapting to .Net, and 'catch' any further errors.

Regards,
Peter T

wrote in message
ups.com...
I am writing a program in VB.NET that populates an Excel spreadsheet.
I know that it is not possible to have autoheight or autofit on merged
cells. I need the height of the row to expand to fit an undetermined
amount of text.

I found some code previously posted and changed the code to fit my
program. Now, I get an error on this line of code (full code below):
For Each CurrCell In CoverWs.Range(strA1).MergeArea

I believe the error is in the CurrCell as Excel.Range, but I'm not
sure.

And advice would be appreciated,
Thanks,
Elena


Error:
An unhandled exception of type
'System.Runtime.InteropServices.COMException'
occurred in mscorlib.dll

Additional information: Member not found.


Current Code:
Dim CurrentRowHeight As Single, MergedCellRgWidth As
Single
Dim CurrCell As Excel.Range
Dim ActiveCellWidth As Single, PossNewRowHeight As
Single

If CoverWs.Range(strA1).MergeCells Then
With CoverWs.Range(strA1).MergeArea
If .Rows.Count = 1 And .WrapText = True Then
ThisApplication.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth =
CoverWs.Range(strA1).ColumnWidth
For Each CurrCell In
CoverWs.Range(strA1).MergeArea
MergedCellRgWidth =
CurrCell.ColumnWidth + _

MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit()
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight
PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Autofit image in merged cells LSG Excel Discussion (Misc queries) 1 January 3rd 08 03:01 PM
Autofit Merged cell Code is changing the format of my merged cells JB Excel Discussion (Misc queries) 0 August 20th 07 02:12 PM
Is there a way to Autofit Merged Cells in a row? JLSmith Excel Discussion (Misc queries) 2 August 1st 06 04:49 PM
Row Autofit on Merged Cells Jluo Excel Discussion (Misc queries) 1 April 18th 05 02:37 PM
Autofit in Merged Cells? Mick Excel Discussion (Misc queries) 4 February 14th 05 05:15 PM


All times are GMT +1. The time now is 02:47 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"