Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
Password Reminder
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.



Reply With Quote
  #2 (permalink)  
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

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

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

All times are GMT -4. The time now is 02:11 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.