我不是这个行业的程序员,我是系统管理员,当我尝试某些东西时,他通常会把这个难题汇集在一起。现在我需要帮助简化Excel表单以使其有用。它将被我使用。
我有一个包含4列的组合框。我也有一个按钮。单击按钮时,我想将组合框中四列的数据保存到从第3行开始的下一个可用行单元格。我希望数据只保存到第30行。我来回测试但是没有得到它,所以我完全删除了代码。有什么想法吗?
表名为"数据"
我在我的ComBobox中使用ListFillRange作为数据源。
msg_isparent = 1
Private Sub CommandButton1_Click()
Call SaveComboBoxData
End Sub
答案 0 :(得分:1)
你可以进一步整理,
注意:强>
假设使用ActiveX组合框。你需要改变这个
Set sourceCombo = sourceSheet.OLEObjects("Combobox1").Object
如果使用表单控件。
订单表的代码窗格:
Option Explicit
Private Sub CommandButton1_Click()
AddRecords
End Sub
标准模块
Option Explicit
Public Sub AddRecords()
Dim wb As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Set wb = ThisWorkbook
Set sourceSheet = wb.Worksheets("Order")
Set targetSheet = wb.Worksheets("Data")
Dim lastRowTarget As Long
Dim sourceCombo As ComboBox
Set sourceCombo = sourceSheet.OLEObjects("Combobox1").Object 'assume activex object
Dim lRow As Long
Dim lCol As Long
Dim nextRow As Long
With sourceCombo
For lRow = 0 To .ListCount - 1
If lRow = sourceCombo.ListIndex Then
nextRow = GetNextRow(targetSheet)
If nextRow = 31 Then
MsgBox "End row of 30 reached"
Exit Sub
End If
For lCol = 0 To .ColumnCount - 1
targetSheet.Cells(nextRow, lCol + 1) = .List(lRow, lCol)
Next lCol
Exit For
End If
Next lRow
End With
End Sub
Private Function GetNextRow(targetSheet As Worksheet) As Long
With targetSheet
GetNextRow = IIf(.Cells(.Rows.Count, "A").End(xlUp).Row < 3, 3, .Cells(.Rows.Count, "A").End(xlUp).Row + 1)
End With
End Function
行动中的代码:
<强>参考文献:强>