好。我希望这是最后一期。我的代码在另一个区域,并得到了一些很好的帮助。我假设他(Ratafia)很忙,今天没有收到回复。所以我想我会转发到这里。我在行RwLast = Range(“AI”& ActiveSheet.Rows.Count,2).End(xlUp).Offset(1,0).Row它也没有输入下一行的数据当我点击提交。它正在替换我不想要的第一行数据。填写并提交表单时,需要添加到每一行。任何帮助都将受到极大的关注。 excel表的代码如下。
Option Explicit
Dim WrkSheet As Worksheet
Private Sub btnSubmit_Click()
Application.EnableEvents = False
Dim ssheet As Workbook
Dim cellVal1 As String, cellVal2 As String, cellVal3 As String, cellVal4 As String, cellVal5 As String, cellVal6 As String, cellVal7 As String, cellVal8 As String, cellVal9 As String, cellVal10 As String, cellVal11 As String, cellVal12 As String
Dim cellVal13 As String, cellVal14 As String, cellVal15 As String, cellVal16 As String, cellVal17 As String, cellVal18 As String, cellVal19 As String, cellVal20 As String, cellVal21 As String, cellVal22 As String
Dim cellVal23 As String, cellVal24 As String, cellVal25 As String, cellVal26 As String, cellVal27 As String, cellVal28 As String, cellVal29 As String, cellVal30 As String, cellVal31 As String, cellVal32 As String, cellVal33 As String, cellVal34 As String
Dim shtCmb As String
Dim RwLast As Long
shtCmb = Me.cmbListItem1.Value
If shtCmb = "" Then
MsgBox "Please choose a month.", vbOKOnly
Me.cmbListItem1.SetFocus
End If
cellVal1 = Me.cmbListItem1.Text
cellVal2 = Me.cmbListItem2.Text
cellVal3 = Me.cmbListItem3.Text
cellVal4 = Me.TextBox31.Text
cellVal5 = Me.TextBox1.Text
cellVal6 = Me.TextBox2.Text
cellVal7 = Me.TextBox3.Text
cellVal8 = Me.TextBox4.Text
cellVal9 = Me.TextBox5.Text
cellVal10 = Me.TextBox6.Text
cellVal11 = Me.TextBox7.Text
cellVal12 = Me.TextBox8.Text
cellVal13 = Me.TextBox9.Text
cellVal14 = Me.TextBox10.Text
cellVal15 = Me.TextBox11.Text
cellVal16 = Me.TextBox12.Text
cellVal17 = Me.TextBox13.Text
cellVal18 = Me.TextBox14.Text
cellVal19 = Me.TextBox15.Text
cellVal20 = Me.TextBox16.Text
cellVal21 = Me.TextBox17.Text
cellVal22 = Me.TextBox18.Text
cellVal23 = Me.TextBox19.Text
cellVal24 = Me.TextBox20.Text
cellVal25 = Me.TextBox21.Text
cellVal26 = Me.TextBox22.Text
cellVal27 = Me.TextBox23.Text
cellVal28 = Me.TextBox24.Text
cellVal29 = Me.TextBox25.Text
cellVal30 = Me.TextBox26.Text
cellVal31 = Me.TextBox27.Text
cellVal32 = Me.TextBox28.Text
cellVal33 = Me.TextBox29.Text
cellVal34 = Me.TextBox30.Text
RwLast = Worksheets(shtCmb).Range("AI" & Worksheets(shtCmb).Rows.Count).End(xlUp).Row
Worksheets(shtCmb).Range("AI" & RwLast + 1).Value = cellVal1
Worksheets(shtCmb).Range("AJ" & RwLast + 1).Value = cellVal2
Worksheets(shtCmb).Range("A" & RwLast + 1).Value = cellVal3
Worksheets(shtCmb).Range("AH" & RwLast + 1).Value = cellVal4
Worksheets(shtCmb).Range("B" & RwLast + 1).Value = cellVal5
Worksheets(shtCmb).Range("C" & RwLast + 1).Value = cellVal6
Worksheets(shtCmb).Range("D" & RwLast + 1).Value = cellVal7
Worksheets(shtCmb).Range("E" & RwLast + 1).Value = cellVal8
Worksheets(shtCmb).Range("F" & RwLast + 1).Value = cellVal9
Worksheets(shtCmb).Range("G" & RwLast + 1).Value = cellVal10
Worksheets(shtCmb).Range("H" & RwLast + 1).Value = cellVal11
Worksheets(shtCmb).Range("I" & RwLast + 1).Value = cellVal12
Worksheets(shtCmb).Range("J" & RwLast + 1).Value = cellVal13
Worksheets(shtCmb).Range("K" & RwLast + 1).Value = cellVal14
Worksheets(shtCmb).Range("L" & RwLast + 1).Value = cellVal15
Worksheets(shtCmb).Range("M" & RwLast + 1).Value = cellVal16
Worksheets(shtCmb).Range("N" & RwLast + 1).Value = cellVal17
Worksheets(shtCmb).Range("O" & RwLast + 1).Value = cellVal18
Worksheets(shtCmb).Range("P" & RwLast + 1).Value = cellVal19
Worksheets(shtCmb).Range("Q" & RwLast + 1).Value = cellVal20
Worksheets(shtCmb).Range("R" & RwLast + 1).Value = cellVal21
Worksheets(shtCmb).Range("S" & RwLast + 1).Value = cellVal22
Worksheets(shtCmb).Range("T" & RwLast + 1).Value = cellVal23
Worksheets(shtCmb).Range("U" & RwLast + 1).Value = cellVal24
Worksheets(shtCmb).Range("V" & RwLast + 1).Value = cellVal25
Worksheets(shtCmb).Range("W" & RwLast + 1).Value = cellVal26
Worksheets(shtCmb).Range("X" & RwLast + 1).Value = cellVal27
Worksheets(shtCmb).Range("Y" & RwLast + 1).Value = cellVal28
Worksheets(shtCmb).Range("Z" & RwLast + 1).Value = cellVal29
Worksheets(shtCmb).Range("AA" & RwLast + 1).Value = cellVal30
Worksheets(shtCmb).Range("AB" & RwLast + 1).Value = cellVal31
Worksheets(shtCmb).Range("AC" & RwLast + 1).Value = cellVal32
Worksheets(shtCmb).Range("AD" & RwLast + 1).Value = cellVal33
Worksheets(shtCmb).Range("AF" & RwLast + 1).Value = cellVal34
Application.EnableEvents = True
End Sub
Private Sub cmbListItem1_Change()
End Sub
Private Sub optionCancel_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim SH As Worksheet
Dim Entry As Variant
' MonthName(Month(Now)) - Will return the name of the current Month
For Each SH In ThisWorkbook.Worksheets
If SH.Name = MonthName(month(Now)) Then
Set WrkSheet = SH
Exit For
End If
Next
'fill the combo box
With Me.cmbListItem1
For Each Entry In [List1]
.AddItem Entry
Next Entry
.Value = MonthName(month(Now))
End With
'fill the combo box
With Me.cmbListItem2
For Each Entry In [List2]
.AddItem Entry
Next Entry
End With
'fill the combo box
With Me.cmbListItem3
For Each Entry In [List3]
.AddItem Entry
Next Entry
End With
End Sub
答案 0 :(得分:0)
我并非100%了解您的应用程序的设计,但我认为以下内容将有所帮助:
Range("AI" & ActiveSheet.Rows.Count, 2).End(xlUp).Offset(1, 0).Row
是无效的语法。我想你想要的是Range("AI" & ActiveSheet.Rows.Count).End(xlUp).Offset(1, 0).Row
。这样做,是查看第AI
列中最后一行使用的第一个空行。但它需要稍微修改一下..
Range
这里可能指的是ActiveSheet
,它不是月份表,而是指Userform所在的工作表。通常,Range
是Activeheet.Range
的快捷方式(如果代码位于VBA
模块中),或者它是指输入它的工作表(如果输入的话)一个表格模块),偶尔会有危险。
一个好的做法是完全限定范围:Worksheets(shtCmb).Range("AI" & Worksheets(shtCmb).Rows.Count).End(xlUp).Offset(1, 0).Row
代替:这可以保证您正在查看目标工作表AI
列的最后一行。
那么,为什么要转到lastrow + 1
而不是lastrow
?
请记住Integers
的范围仅限于32,767
,并且每当行号超过此值时都会出错。我会改用Long
。实际上,Integers
无论如何都会在内部转换为Long
(但是如果它们超出了它们的范围仍然会出错!),那么为什么不在第一个地方使用多头。
我希望这可以让你开始,但请通过以下评论告诉我是否有任何修改可以提供帮助。
最佳, 扬
PS:同样,你做了太多艰苦的工作,也许考虑定义数组而不是所有这些变量。修改强>
it is skipping a row per each entry submited
这是因为它标识了RwLast
,这是第一行有空单元格,它将新内容写入下面的一行:
Worksheets(shtCmb).Range("AI" & RwLast + 1).Value = cellVal1
等等。要解决此问题,请更改
RwLast = Worksheets(shtCmb).Range("AI" & Worksheets(shtCmb).Rows.Count).End(xlUp).Offset(1, 0).Row
到
RwLast = Worksheets(shtCmb).Range("AI" & Worksheets(shtCmb).Rows.Count).End(xlUp).Row
<强> EDIT2:强>
添加Application.EnableEvents=False
可确保在Sub btnSubmit_Click()
执行时不会自动触发任何内容。但是,我们需要小心。子程序应该像这样开始:
Sub btnSubmit_Click()
On Error GoTo errHandler
Application.EnableEvents = False
并像这样结束
errHandler:
Application.EnableEvents = True
End Sub
然后btnSubmit_Click
中的任何内容都不会更改Row 2
的shtCmb
,除非 WrkSheet = Worksheets(shtCmb)
和 {{1 }}。目前尚不清楚RwLast = 1
是如何定义的,但我认为你已经解决了这个问题。