简体   繁体   中英

VBA Excel row height to the last row

This is the query strongly related to this one:

Setting RowHeight Excel VBA

but when I modify it a bit (as I want the row height = 15 only).

 Dim ws As Worksheet
 Set ws = ActiveSheet

 Dim Rng As Range
 Dim cel As Range
 Set Rng = Range(ActiveCell, Cells(Rows.Count, ActiveCell.Column).End(xlUp)) '& lastRow

 For Each cel In Rng
 cel.Rows.AutoFit
 cel.Rows.RowHeight = 15
Next cel

I am getting the result in the place, where the cell has been clicked (active cell).

I tried to put the & lastRow straight after the .End(xlUp), but it didn't work, unfortunately.

The situation looks as follows:

在此处输入图片说明

Only the first 5 rows have been adjusted (where the cell was initially activated), but I need all of them down to the end. The value it's going to be flexible in the future.

How can I do that?

In set rng you specify activecell which is causing the problem. Also you have xlUp which is, as you can see here, formatting the cells upwards of your selection. Does this work for you?

 Sub test()
    ActiveSheet.UsedRange.Rows.AutoFit
    ActiveSheet.UsedRange.Rows.RowHeight = 15
 End Sub

Row height applies to the whole row so the column selected is irrelevant.

Also there's no need to loop.

Sub x()

Dim Rng As Range

Set Rng = Range("S1", Cells(Rows.Count, "S").End(xlUp))

rng.EntireRow.AutoFit 
rng.EntireRow.RowHeight = 15   

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM