【问题标题】:Excel VBA UserFormsExcel VBA 用户窗体
【发布时间】:2014-02-09 21:40:23
【问题描述】:

我已经尝试自己处理并重新编辑了问题。所以我在 Excel VBA 中完成了 UserForm(我上传了一张看起来像 的图片

我使用以下代码将信息从文本框传输到 excel 行

'Determine Empty Row
emptyRow= WorksheetFunction.Counta(Range("A:A"))+1
'Transfer into to cells
Cells(emptyRow, 1).Value= NOD_Text.Value
Cells(emptyRow,2).Value=TOD_Text.Value
Cells(emptyRow,3).Value=Program_Text.value
Cells(emptyRow,4).Value=email_Text.value
Cells(emptyRow,5).Value=OPN_Text.value
Cells(emptyRow,6).Value=CPN_Text.Value

我有多个与利益相关者相同的工作表(工作表 A、工作表 B、工作表 C、工作表 D 等),我想根据选中的复选框传输上述信息。例如,如果单击复选框 A、B、C,则上述信息将传输到工作表 A、B、C。

我不确定如何根据利益相关者的复选框激活工作表...

If A_Checkbox.Value=True then Worksheets(A).Activate then Cells(emptyRow, 1).Value= NOD_Text.Value
Cells(emptyRow,2).Value=TOD_Text.Value
Cells(emptyRow,3).Value=Program_Text.value
Cells(emptyRow,4).Value=email_Text.value
Cells(emptyRow,5).Value=OPN_Text.value
Cells(emptyRow,6).Value=CPN_Text.Value

不确定上面的代码是否正确,但问题是..如果该人选中 3 个利益相关者 (A,B,C)...怎么办?我不知道如何编码...

另外,我想把所有信息都放在主选项卡中,无论检查哪些框,但我不知道如何始终保持主选项卡处于激活状态...

我希望这比以前更清楚

【问题讨论】:

  • 如果您能帮助我或建议参考/链接/指南供我学习,我们将不胜感激。 这不是本网站的运作方式。我建议使用UserForm(谷歌搜索),否则一系列Inputbox 就可以了。但似乎您也在询问广泛的问题来编码您的整个应用程序,这超出了 SO 的范围。祝你好运。
  • 我已经重新编辑了这个问题 - 如果更清楚请告诉我

标签: vba excel userform


【解决方案1】:

为此,我将使用控件的 TAG 属性。

在每个文本框的 TAG 属性中,我会写出将值粘贴到何处的单元格引用 - 例如 $B$2。

在每个复选框的 TAG 属性中,我会写下链接到该控件的工作表名称 - 例如 Sheet1。

然后在命令按钮后面我会写这样的代码:

Private Sub CommandButton1_Click()
    Dim ctrl As Control
    Dim ctrl1 As Control

    'Cycle through each control looking for checkboxes.
    For Each ctrl In Me.Controls

        If TypeName(ctrl) = "CheckBox" Then

            'If the checkbox is ticked
            If ctrl.Value = True Then

                With ThisWorkbook.Worksheets(ctrl.Tag) 'Grab the sheet name from the check box.

                    'Cycle through each control looking for textboxes.
                    For Each ctrl1 In Me.Controls
                        If TypeName(ctrl1) = "TextBox" Then
                            .Range(ctrl1.Tag) = ctrl1.Value 'Grab the cell address from the text box.
                        End If
                    Next ctrl1
                End With
            End If
        End If
    Next ctrl
End Sub

【讨论】:

    【解决方案2】:

    您需要在 if 中激活工作表,并且在知道第一个空单元格之后:

    If A_Checkbox.Value=True then 
       Worksheets("A").Activate
    ElseIf B_Checkbox.Value=True then 
       Worksheets("B").Activate
    End If
    
    emptyRow=Activesheet.range("A1000000").end(XlUp).Row+1
    Cells(emptyRow,2).Value=TOD_Text.Value
    

    【讨论】:

      【解决方案3】:

      这应该可以解决问题:

      Private Sub AddButton_Click()
      
      Dim CB As Control
      
      For Each CB In UserForm1.Controls
      
          If TypeName(CB) = "CheckBox" Then
      
              If CB.Value = True Then
      
                  ShtNm = CB.Caption
      
                  With ActiveWorkbook.Sheets(ShtNm)
      
                      'Determine Empty Row
                      emptyRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row + 1
                      'Transfer into to cells
                      .Cells(emptyRow, 1).Value = NOD_Text.Value
                      .Cells(emptyRow, 2).Value = TOD_Text.Value
                      .Cells(emptyRow, 3).Value = Program_Text.Value
                      .Cells(emptyRow, 4).Value = email_Text.Value
                      .Cells(emptyRow, 5).Value = OPN_Text.Value
                      .Cells(emptyRow, 6).Value = CPN_Text.Value
      
                  End With
      
              End If
      
          End If
      
      Next CB
      
      With ActiveWorkbook.Sheets("Master")
      
          'Determine Empty Row
          emptyRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row + 1
          'Transfer into to cells
          .Cells(emptyRow, 1).Value = NOD_Text.Value
          .Cells(emptyRow, 2).Value = TOD_Text.Value
          .Cells(emptyRow, 3).Value = Program_Text.Value
          .Cells(emptyRow, 4).Value = email_Text.Value
          .Cells(emptyRow, 5).Value = OPN_Text.Value
          .Cells(emptyRow, 6).Value = CPN_Text.Value
      
      End With
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多