Update userform

Formula11

Active Member
Joined
Mar 1, 2005
Messages
468
Office Version
  1. 365
Platform
  1. Windows
Using this code to show worksheets on a userform.

If I add a new sheet, want the form to update, while it's still running, and the form to stay in the same position.

Tried Workbook_NewSheet event but form position reverts to the original position.

Is there a refresh method with the form to stay in the same position?

IN USERFORM "form_sheets"
VBA Code:
Option Explicit

Private Sub UserForm_Initialize()
    Call tabs_in_form
End Sub

'CREATE FORM
Private Sub tabs_in_form()
    Dim Box As MSForms.CommandButton
    Dim box_top As Long, box_height As Long, box_left As Long, box_width As Long, box_gap As Long
    Dim box_Name As String
    Dim my_sht As Worksheet
    Dim Index As Long

    box_height = 24: box_top = 12: box_left = 12: box_width = 110: box_gap = 0
    Index = 1

    'Create boxes
    For Each my_sht In Worksheets
        box_Name = "MyButton" & Index
        Set Box = Me.Controls.Add("Forms.CommandButton.1", box_Name, True)
        With Box
            .Height = 24: .Top = box_top: .Left = box_left: .Width = box_width
            .Caption = my_sht.Name
            .BackColor = my_sht.Tab.Color
        End With
        Index = Index + 1
        box_top = box_top + box_height + box_gap
    Next my_sht
   
    Me.Height = box_top + 48
    Me.Width = box_width + 48

End Sub

Private Sub Workbook_NewSheet(ByVal Sh As Object)
    MsgBox "new"
    Call tabs_in_form
End Sub


IN WORKBOOK
VBA Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
    Unload form_sheets
    form_sheets.Show vbModeless
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I think it's "dummy" info because the method requires at least one of the RefersTo arguments?
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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