Need help understandig why something works in VBA, and something else doesn't

Vantom

Board Regular
Joined
Feb 28, 2014
Messages
65
These work:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With ActiveSheet.CommandButton1
        .Top = ActiveWindow.ScrollRow * [COLOR=#ff0000]Rows(ActiveWindow.ScrollRow).Height[/COLOR] - [COLOR=#ff0000]14[/COLOR]
    End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim intCellHeight As Integer: intCellHeight = Rows(ActiveWindow.ScrollRow).Height
        With ActiveSheet.CommandButton1
            .Top = ActiveWindow.ScrollRow * [COLOR=#ff0000]Rows(ActiveWindow.ScrollRow).Height[/COLOR] - [COLOR=#ff0000]intCellHeight[/COLOR]
        End With
End Sub
These does not:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With ActiveSheet.CommandButton1
        .Top = ActiveWindow.ScrollRow * [COLOR=#ff0000]14 [/COLOR]- [COLOR=#ff0000]14[/COLOR]
    End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim intCellHeight As Integer: intCellHeight = Rows(ActiveWindow.ScrollRow).Height
        With ActiveSheet.CommandButton1
            .Top = ActiveWindow.ScrollRow * [COLOR=#ff0000]intCellHeight [/COLOR]- [COLOR=#ff0000]14[/COLOR]
        End With
End Sub
Why is that?
 
Last edited:
Yes, your inititial method was actually still wrong..Barely, just not really noticeable
.Top = ActiveWindow.ScrollRow * Rows(ActiveWindow.ScrollRow).Height - 14

I suggest the most accurate way would be
.Top = ActiveWindow.ScrollRow * Rows(ActiveWindow.ScrollRow).Height - Rows(ActiveWindow.ScrollRow).Height

Or use a variable
MyHeight = Rows(ActiveWindow.ScrollRow).Height
.Top = ActiveWindow.ScrollRow * MyHeight - MyHeight
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
This method is still not bullet proof, because it assumes all rows are the same height.

If you have just 1 row that is of a different height, then it is thrown off.
I would look for other methods of positioning your command button.
 
Upvote 0
'VisibleRange.Top' will get the top, regardless of various row heights etc?
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        With ActiveSheet.CommandButton1
            .Top = Windows(1).VisibleRange.Top 
        End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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