Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 将复选框标题导入表中_Excel_Vba_Checkbox - Fatal编程技术网

Excel 将复选框标题导入表中

Excel 将复选框标题导入表中,excel,vba,checkbox,Excel,Vba,Checkbox,我有一个userform,希望从中将值导入预定义的表(称为“Overzicht”) 我有12个复选框(每个月一个),如果选中该复选框,我希望每个月有一行。我想将复选框的标题放在表中的一列中 例如,如果我选中一月、二月和三月,我希望将所有值导入到一个表中的三行中,其中第一行“月”列显示“一月”,第二行显示“二月”,以此类推 我有一段代码,可以检查复选框的数量,并创建相同数量的行。我不知道如何在行中获得正确的标题。这就是我到目前为止所做的: Private Sub CommandButton1_Cl

我有一个userform,希望从中将值导入预定义的表(称为“Overzicht”)

我有12个复选框(每个月一个),如果选中该复选框,我希望每个月有一行。我想将复选框的标题放在表中的一列中

例如,如果我选中一月、二月和三月,我希望将所有值导入到一个表中的三行中,其中第一行“月”列显示“一月”,第二行显示“二月”,以此类推

我有一段代码,可以检查复选框的数量,并创建相同数量的行。我不知道如何在行中获得正确的标题。这就是我到目前为止所做的:

Private Sub CommandButton1_Click()

Dim rng As Range
Dim newrow As ListRow
Set rng = ThisWorkbook.Worksheets("Kostenoverzicht").Range("Overzicht")
Dim answer As Integer
'check number of rows to insert based on # of checked months
Dim ctl As MSForms.Control
Dim rows As Long
For Each ctl In Kostenoverzicht.Frame2.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Kostenoverzicht.Frame2.Controls(ctl.Name).Value = True Then
rows = rows + 1
End If
End If
Next

answer = MsgBox("Are you sure you want to continue?", vbQuestion + vbYesNo + vbDefaultButton1, "Zet in overzicht?")

If answer = vbYes Then
rng.Select
Set newrow = Selection.ListObject.ListRows.Add(alwaysinsert:=True)
With ws
For rows = 1 To rows
newrow.Range.Cells(rows, 1).Value = Me.TextBox1.Value
newrow.Range.Cells(rows, 2).Value = Me.CategorieBox.Value
newrow.Range.Cells(rows, 3).Value = Me.SubCategorieBox.Value
newrow.Range.Cells(rows, 4).Value = Me.BankrekeningBox.Value
If OptionButton1.Value = True Then newrow.Range.Cells(rows, 5).Value = "Af" Else newrow.Range.Cells(1, 5).Value = "Bij"
newrow.Range.Cells(rows, 6).Value = Me.TextBox2.Value
newrow.Range.Cells(rows, 7).Value = Me.CheckBox1.Caption
If OptionButton3.Value = True Then newrow.Range.Cells(rows, 8).Value = "Ja" Else newrow.Range.Cells(1, 8).Value = "Nee"
Next
End With

End If
End Sub
它将为所有行提供值“一月”

我需要代码来替换
newrow.Range.Cells(行,7).Value=Me.CheckBox1.Caption
以获取选中复选框的标题


我试图在几个网站上找到我的答案。

尽管我的第一个答案是错误的,我认为它总是从一月到另一个月。但现在看来也可能是9月和11月。在底部找到旧的/错误的答案。 由于某种原因,我很难理解你的代码。您检查要在顶部插入多少行,那么所有行似乎都获得了相同的信息,因为单元格1-6是来自其他非变量/动态源的值?我不确定我是否明白发生了什么。到底是什么?它也会调用用户表单,对吗?我真的无法重建你所拥有的,因为我不太清楚

最佳镜头:

Private Sub CommandButton1_Click()

Dim rng As Range
Dim newrow As ListRow
Set rng = ThisWorkbook.Worksheets("Kostenoverzicht").Range("Overzicht")
Dim answer As Integer
Dim ctl As MSForms.Control
Dim rows As Long

answer = MsgBox("Are you sure you want to continue?", vbQuestion + vbYesNo + vbDefaultButton1, "Zet in overzicht?")
If Not answer = vbYes Then Exit Sub

