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:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Not sure about your question. Here is a script I like to use.
Keeps my command button near the activecell.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ans As String
ans = "CommandButton1"
If Target.Cells.Count > 1 Then Exit Sub
ActiveSheet.Shapes.Range(ans).Top = ActiveCell.Top
ActiveSheet.Shapes.Range(ans).Left = ActiveCell.Offset(, 2).Left
End Sub
 
Upvote 0
That type of code in the OP can be perilous. First of all, the ScrollRow returns the integer value of the first visible row at the top of the monitor screen. So generally speaking, to use it properly, the programmer would need to know which row that happened to be at the time it is applied in the code. Secondly, the Rows(...).Height also returns an integer value and the programmer would need to know that value as well when applying it in code. But in this case, it appears that the code that works is simply trying to position a control at the top of the screen as the user scrolls up and down by simply moving the control relative to the first visible row on the screen. However, the code that does not work is attempting to use math calculation versus relative positioning.
 
Last edited:
Upvote 0
I wonder why the calculations(the red values) does not work when using integer/variable?
 
Upvote 0
'math calculation versus relative positioning' aren't they all numbers being multiplied?
 
Upvote 0
I wonder why the calculations(the red values) does not work when using integer/variable?
Because your integer value does not automatically change relative to the rows displayed on the screen. See Post #3
 
Upvote 0
Code:
ActiveWindow.ScrollRow * [COLOR=#ff0000]Rows(ActiveWindow.ScrollRow).Height[/COLOR]
This multiplies the row number with 'Rows(ActiveWindow.ScrollRow).Height'(14)?
Code:
ActiveWindow.ScrollRow * [COLOR=#ff0000]intCellHeight [/COLOR]
This multiplies the row number with 'intCellHeight'(14)?
Code:
ActiveWindow.ScrollRow * [COLOR=#ff0000]14[/COLOR]
This multiplies the row number with '14'(14)?
 
Upvote 0
Are you sure 14 is the correct number ?

What does this show

MsgBox Rows(ActiveWindow.ScrollRow).Height
 
Upvote 0
I understand, '14.25' puts it at the top of the visible screen, '14.26' puts it a little lower.
Thank you!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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