Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 30th, 2004, 05:00 PM
mls mls is offline
Registered User
Join Date: Dec 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Autofit Merged cells

I'm creating a report in Excel from VB6, does anyone know if you can autofit merged cells? If so, please send an example my way.



Old April 29th, 2006, 08:26 PM
Registered User
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Here's some code that does the trick. It first sets the width of the first column to the total of the merged cells, pastes the text into autofit set cells, then removes the autofit and marges the columns as required.

TWidth = 0: ROffset = 100 ' ROffset sets othe start row of the pasted text.
For Each col In Columns("A:E") ' get total width of cells to be merged
    TWidth = TWidth + col.ColumnWidth
    Next col
OldWidth = Columns("A").ColumnWidth
Columns("A").ColumnWidth = TWidth
Range("A1:A50").Offset(ROffset, 0).WrapText = True
Range("A1:A50").Offset(ROffset, 0).Rows.AutoFit
ThisWorkbook.Sheets("Reference Text").Range("A1:A50").Copy Destination:=Range("A1").Offset(ROffset, 0)
For Each rw In Selection.Rows ' set row heights to match auto fit & remove autofit
    rw.RowHeight = rw.RowHeight
Next rw
Range("A1").ColumnWidth = OldWidth ' restore first column's width
For i = 0 To 50: Range("A1:E1").Offset(ROffset + i, 0).MergeCells = True: Next i ' merge target cells

Similar Threads
Thread Thread Starter Forum Replies Last Post
Format Merged Lines edove XSLT 2 August 7th, 2007 05:25 PM
Merged cells when exporting a report to excel bea Reporting Services 0 June 22nd, 2007 09:18 AM
XSLT help: First instance of a node in merged file jftaylor XSLT 2 August 17th, 2006 09:23 PM
split cells up if merged crmpicco Excel VBA 4 May 4th, 2005 10:25 AM
Wrap text with merged cells aspadda Excel VBA 4 January 29th, 2004 03:02 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.