For Each ctl In Kostenoverzicht.Frame2.Controls
    If TypeOf ctl Is MSForms.CheckBox Then
        If Kostenoverzicht.Frame2.Controls(ctl.Name).Value = True Then
            Set newrow = rng.ListObject.ListRows.Add(alwaysinsert:=True)

            newrow.Range.Cells(rows, 1).Value = Me.TextBox1.Value
            newrow.Range.Cells(rows, 2).Value = Me.CategorieBox.Value
            newrow.Range.Cells(rows, 3).Value = Me.SubCategorieBox.Value
            newrow.Range.Cells(rows, 4).Value = Me.BankrekeningBox.Value

            If OptionButton1.Value = True Then newrow.Range.Cells(rows, 5).Value = "Af" Else newrow.Range.Cells(1, 5).Value = "Bij"

            newrow.Range.Cells(rows, 6).Value = Me.TextBox2.Value
            newrow.Range.Cells(rows, 7).Value = ctl.Name 'Or maybe ctl.Caption ?

            If OptionButton3.Value = True Then newrow.Range.Cells(rows, 8).Value = "Ja" Else newrow.Range.Cells(1, 8).Value = "Nee"


        End If
    End If
Next

End Sub
我希望你能看到我在这里试图做什么,并能根据你的需要修复它/bug修复它。正如我所说,我没有重新构建一个表来测试它。也许它对你有帮助

旧的:


