简体   繁体   English

Excel VBA用户表单

[英]Excel VBA UserForms

I've tried to work on it myself and re-edited the question. 我已尝试自己进行处理,并重新编辑了问题。 So I've done UserForm in Excel VBA (I've uploaded a picture of what it looks like 因此,我已经在Excel VBA中完成了UserForm(我上传了一张看起来像的图片 在此处输入图片说明

and I've used the following codes to transfer information from the textbox to excel rows 并且我使用以下代码将信息从文本框传输到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

I have multiple sheets same as the Stakeholder (Sheet A, Sheet B, Sheet C, Sheet D etc) and I want to transfer the above information depending on which checkbox is checked. 我有多个与利益相关者相同的工作表(工作表A,工作表B,工作表C,工作表D等),并且我想根据选中的复选框来传递以上信息。 Example, if checkbox A,B,C is clicked then above information is transferred to sheets A,B,C. 例如,如果单击复选框A,B,C,则以上信息将传输到工作表A,B,C。

I'm not sure how to activate sheets depending on checkboxes of Stakeholders... 我不确定如何根据利益相关者的复选框激活工作表...

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

not sure if above code is correct but the problem is..what if the person checkboxes 3 stakeholder (A,B,C)...? 不知道上面的代码是否正确,但是问题是..如果此人选中3个涉众(A,B,C)...该怎么办? I'm not sure how to code that... 我不确定如何编码...

also, I want to put all informatoin in the Master Tab no matter which boxes are chekced, but i don't know how to always keep master tab activated... 另外,无论哪个框被选中,我都希望将所有信息放入``主选项卡''中,但是我不知道如何始终保持激活主选项卡...

i hope this is more clearer than before 我希望这比以前更清楚

To do this I'd use the TAG property of the controls. 为此,我将使用控件的TAG属性。

In the TAG property of each text box I'd write the cell reference of where to paste the value - for example $B$2. 在每个文本框的TAG属性中,我将写入粘贴该值的位置的单元格引用-例如$ B $ 2。

In the TAG property of each check box I'd write the sheet name linked to that control - for example Sheet1. 在每个复选框的TAG属性中,我编写链接到该控件的工作表名称-例如Sheet1。

Then behind the command button I'd write code something like this: 然后在命令按钮后面,我将编写如下代码:

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

you need activate the sheet in the if and after know the firt empty cell: 您需要在if和after知道第一空单元格中激活工作表:

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

This Should do the trick: 这应该可以解决问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM