_ Author Debra Dalgleish Posted on JApCategories Excel VBA Is this code, to AutoFit merged cell row height, something that you’ll use in your workbooks? Please let me know in the comments. The row height should adjust automatically. To test the code, make a change to the text in the named merged cells, then press Enter. Screen updating is turned off while the code runs, and it all happens in the blink of an eye. Change the first column to its original width.Autofit the row, based on the note next in the first column.Set the first column to the calculated total width.Add a little extra to the calculated width.Get the total width for all columns in the OrderNote range.Get the width of the first column in the OrderNote range.If it is, the code runs, and does the following: The event code checks to see if the changed cell is in the OrderNote range. Set AutoFitRng = Range(Range(str01).MergeArea.Address) If Not Intersect(Target, Range(str01)) Is Nothing Then Private Sub Worksheet_Change(ByVal Target As Range) If your worksheet is protected, you can add code to unprotect and protect the worksheet.Change the range name from “OrderNote”, to the named range on your worksheet.Note: Only one Worksheet_Change event is allowed in each worksheet module. Right-click on the sheet tab, and paste the following code on the worksheet module.So, instead of using the Worksheet_Change event, you could use the workbook’s BeforePrint event, to reduce the Undo problem. Note: As Jeff Weir pointed out in the comments below, this code will wipe out the Undo stack, so you won’t be able to undo any steps you’ve previously taken. The code that I use is based on an old Excel newsgroup example, that was posted by Excel MVP, Jim Rech. We want the row height to adjust if the OrderNote range is changed, so we’ll add code to the Worksheet_Change event. The merged cells are named OrderNote, and that name will be referenced in the event code. To fix the worksheet, so the merged cells adjust automatically, you can add event code to the worksheet. That works well, as long as you remember to do it, but it can be a nuisance, if the text changes frequently.Īnd if you forget to adjust the row height, you might print the order form, while key instructions are hidden. When the cells are merged in row 10, the row height has to be manually adjusted when the text changes. Usually, if you add more text to a single cell, and Wrap Text is turned on, the row height automatically adjusts, to fit the text. Adjusting the column width would affect the product list that starts in row 12, so that’s not an option. However, if the notes will be two or more lines, you’ll need to merge the cells, and turn on Wrap Text. If the note will always be short, there’s no need to merge the cells – just let the text flow across the columns. In the example shown below, there is an order form, and space for a note about the order. As long as you avoid merging table cells, and proceed with caution, things might be okay. Occasionally though, you might have no choice but to use one or more merged cells on a worksheet. You’ll run into more problems if you try to autofit merged cell row height. Merged cells can cause problems, especially when they’re in a table that you’ll be sorting and filtering. You’ve most likely heard this warning - “Avoid merged cells in your Excel worksheets!”, and that is excellent advice.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |