Динамически добавлять новые флажки управления формой на основе имен листов (Excel VBA) - PullRequest
0 голосов
/ 07 сентября 2018

Я пытаюсь создать систему, которая добавляет флажки (по нажатию кнопки), если в книгу добавляются новые листы. Мой код ниже. Он мог создавать флажки, пока я не попытался изменить местоположение, поэтому я предполагаю, что причина, по которой он не работает, связана с этим.

Private Sub Update_Click()
Dim cb As CheckBox
Dim Exists As Boolean
'I think these location/ dimension variables are perhaps wrong (I'm not sure what values they take)
Dim TopLocation As Double
Dim LeftLocation As Double
Dim Width As Double
Dim Height As Double

    For Each ws In ActiveWorkbook.Worksheets
'This loop is simply to stop it from making duplicate checkboxes
Exists=False
        For Each cb In ThisWorkbook.Worksheets("Summary").CheckBoxes
            If cb.name = ws.name Or ws.name = "Summary" Or ws.name = "Price List (2)" Then
                Exists = True

            End If

        Next

        If Exists = False Then

        TopLocation = 0
    LeftLocation = 0
    Width = 0
    Height = 0
'The following loop is an attempt to find the checkbox that is furthest down the page, the problem is that I am not too familiar with the location attribute so am just assuming that it increases as you move down the page
    For Each cb In ThisWorkbook.Worksheets("Summary").CheckBoxes
        If cb.Top > TopLocation Then
            TopLocation = cb.Top
        End If
         If cb.Left > LeftLocation Then
            LeftLocation = cb.Left
        End If
         If cb.Width > Width Then
            Width = cb.Width
        End If
         If cb.Height > Height Then
            Height = cb.Height
        End If

    Next
'The following is where I believe the problem to be, I thought that I could simply use the variables I had created to place the new one in this location
            With ThisWorkbook.Worksheets("Summary").CheckBoxes.Add(LeftLocation, TopLocation + Height, Width, Height)
                .name = ws.name
                .Caption = ws.name
            End With

        End If
    Next ws
End Sub

Я думаю, что, возможно, это неправильное понимание синтаксиса флажков, и надеялся, что кто-нибудь поможет мне понять, где я ошибся. Любая помощь приветствуется, спасибо:)

1 Ответ

0 голосов
/ 08 сентября 2018

у вас есть две опечатки ..

в строке

With ThisWorkbook.Worksheets("Summary").CheckBoxes.Add(LocationLeft, LocationTop + Height, Width, Height)

переменные должны быть LeftLocation (не LocationLeft) и TopLocation (не LocationTop)

удачи

...