newrow.Range.Cells(rows,7.Value=Format(3+31*(rows-1),“MMMM”)

无论我的第一个答案是什么,我都误解了,以为它总是从一月到另一个月。但现在似乎也可能是九月和十一月。在底部找到旧的/错误的答案。 由于某些原因,我越来越难以理解您的代码。您检查要在顶部插入多少行,然后所有行似乎都得到了相同的信息,因为单元格1-6是来自其他非变量/动态源的值?我不确定我是否理解发生了什么。ws是什么?它也有点调用用户表单,对吗?我可以“我真的不知道你有什么,因为我不太清楚

最佳镜头:

Private Sub CommandButton1_Click()

Dim rng As Range
Dim newrow As ListRow
Set rng = ThisWorkbook.Worksheets("Kostenoverzicht").Range("Overzicht")
Dim answer As Integer
Dim ctl As MSForms.Control
Dim rows As Long

answer = MsgBox("Are you sure you want to continue?", vbQuestion + vbYesNo + vbDefaultButton1, "Zet in overzicht?")
If Not answer = vbYes Then Exit Sub

For Each ctl In Kostenoverzicht.Frame2.Controls
    If TypeOf ctl Is MSForms.CheckBox Then
        If Kostenoverzicht.Frame2.Controls(ctl.Name).Value = True Then
            Set newrow = rng.ListObject.ListRows.Add(alwaysinsert:=True)

            newrow.Range.Cells(rows, 1).Value = Me.TextBox1.Value
            newrow.Range.Cells(rows, 2).Value = Me.CategorieBox.Value
            newrow.Range.Cells(rows, 3).Value = Me.SubCategorieBox.Value
            newrow.Range.Cells(rows, 4).Value = Me.BankrekeningBox.Value

            If OptionButton1.Value = True Then newrow.Range.Cells(rows, 5).Value = "Af" Else newrow.Range.Cells(1, 5).Value = "Bij"

            newrow.Range.Cells(rows, 6).Value = Me.TextBox2.Value
            newrow.Range.Cells(rows, 7).Value = ctl.Name 'Or maybe ctl.Caption ?

            If OptionButton3.Value = True Then newrow.Range.Cells(rows, 8).Value = "Ja" Else newrow.Range.Cells(1, 8).Value = "Nee"


        End If
    End If
Next

End Sub
我希望你能看到我在这里试图做的事情,并能根据你的需要/bug修复它。就像我说的,我没有重新构建一张表来测试它。也许这对你有帮助

旧的:


newrow.Range.Cells(第7行)。Value=Format(3+31*(第1行),“MMMM”)

此代码应该放在用户表单后面

以下是编码时的一些建议:

  • 使用,这样您就不会有未定义变量的意外行为
  • 始终缩进您的代码(请参阅一个免费的工具来帮助您)
  • 尝试将定义变量的逻辑与重用变量的逻辑分开
  • 检查和自定义代码,使其符合您的需要

    通过添加一个
    stop
    并按
    F8
    并逐行执行,可以看到代码中发生了什么

    代码:

    Option Explicit
    
    Private Sub CommandButton1_Click()
    
        ProcessForm
    
    End Sub
    
    Private Sub ProcessForm()
    
        Dim targetControl As MSForms.control
    
        Dim continue As Boolean
    
        continue = MsgBox("Are you sure you want to continue?", vbQuestion + vbYesNo + vbDefaultButton1, "Zet in overzicht?") = vbYes
    
        ' If it's not yes
        If Not continue Then Exit Sub
    
        For Each targetControl In Me.Controls
    
            If TypeOf targetControl Is MSForms.CheckBox Then
                Select Case targetControl.Value
                Case True
                    ' Call the RecordData procedure (remember to add the controls in the parameters)
                    RecordData targetControl.Caption, Me.TextBox1.Value, Me.TextBox2.Value
                Case False
                    ' Do something?
                End Select
            End If
    
        Next targetControl
    End Sub
    
    Private Sub RecordData(ByVal recordMonth As String, ByVal textbox01Value As String, ByVal textbox02Value As String)
    
        Dim targetTable As ListObject
        Dim newRow As ListRow
    
        ' Refer to the table (no need for the worksheet reference)
        Set targetTable = Range("Overzicht").ListObject
    
        ' Add a new row an set a reference
        Set newRow = targetTable.ListRows.Add
    
        ' Refer to the columns by their header (replace with yours)
        newRow.Range.Cells(1, targetTable.ListColumns("Month").Index).Value = recordMonth
        newRow.Range.Cells(1, targetTable.ListColumns("Textbox1").Index).Value = textbox01Value
        newRow.Range.Cells(1, targetTable.ListColumns("Textbox2").Index).Value = textbox02Value
    
    End Sub
    

    让我知道它是否有效或者您是否需要更多帮助

    此代码应该放在您的用户表单后面

    以下是编码时的一些建议:

  • 使用,这样您就不会有未定义变量的意外行为
  • 始终缩进您的代码(请参阅一个免费的工具来帮助您)
  • 尝试将定义变量的逻辑与重用变量的逻辑分开
  • 检查和自定义代码,使其符合您的需要

    通过添加一个
    stop
    并按
    F8
    并逐行执行,可以看到代码中发生了什么

    代码:

    Option Explicit
    
    Private Sub CommandButton1_Click()
    
        ProcessForm
    
    End Sub
    
    Private Sub ProcessForm()
    
        Dim targetControl As MSForms.control
    
        Dim continue As Boolean
    
        continue = MsgBox("Are you sure you want to continue?", vbQuestion + vbYesNo + vbDefaultButton1, "Zet in overzicht?") = vbYes
    
        ' If it's not yes
        If Not continue Then Exit Sub
    
        For Each targetControl In Me.Controls
    
            If TypeOf targetControl Is MSForms.CheckBox Then
                Select Case targetControl.Value
                Case True
                    ' Call the RecordData procedure (remember to add the controls in the parameters)
                    RecordData targetControl.Caption, Me.TextBox1.Value, Me.TextBox2.Value
                Case False
                    ' Do something?
                End Select
            End If
    
        Next targetControl
    End Sub
    
    Private Sub RecordData(ByVal recordMonth As String, ByVal textbox01Value As String, ByVal textbox02Value As String)
    
        Dim targetTable As ListObject
        Dim newRow As ListRow
    
        ' Refer to the table (no need for the worksheet reference)
        Set targetTable = Range("Overzicht").ListObject
    
        ' Add a new row an set a reference
        Set newRow = targetTable.ListRows.Add
    
        ' Refer to the columns by their header (replace with yours)
        newRow.Range.Cells(1, targetTable.ListColumns("Month").Index).Value = recordMonth
        newRow.Range.Cells(1, targetTable.ListColumns("Textbox1").Index).Value = textbox01Value
        newRow.Range.Cells(1, targetTable.ListColumns("Textbox2").Index).Value = textbox02Value
    
    End Sub
    

    让我知道它是否有效,或者您是否需要更多帮助

    因此您需要存储复选框的名称?如果是真的,为什么不单独设置一个子项并传递复选框的名称呢,我需要存储复选框的标题,我想我会一次完成所有操作,但不确定如何以简洁、快速的方式完成。尝试将此替换为:
    对于行=1到行
    ,其他类似于
    对于rw=1到行
    ,显然还有后续的
    …单元格(行,…
    references好吧,这个更改提供了我需要的内容,除了我要将月份存储到的列之外,该列仍然提供“一月”在所有行中,您需要存储复选框的名称?为什么不使用单独的子项并传递复选框的名称(如果是真的)?好的,我需要存储复选框的标题,并认为我会一次完成所有操作,但不确定如何以简洁快速的方式完成此操作尝试将此替换为:
    For rows=1 to rows
    与rw=1到行的
    以及随后的
    …单元格(行,
    引用)一样,该更改提供了我需要的内容,但我要将月份存储到的列除外,该列仍然提供“一月”在所有的linesThanks中,这都是有效的。我只是一个vba的新手,我使用了我在不久前创建的另一个userform上的一些代码,想想ws就是从那里来的。现在我看了你的代码,它看起来更具逻辑性,我想我在编码中迷失了一点(我需要在构建代码时更结构化;)再次感谢,非常感谢,请接受这个答案,如果它帮助了你,并且考虑李嘉图的回答,如果它有帮助的话。嘿,我很高兴它帮助了你!thx也接受了答案。谢谢,这很有效。我只是一个vba的新手,我正在使用我在不久前创建的另一个userform上的一些代码,想想ws就是从那里来的。现在我看了你的代码,它看起来更具逻辑性,我想我在编码时迷失了一点(我需要更加结构化)