如何包含此代码以自动填充动态数据中的复选框

问题描述:

我有一个包含5列的工作表,我想在另一工作表中自动创建ActiveX复选框,并将其标题作为动态数据值。如何包含此代码以自动填充动态数据中的复选框

Sheet 1中包括动态数据范围G,H,I,J让说值10,20,30,40

我想在Sheet 2中,细胞E2自动创建的复选框一旦有数据在Sheet1范围G,H,I,J

'Private Sub UserForm_Initialize() 
Dim NewChkBx As MSForms.CheckBox 
    Dim rngSource As Range 
    Dim rngSource2 As Range 
    Dim rngSource3 As Range 
    Dim rngSource4 As Range 
    Dim rngSource5 As Range 
    Dim Quantity_definition_1 As Range 
    Dim Quantity_definition_2 As Range 
    Dim Quantity_definition_3 As Range 
    Dim Quantity_definition_4 As Range 
    Dim Quantity_definition_5 As Range 
    Dim TopPos As Integer 
    Dim MaxWidth As Long 

    With Worksheets("AppSyncData") 

     Set rngSource = .Range("F2", .Cells(.Rows.Count, "F").End(xlUp)) 
     Set rngSource2 = .Range("G2", .Cells(.Rows.Count, "G").End(xlUp)) 
     Set rngSource3 = .Range("H2", .Cells(.Rows.Count, "H").End(xlUp)) 
     Set rngSource4 = .Range("I2", .Cells(.Rows.Count, "I").End(xlUp)) 
     Set rngSource5 = .Range("J2", .Cells(.Rows.Count, "J").End(xlUp)) 

    End With 

    TopPos = 15 

    MaxWidth = 0 

    For Each Quantity_definition_1 In rngSource 
     If Quantity_definition_1.Value <> "" Then 
      Set NewChkBx = Me.Controls.Add("Forms.CheckBox.1") 
      With NewChkBx 
       .Caption = Quantity_definition_1.Value 
       .Left = 5 
       .Top = TopPos 
       .AutoSize = True 
       If .Width > MaxWidth Then MaxWidth = .Width 
      End With 
      TopPos = TopPos + 15 
     End If 
    Next Quantity_definition_1 

    TopPos = 15 

    For Each Quantity_definition_2 In rngSource2 
     If Quantity_definition_2.Value <> "" Then 
      Set NewChkBx = Me.Controls.Add("Forms.CheckBox.1") 
      With NewChkBx 
       .Caption = Quantity_definition_2.Value 
       .Left = 50 
       .Top = TopPos 
       .AutoSize = True 
       If .Width > MaxWidth Then MaxWidth = .Width 
      End With 
      TopPos = TopPos + 15 
     End If 

    Next Quantity_definition_2 

     TopPos = 15 

    For Each Quantity_definition_3 In rngSource3 
     If Quantity_definition_3.Value <> "" Then 
      Set NewChkBx = Me.Controls.Add("Forms.CheckBox.1") 
      With NewChkBx 
       .Caption = Quantity_definition_3.Value 
       .Left = 95 
       .Top = TopPos 
       .AutoSize = True 
       If .Width > MaxWidth Then MaxWidth = 500 
      End With 
      TopPos = TopPos + 15 
     End If 
    Next Quantity_definition_3 

    TopPos = 15 

    For Each Quantity_definition_4 In rngSource4 
     If Quantity_definition_4.Value <> "" Then 
      Set NewChkBx = Me.Controls.Add("Forms.CheckBox.1") 
      With NewChkBx 
       .Caption = Quantity_definition_4.Value 
       .Left = 135 
       .Top = TopPos 
       .AutoSize = True 
       If .Width > MaxWidth Then MaxWidth = 500 
      End With 
      TopPos = TopPos + 15 
     End If 
    Next Quantity_definition_4 

    TopPos = 15 

    For Each Quantity_definition_5 In rngSource5 
     If Quantity_definition_5.Value <> "" Then 
      Set NewChkBx = Me.Controls.Add("Forms.CheckBox.1") 
      With NewChkBx 
       .Caption = Quantity_definition_5.Value 
       .Left = 180 
       .Top = TopPos 
       .AutoSize = True 
       If .Width > MaxWidth Then MaxWidth = 500 

       End With 

      TopPos = TopPos + 15 
     End If 
    Next Quantity_definition_5 


    Me.Width = MaxWidth + 40 

    Me.Height = TopPos + 40 


End Sub 
+1

请仔细阅读[在什么情况下我想补充“紧急”或其他类似的短语我的问题,为了获得更快的答案?](// meta.*.com/q/326569) - 摘要这不是解决志愿者问题的理想方式,而且可能会对获得答案产生反作用。请不要将这添加到您的问题。 – halfer

+1

我很抱歉,我会马上删除 –

所以这里是我的版本。

Private Sub GenerateCheckboxes() 
    Dim ws As Excel.Worksheet 
    Set ws = ThisWorkbook.Worksheets("AppSyncData") 

    Dim ws2 As Excel.Worksheet 
    Set ws2 = ThisWorkbook.Worksheets("Checkboxes") 

    Dim vCheckBoxLefts As Variant 
    vCheckBoxLefts = Array(5, 50, 95, 135, 180) 

    Dim lLeftLoop As Long: lLeftLoop = 0 

    Const TopPos As Long = 15 
    Dim lTopOffset As Long 
    Dim lMaxBottom As Long 

    Dim lMaxRight As Long 
    lMaxRight = 0 


    Dim lColumnLoop As Long 
    For lColumnLoop = 6 To 10 

     lTopOffset = 0 

     Dim rngSource As Excel.Range 
     Set rngSource = ws.Range(ws.Cells(2, lColumnLoop), ws.Cells(ws.Rows.Count, lColumnLoop).End(xlUp)) 

     Dim vSource As Variant 
     vSource = rngSource.Value 

     Dim vQuantityDefinition As Variant 
     For Each vQuantityDefinition In vSource 
      If Len(vQuantityDefinition) > 0 Then 

       Dim chkNew As Excel.CheckBox 
       Set chkNew = ws2.CheckBoxes.Add(362.25, 92.25, 166.5, 48) 
       chkNew.Caption = vQuantityDefinition 
       chkNew.Left = vCheckBoxLefts(lLeftLoop) 
       chkNew.Top = TopPos + lTopOffset 
       If chkNew.Left + chkNew.Width > lMaxRight Then lMaxRight = chkNew.Left + chkNew.Width 
       If chkNew.Top + chkNew.Height > lMaxBottom Then lMaxBottom = chkNew.Top + chkNew.Height 

      lTopOffset = lTopOffset + 15 

      End If 
     Next vQuantityDefinition 

     lLeftLoop = lLeftLoop + 1 
    Next lColumnLoop 


End Sub 
+0

我问我是否可以嵌入这个诠释,他的表, 现在我已成功使用下面的代码来做到这一点,但每次的代码会清除细胞中的reffernces 对于每个CEL2在myRange2 如果cel2.Value “0”,则 集CB = cel2.Worksheet.CheckBoxes.Add(cel2.Left + cel2.Width/2 - 8.25,_ cel2.Top + CEL2。高度/ 2 - 8.25,0,0) cb.Text = cel2.Value cb.LinkedCell = cel2.Address(0,0) cb.Name =“cb”&cb.LinkedCell cel2.NumberFormat =“; ;;” End If –

+0

我修改了我的代码,将复选框添加到工作表中,但没有看到您最后的评论。 –

+0

任何反馈,Hany? –