连续向我的excel数据库添加多个数据,但仅在同一列中
下面是我的代码:连续向我的excel数据库添加多个数据,但仅在同一列中,excel,vba,Excel,Vba,下面是我的代码: *Private Sub CommandButton8_Click() Dim iRow As Long Dim ws As Worksheet Set ws = Worksheets("Summary") Dim rngNullString 'find first empty row in database Set rngNullString = Intersect(ws.Colum
*Private Sub CommandButton8_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Summary")
Dim rngNullString
'find first empty row in database
Set rngNullString = Intersect(ws.Columns("B"), ws.Columns("B")).Find("")
If rngNullString.Row < ws.Cells(ws.Rows.Count, "B").End(xlUp).Row Then
Set rngNullString = Intersect(ws.Columns("B"), ws.Columns("B")).SpecialCells(xlCellTypeBlanks)
End If
iRow = rngNullString.Row
' iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
' SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for Name number
If Trim(Me.ComboBox1.Value) = "" Then
Me.ComboBox1.SetFocus
MsgBox "Please complete the FORM"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 2).Value = Me.ComboBox21.Value
ws.Cells(iRow, 4).Value = Me.ComboBox39.Value
ws.Cells(iRow, 5).Value = Me.TextBox8.Value
ws.Cells(iRow, 6).Value = Me.TextBox9.Value
ws.Cells(iRow, 7).Value = Me.TextBox112.Value
ws.Cells(iRow, 2).Value = Me.ComboBox22.Value
ws.Cells(iRow, 4).Value = Me.ComboBox40.Value
ws.Cells(iRow, 5).Value = Me.TextBox11.Value
ws.Cells(iRow, 6).Value = Me.TextBox15.Value
ws.Cells(iRow, 7).Value = Me.TextBox113.Value
ws.Cells(iRow, 2).Value = Me.ComboBox23.Value
ws.Cells(iRow, 4).Value = Me.ComboBox41.Value
ws.Cells(iRow, 5).Value = Me.TextBox17.Value
ws.Cells(iRow, 6).Value = Me.TextBox21.Value
ws.Cells(iRow, 7).Value = Me.TextBox114.Value
ws.Cells(iRow, 2).Value = Me.ComboBox24.Value
ws.Cells(iRow, 4).Value = Me.ComboBox42.Value
ws.Cells(iRow, 5).Value = Me.TextBox23.Value
ws.Cells(iRow, 6).Value = Me.TextBox27.Value
ws.Cells(iRow, 7).Value = Me.TextBox115.Value
ws.Cells(iRow, 2).Value = Me.ComboBox25.Value
ws.Cells(iRow, 4).Value = Me.ComboBox43.Value
ws.Cells(iRow, 5).Value = Me.TextBox29.Value
ws.Cells(iRow, 6).Value = Me.TextBox33.Value
ws.Cells(iRow, 7).Value = Me.TextBox116.Value
ws.Cells(iRow, 2).Value = Me.ComboBox26.Value
ws.Cells(iRow, 4).Value = Me.ComboBox44.Value
ws.Cells(iRow, 5).Value = Me.TextBox35.Value
ws.Cells(iRow, 6).Value = Me.TextBox39.Value
ws.Cells(iRow, 7).Value = Me.TextBox117.Value
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
End Sub*
*专用子命令按钮8\u单击()
暗淡无光
将ws设置为工作表
设置ws=工作表(“摘要”)
Dim rngNullString
'查找数据库中的第一个空行
设置rngNullString=Intersect(ws.Columns(“B”)、ws.Columns(“B”)).Find(“”)
如果rngNullString.Row
**我的代码不起作用,只能添加1行。数据在相同的列中,但在不同的行中。正如您在下面看到的(附图),如果我要在我的组合框和文本框中添加一个值,我想分别在行中添加多个数据。这将清除您的工作表列(c)
您的代码覆盖同一行6次
Private Sub CommandButton8_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Summary")
Dim rngNullString
Dim Target As Range
Dim vDB() As Variant
'find first empty row in database
Set Target = ws.Range("b" & Rows.Count).End(xlUp)(2)
'check for Name number
If Trim(Me.ComboBox1.Value) = "" Then
Me.ComboBox1.SetFocus
MsgBox "Please complete the FORM"
Exit Sub
End If
'copy the data to the database
ReDim vDB(1 To 6, 1 To 6) 'set the array size
vDB(1, 1) = Me.ComboBox21.Value
vDB(1, 3) = Me.ComboBox39.Value
vDB(1, 4) = Me.TextBox8.Value
vDB(1, 5) = Me.TextBox9.Value
vDB(1, 6) = Me.TextBox112.Value
vDB(2, 1) = Me.ComboBox22.Value
vDB(2, 3) = Me.ComboBox40.Value
vDB(2, 4) = Me.TextBox11.Value
vDB(2, 6) = Me.TextBox15.Value
vDB(2, 6) = Me.TextBox113.Value
vDB(3, 1) = Me.ComboBox23.Value
vDB(3, 3) = Me.ComboBox41.Value
vDB(3, 4) = Me.TextBox17.Value
vDB(3, 5) = Me.TextBox21.Value
vDB(3, 6) = Me.TextBox114.Value
vDB(4, 1) = Me.ComboBox24.Value
vDB(4, 3) = Me.ComboBox42.Value
vDB(4, 4) = Me.TextBox23.Value
vDB(4, 5) = Me.TextBox27.Value
vDB(4, 6) = Me.TextBox115.Value
vDB(5, 1) = Me.ComboBox25.Value
vDB(5, 3) = Me.ComboBox43.Value
vDB(5, 4) = Me.TextBox29.Value
vDB(5, 5) = Me.TextBox33.Value
vDB(5, 6) = Me.TextBox116.Value
vDB(6, 1) = Me.ComboBox26.Value
vDB(6, 3) = Me.ComboBox44.Value
vDB(6, 4) = Me.TextBox35.Value
vDB(6, 5) = Me.TextBox39.Value
vDB(6, 6) = Me.TextBox117.Value
Target.Resize(6, 6) = vDB
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
'clear the data
End Sub