Using Autofit on Multiple Rows

Yabiman

New Member
Joined
Aug 25, 2010
Messages
8
This is a follow up to a thread I followed in "How to make a macro run each time a specific cell value changes." That thread helped me understand how to get a macro to execute when a particular cell was changed, but the macro itself does not do what it is supposed to do.

The workbook I am developing has one sheet (sheet A) that contains a list of scheduled events or appointments. A second sheet (sheet B) draws information from Sheet A to produce a report of appointments for any given day. Cell B10 in sheet B allows the user to enter a date for the report. Formulas in sheet B match the date in B10 to entries in sheet A and list all the appointments that match that date. For lines where the date does not match, the formulas display blanks. Worksheet A also sorts these appointments by family group, based on a sort key field.

Worksheet A lists appointments for children with dates and times. Since the children in each family would be seen together, I want to have a blank line between each family group in Worksheet B. The result should be a page that lists a series of appointments, with the members of each family group single spaced, and a blank line between each family group.

To accomplish this, I included a conditional CHAR(10) (linefeed) character in the formula in Worksheet B. When the family identifier of the line does not match the identifier for the line above, a linefeed is included. This creates the extra space between lines. Based on information I've seen in other forums, I turned word wrap on so that the CHAR(10) will force an extra line in the cell. The formula for that cell is:

=IF(A16=A15,"",IF(AND(INDEX(Data,$K$10+K15,1)=$B$10,INDEX(Data,$K$10+K15,3)>0.5),""&CHAR(10)&A16&".","")).​

The last part of the formula inserts a null, a line feed and an item number followed by a period.

I am trying to get the sheet to execute an auto row height so that the row height will increase for those rows containing the extra linefeed, and shrink for those rows without the extra linefeed. If a different date is selected in B10 and a different grouping of children results, the change in the date should trigger the macro to autofit to the new data. I have been able to confirm that the technique I got from the early forum does result in triggering the macro, but I haven't been able to get the macro to work correctly. Actually, the problem appears to be with the autofit command itself, because if I use the menu selection Home|Format|Autofit Row Height on multiple rows, it doesn't work either. If I apply it to an individual row, it does work. Likewise, if I modify my macro to work only on one row, it works, but if I apply it to a range of rows, it doesn't. I have scoured various forums to find techniques. It appears that I may have to use a For ... Next or Do ... Until loop to autofit the rows one at a time, but this will slow down the execution. Does anyone have suggestions? By the way, I've seen many posts that indicate that trying to use Autofit with merged cells does not work, the rows I am operating on do not include any merged cells.

To trigger the automatic action, I use the following VBA code in worksheet B's module:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$10" Then

Application.Run "Row_Height"

End If
End Sub​

Here is the Row_Height Macro (it's in a different module, because if I get this working, it will be called by several different report worksheets.)

Sub Row_Height()
' Adjust Row Height Macro

ActiveWorkbook.Rows("15:43").AutoFit

End Sub​
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hello

I'm not sure that i understand but, maybe this, please test the below on a copy of your data.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Row_Height()<br><SPAN style="color:#007F00">' Adjust Row Height Macro</SPAN><br><br>ActiveSheet.Rows("15:43").WrapText = <SPAN style="color:#00007F">True</SPAN><br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Turns out that there is some data in other columns that needs to not wrap. Can I use a similar technique on the same range of rows, except autofit based only on the contents of column B? I tried substituting

Code:
ActiveSheet.Rows("B16:B45").WrapText = True

and

Code:
ActiveSheet.Cells("B16:B45").WrapText = True

but I got error messages for both.

Thanks!
 
Upvote 0
No sorry, you can only AutoFit or WrapText, entire columns or rows, not individual cells.
 
Upvote 0
Thank you for your help. I am able to work around the problem caused by information in the other columns and so your solution works. I did learn when I transferred the macro from my practice copy of the workbook to the working copy that in order to get to work, I had to format the entire range using the Home|Format|Autofit Row Height command. Also (for anyone who sees this later), I had to play with my vertical alignment. After I did it once on each sheet, the macro worked well.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top