Excel 如果满足条件,则替换单元格值
希望这个问题对你有帮助。我试图使用VBA在excel中进行一些测试,但遇到了一个问题。 下面是我写的代码,但它没有按照我的期望工作 详情:Excel 如果满足条件,则替换单元格值,excel,vba,Excel,Vba,希望这个问题对你有帮助。我试图使用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添加 下面是代码应该做的事情:一旦我在userf
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中单击submit按钮,它应该首先将Shift中指定的值(Me.cmb_.S.value)粘贴到一个范围内(在此之前它还可以),然后如果我输入“WO”作为条目(查看第一个屏幕快照1),代码应该用“WO”更改单元格值。
示例:如果我选择星期二(星期二)和星期三(星期三)的“WO”,并且我的班次是晚上(E),那么它应该首先用E填充所有单元格(范围E32:AI32),然后用所有星期二和星期三的“WO”替换“E”。
下面的代码有点慢,它给我的结果如屏幕截图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
你用
For m = 5 To 29
...
m = m + 6 'here you increase counter by 6
Next 'here you increase counter by 1
因此,在上一个预期循环之前,计数器(m)已经=32,因此它>然后29,因此循环结束。这就是为什么最后几天被排除在外你必须稍微改变一下你的循环
如果day name的行始终为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
没有测试它,所以可能需要一些调整非常感谢,效果非常好。竖起大拇指!