希望这个问题找到你。我正在尝试使用VBA在Excel中进行一些测试但是我遇到了一个问题。 下面是我写的代码,但它没有按照我的期望工作。
详细说明:
Me.txt_EN.Value is Employee Name
Me.cmb_S.Value is Shift
Me.cmb_M.Value is Month
Worksheet Name = Test
Userform Name is Userform1
以上值将通过Excel中的userform添加。
以下是代码应该做的事情:一旦我点击了我的userform1中的提交按钮,它应该首先将Shift(Me.cmb_S.Value)中指定的值粘贴到一个范围内(它在这里做得很好)然后如果我输入“WO”作为条目(查看第一个屏幕截图1),代码应该用“WO”更改单元格值。 示例:如果我选择星期二(星期二)和星期三(星期三)的“WO”和我的班次作为晚上(E),它应首先用E填充所有单元格(范围E32:AI32),然后用“WO”替换“E” “适用于所有星期二和星期三。Screenshot1 Screenshot2
下面的代码有点慢,它给我的结果如屏幕截图2所示,它总是排除上周二和周三(看看1/30/18和1/3/18,它们应该改为“WO “)
任何帮助解决这个问题都将受到赞赏,也有一种方法可以添加规则,以便同一个用户不能多次填写此表单?
Private Sub cmd_Submit_Click()
'select worksheet
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("RawData")
Dim i As Integer
Dim m As Integer
Dim n As Integer
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows,
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'copy the data to the database
For i = 5 To 36
ws.Cells(iRow, 1).Value = Me.txt_EN.Value
ws.Cells(iRow, i).Value = Me.cmb_S.Value
ws.Cells(iRow, 37).Value = Me.cmb_M.Value
Next i
For m = 5 To 29
If Me.cmb_mon.Value = "WO" Then ws.Cells(iRow, m).Value = "WO"
If Me.cmb_Tue.Value = "WO" Then ws.Cells(iRow, m + 1).Value = "WO"
If Me.cmb_Wed.Value = "WO" Then ws.Cells(iRow, m + 2).Value = "WO"
If Me.cmb_thu.Value = "WO" Then ws.Cells(iRow, m + 3).Value = "WO"
If Me.cmb_Fri.Value = "WO" Then ws.Cells(iRow, m + 4).Value = "WO"
If Me.cmb_Sat.Value = "WO" Then ws.Cells(iRow, m + 5).Value = "WO"
If Me.cmb_Sun.Value = "WO" Then ws.Cells(iRow, m + 6).Value = "WO"
m = m + 6
If m > 35 Then Exit For Else
Next
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
Unload Me
UserForm1.Show
End Sub
答案 0 :(得分:0)
您使用
For m = 5 To 29
...
m = m + 6 'here you increase counter by 6
Next 'here you increase counter by 1
所以在你最后一个预期的循环之前,计数器(m)已经是= 32所以它的>然后29所以循环结束。这就是为什么排除前几天的原因。
你必须稍微改变你的循环。
如果带有日期名称的行始终为30,则可以使用类似这样的内容
Private Sub cmd_Submit_Click()
'select worksheet
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("RawData")
Dim i As Integer
Dim m As Integer
Dim n As Integer
Dim Mon As Boolean, Tue As Boolean, Wed As Boolean, Thu As Boolean
DIM Fri As Boolean, Sat As Boolean, Sun As Boolean
DIM aCell as Range
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows,
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'copy the data to the database
For i = 5 To 36
ws.Cells(iRow, 1).Value = Me.txt_EN.Value
ws.Cells(iRow, i).Value = Me.cmb_S.Value
ws.Cells(iRow, 37).Value = Me.cmb_M.Value
Next i
If Me.cmb_mon.Value = "WO" Then Mon = True
If Me.cmb_Tue.Value = "WO" Then Tue = True
If Me.cmb_Wed.Value = "WO" Then Wed = True
If Me.cmb_thu.Value = "WO" Then Thu = True
If Me.cmb_Fri.Value = "WO" Then Fri = True
If Me.cmb_Sat.Value = "WO" Then Sat = True
If Me.cmb_Sun.Value = "WO" Then Sun = True
For Each aCell In ws.Range("E30:AI30")
If aCell = "Mon" And Mon Then ws.Cells(iRow, aCell.Column).Value = "WO"
If aCell = "Tue" And Tue Then ws.Cells(iRow, aCell.Column).Value = "WO"
If aCell = "Wed" And Wed Then ws.Cells(iRow, aCell.Column).Value = "WO"
If aCell = "Thu" And Thu Then ws.Cells(iRow, aCell.Column).Value = "WO"
If aCell = "Fri" And Fri Then ws.Cells(iRow, aCell.Column).Value = "WO"
If aCell = "Sat" And Sat Then ws.Cells(iRow, aCell.Column).Value = "WO"
If aCell = "Sun" And Sun Then ws.Cells(iRow, aCell.Column).Value = "WO"
Next
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
Unload Me
UserForm1.Show
application.screenupdating = true
application.Calculation = xlCalculationAutomatic
End Sub
没有对它进行测试,因此可能需要进行一些调整