我创建了一个表,然后用图中所示的值填充它。现在我想用我创建的用户表单填充该表中的值。分配给图像中的项目按钮的代码仅适用于前两列。
第一个效果很好并且在填充之后添加数据,但第二个没有,因为它将数据添加到远离填充的数据。
这是图片:
以下是代码:
Private Sub CommandButton1_Click()
Dim RowCount As Long
If CategoriesComboBox.Value = "Household" Then
RowCount = Worksheets("Data Lists").Range("C7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("C7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Entertainment" Then
RowCount = Worksheets("Data Lists").Range("D7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("D7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Food" Then
RowCount = Worksheets("Data Lists").Range("E7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("E7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Gifts/Donations" Then
RowCount = Worksheets("Data Lists").Range("F7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("F7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Children" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Investment Accounts" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Medical" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Other" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Personal" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Pets" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Taxes/Legal" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Transportation" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
End Sub
答案 0 :(得分:1)
这应该解决它:
纠正两个RowCount变量
'RowCount in household
RowCount = Cells(Rows.Count, "C").End(xlUp).Row + 1 'probably + 1 is going to leave a blank, check for it
'RowCount in entretainment
RowCount = Cells(Rows.Count, "D").End(xlUp).Row + 1 'probably + 1 is going to leave a blank, check for it
'... and so on for each field in your form with its corresponding Column letter
PS:"我赶时间。请尽快。"要求事情不是一种好方法。这不是我们为免费网站工作,也不是全天候可用,特别是当您只为您想要做的事情粘贴图像时。
修改强>
替换此
If CategoriesComboBox.Value = "Entertainment" Then
RowCount = Worksheets("Data Lists").Range("D7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("D7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
为此:
If CategoriesComboBox.Value = "Entertainment" Then
RowCount = Worksheets("Data Lists").Cells(Rows.Count, "D").End(xlUp).Row -6 'since offset is starting at D7
With Worksheets("Data Lists").Range("D7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
答案 1 :(得分:0)
我想要的只是在指定的单元格中输出---> Sample
Private Sub CommandButton1_Click()
Dim RowCount As Long
If CategoriesComboBox.Value = "Household" Then
RowCount = Worksheets("Data Lists").Range("C7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("C7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Entertainment" Then
RowCount = Worksheets("Data Lists").Cells(Rows.Count, "D").End(xlUp).Row - 6 'since offset is starting at D7
With Worksheets("Data Lists").Range("D7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Food" Then
RowCount = Worksheets("Data Lists").Range("E7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("E7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Gifts/Donations" Then
RowCount = Worksheets("Data Lists").Range("F7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("F7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Children" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Investment Accounts" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Medical" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Other" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Personal" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Pets" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Taxes/Legal" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
If CategoriesComboBox.Value = "Transportation" Then
RowCount = Worksheets("Data Lists").Range("G7").CurrentRegion.Rows.Count
With Worksheets("Data Lists").Range("G7")
.Offset(RowCount, 0).Value = Me.NameTextBox.Value
End With
End If
End Sub