Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelectionChange

Status
Not open for further replies.

Robert1688

Agricultural
Jan 13, 2007
14
I've made the following to autofit the row of the cell I selected, but I want it to be the origin row height after another cell is selected.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.EntireRow.AutoFit
End Sub
 
Replies continue below

Recommended for you

Hi Robert:

Is Origin a fixed range, such as cell C3? ... if so then let us try ...
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Set Origin = Range("C3")
    Origin.EntireRow.AutoFit
End Sub

Yogi Anand, D.Eng, P.E.
Energy Efficient Building Network LLC
ANAND Enterprises LLC
 
Sorry, I did not describe it clearly.
Like this:
I select C3, the 3rd row becames autofit, I can read the whole data in this row.
And then, I select D4, then the 3rd row will be back to its origin height, like 12.75, and the 4th row becames autofit.....
 
You mean like:

Code:
Dim OldHt As Double
Dim NewHt As Double
Dim OldSel As Range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If (Target.Row <> OldSel.Row) And (Target.Rows.Count = 1) Then
    
    If Not OldSel Is Nothing Then
        OldSel.RowHeight = OldHt
    End If

    Set OldSel = Target
    OldHt = Target.EntireRow.Height
    
    Target.EntireRow.AutoFit
    
End If
End Sub
 
Thanks, handleman

As I run your codes, it says "Object variable or With block variable not set", it may points to OldSel.

I've tryed this kind of method before, set Oldsel as Target at end of program.
 
I've got it solved:

Public ff, tt
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
Rows(ff).RowHeight = tt
ff = Target.Row
tt = Target.RowHeight
Target.EntireRow.AutoFit
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor