Floating Command Button on Excel Sheet within the Active Window

arijitirf

Board Regular
Joined
Aug 11, 2016
Messages
98
Office Version
  1. 2016
Platform
  1. Windows
Hello!!
I need a Floating Command Button on the Excel Worksheet which can automatically moves around within the area of Active Window (if the Command Button is positioned on the Top Right Corner and as soon as I move the Cursor to the active cell in the Top Right corner then the Command Button should move to its Left and the Command Button would not leave the Active Window Area.)

I'm using below Codes to get the position of the Command Button in the Top Right Corner but issue with this code is it blocks view of the Cell on the Top Right Corner.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
With Me.CommandButton1
.Top = ActiveWindow.VisibleRange.Top 'ActiveCell.Top + ActiveWindow.VisibleRange '+ .Width
.Left = ActiveWindow.VisibleRange.Width - 93
End With
End Sub


I want it to be moved automatically to its left when the cursor position is on the top Right Cell, just beneath the Command Button.

Requesting help

Thanks in advance.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Something like this maybe?
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Me.CommandButton1
        .Left = ActiveCell.Left - .Width
        .Top = ActiveCell.Top
    End With
End Sub
 
Upvote 0
Something like this maybe?
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Me.CommandButton1
        .Left = ActiveCell.Left - .Width
        .Top = ActiveCell.Top
    End With
End Sub
Actually, I want a code by which, Command Button will stick to the Top Right Corner but if I need to see the cell where the Command Button is positioned, then the Command Button will move to its left. It will not leave the Active Window area.
 
Upvote 0
This appears to work:
VBA Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim topRightCell As Range
    Dim vr As Range
    Set vr = ActiveWindow.VisibleRange
    Set topRightCell = ActiveSheet.Cells(vr.Cells(1, 1).Row, vr.Columns.Count + vr.Cells(1, 1).Column - 3)
    With Me.CommandButton1
        .Left = topRightCell.Left
        .Top = topRightCell.Top
        If ActiveCell.Left >= .Left Then
            If ActiveCell.Top <= (.Top + .Height) Then
                .Left = .Left - .Width
            End If
        End If
    End With
End Sub
 
Upvote 0
Dear Pieterse,

Thanks you so much for helping me with the fantastic code. It works absolutely as per my requirement.

Regards,

Arijit Ghosh
 
Upvote 0
Dear Pieterse,

One little help is required. Code is working fine but only problem is its position is not on the extreme top right corner. Position of the Command Button is on the top but little shifted towards left.

Can this be fixed. If not then kindly help with your code so that the button could be placed on the Extreme Bottom Left position.

Thanks & Regards,

Arijit Ghosh
 
Upvote 0
The problem is that the extreme right position of the screen does not nicely line up with a cell border. No time to adapt to bottom-left, perhaps someone else here has?
 
Upvote 0
The problem is that the extreme right position of the screen does not nicely line up with a cell border. No time to adapt to bottom-left, perhaps someone else here has?
Yes Cell Border is creating the issue in it. Anyway Thank you for your time.

Regards,

Arijit Ghosh
 
Upvote 0
For bottom-left, the code changes to this:
VBA Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim bottomLeftCell As Range
    Dim vr As Range
    Set vr = ActiveWindow.VisibleRange
    Set bottomLeftCell = ActiveSheet.Cells(vr.Rows.Count + vr.Cells(1, 1).Row - 3, vr.Cells(1, 1).Column)
    With Me.CommandButton1
        .Left = bottomLeftCell.Left
        .Top = bottomLeftCell.Top
        If ActiveCell.Left >= .Left Then
            If ActiveCell.Top >= (.Top - .Height) Then
                .Left = .Left + .Width
            End If
        End If
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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