将用户窗体数据插入各行的下一列

Insert userform data into next column on respective rows

我想将表单中的值添加到 Excel sheet。

我在 A 列中有“headers”,从 A1 到 A20,A21 是一个从 B21 到 CZ21 的“分数”字段,它会根据上面分别输入的值自动计算分数列。

A​​1 到 20 有 headers 的问题,我想要最初在 B1 到 20 中输入的表单中的值,然后是 C1-20 等等。

例如,第一个表单响应应输入到 B1 到 B20 行,每行都有一个值。第二个表单响应将输入到 C1 到 C20 行,每行都有自己的值。

A​​ 列是冻结窗格。

Private Sub SaveButton_Click()

'Make Sheet2 active
Sheet4.Activate

'Determine empty Row
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

Cells(emptyRow, 2).Value = TextBox1.Value

Cells(emptyRow, 3).Value = Format(Now)

Cells(emptyRow, 4).Value = TextBox3.Value

If CheckBox1.Value = True Then
Cells(emptyRow, 5).Value = CheckBox1
Else
Cells(emptyRow, 5).Value = CheckBox1
End If

If CheckBox2.Value = True Then
Cells(emptyRow, 6).Value = CheckBox2
Else
Cells(emptyRow, 6).Value = CheckBox2
End If

If CheckBox3.Value = True Then
Cells(emptyRow, 7).Value = CheckBox3
Else
Cells(emptyRow, 7).Value = CheckBox3
End If

If CheckBox4.Value = True Then
Cells(emptyRow, 8).Value = CheckBox4
Else
Cells(emptyRow, 8).Value = CheckBox4
End If

If CheckBox9.Value = True Then
Cells(emptyRow, 9).Value = CheckBox9
Else
Cells(emptyRow, 9).Value = CheckBox9
End If

If CheckBox11.Value = True Then
Cells(emptyRow, 10).Value = CheckBox11
Else
Cells(emptyRow, 10).Value = CheckBox11
End If

If CheckBox14.Value = True Then
Cells(emptyRow, 11).Value = CheckBox14
Else
Cells(emptyRow, 11).Value = CheckBox14
End If

If CheckBox16.Value = True Then
Cells(emptyRow, 12).Value = CheckBox16
Else
Cells(emptyRow, 12).Value = CheckBox16
End If

If CheckBox18.Value = True Then
Cells(emptyRow, 13).Value = CheckBox18
Else
Cells(emptyRow, 13).Value = CheckBox18
End If

If CheckBox20.Value = True Then
Cells(emptyRow, 14).Value = CheckBox20
Else
Cells(emptyRow, 14).Value = CheckBox20
End If

If CheckBox22.Value = True Then
Cells(emptyRow, 15).Value = CheckBox22
Else
Cells(emptyRow, 15).Value = CheckBox22
End If

If CheckBox24.Value = True Then
Cells(emptyRow, 16).Value = CheckBox24
Else
Cells(emptyRow, 16).Value = CheckBox24
End If

If CheckBox26.Value = True Then
Cells(emptyRow, 17).Value = CheckBox26
Else
Cells(emptyRow, 17).Value = CheckBox26
End If

If CheckBox27.Value = True Then
Cells(emptyRow, 18).Value = CheckBox27
Else
Cells(emptyRow, 18).Value = CheckBox27
End If

If CheckBox28.Value = True Then
Cells(emptyRow, 19).Value = CheckBox28
Else
Cells(emptyRow, 19).Value = CheckBox28
End If

Cells(emptyRow, 20).Value = TextBox5.Value

Cells(emptyRow, 21).Value = TextBox4.Value

'Clearing data

CheckBox1.Value = "False"
CheckBox2.Value = "False"
CheckBox3.Value = "False"
CheckBox4.Value = "False"
CheckBox9.Value = "False"
CheckBox11.Value = "False"
CheckBox14.Value = "False"
CheckBox16.Value = "False"
CheckBox18.Value = "False"
CheckBox20.Value = "False"
CheckBox22.Value = "False"
CheckBox24.Value = "False"
CheckBox26.Value = "False"
CheckBox27.Value = "False"
CheckBox28.Value = "False"
TextBox1.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""

End Sub

你可以这样做

Private Sub SaveButton_Click()

    Dim col as Range

    Set col = Sheet4.Range("B1:B20")  'first potential location
    'find first unused column
    Do While Application.CountA(col) > 0
        Set col = col.Offset(0, 1)
    Loop

    col.cells(1).Value = TextBox1.Value
    col.cells(2).Value = Format(Now)
    col.cells(3).Value = TextBox3.Value
    'etc etc