我正在尝试询问员工ID号,然后搜索其他表单以找到该员工。我知道我的Mail_Alert有效,PN正在复制数字,但它没有检测到其他表格的一面。
Sub Find_Employee()
Dim PN As String
PN = Worksheets("Sheet3").Cells(1, "B").Value
Range("B2") = PN
Worksheets("Sheet4").Activate
Range("C2").Activate
Do Until IsEmpty(ActiveCell.Value)
If ActiveCell.Value = "PN" Then
Call Mail_Alert
End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub
答案 0 :(得分:2)
Dim PN As String
此处PN
是变量,程序化符号。
If ActiveCell.Value = "PN" Then
此处"PN"
是符号可以具有的字符串文字,值,例如ActiveCell.Value
即可。但条件可能意味着验证ActiveCell.Value
是否匹配PN
(变量)包含的内容,而不是"PN"
(字面值)。
在VBA中,双引号表示字符串文字。删除它们。
答案 1 :(得分:0)
请更好地使用您的代码,不要使用工作表(“Sheet4”)。激活:
Option Explicit
Sub Find_Employee()
Dim PN As String
Dim sh As Worksheet
Set sh = Worksheets("sheet4")
PN = Worksheets("Sheet3").Cells(1, "B").Value
Range("B2") = PN
With sh
Do Until IsEmpty(.Range("C2").Value)
If .Range("C2").Value = PN Then
Call Mail_Alert
End If
Loop
.Activate 'activate sheet because wants to select Range("C2").Offset(1, 0) for some reason
Range("C2").Offset(1, 0).Select
End With
End Sub