Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 除了用户输入的内容之外,如何附加此值?_Excel_Vba - Fatal编程技术网

Excel 除了用户输入的内容之外,如何附加此值?

Excel 除了用户输入的内容之外,如何附加此值?,excel,vba,Excel,Vba,我有一个脚本,它实现了FindNext,以便在工作表中搜索一行,以匹配用户通过Application.InputBox输入的两列值。工作完美无瑕。但有一个实例,我希望其中一个msgbox在该行中显示另一个单元格值,而不需要用户输入 示例:用户输入A列中的职务代码000001,C列中的成本中心100100010001 msgbox将匹配它并说:msgbox“作业代码(&lJobCode&”)符合此成本中心的条件。 如果我还希望该消息框添加在另一列中找到的值,但是自动添加呢?比如,没有用户输入,因为

我有一个脚本,它实现了
FindNext
,以便在工作表中搜索一行,以匹配用户通过
Application.InputBox
输入的两列值。工作完美无瑕。但有一个实例,我希望其中一个msgbox在该行中显示另一个单元格值,而不需要用户输入

示例:用户输入A列中的职务代码
000001
,C列中的成本中心
100100010001

msgbox将匹配它并说:
msgbox“作业代码(&lJobCode&”)符合此成本中心的条件。

如果我还希望该消息框添加在另一列中找到的值,但是自动添加呢?比如,没有用户输入,因为他们不会马上知道这些信息

新的MsgBox示例:
MsgBox“作业代码(&lJobCode&”)符合此(“&Column H Value for this row&”)成本中心的条件。

我想用一个
str
变量来引用列,但在匹配行后让VBA触发该值时遇到了问题

脚本的其余部分:

Option Explicit
Sub findJC_CC()

Dim wsData As Worksheet
Dim rFound As Range
Dim lJobCode As String
Dim lCC As String
Dim sFirst As String
Dim matched As Boolean

lJobCode = Application.InputBox("Please provide a job code", "Job Code", Type:=2)
If lJobCode = "False" Then Exit Sub 'Pressed cancel
lCC = Application.InputBox("Please enter in a cost-center", "CC", Type:=2)
If lCC = "False" Then Exit Sub 'Pressed cancel

matched = False
Set wsData = ThisWorkbook.Worksheets("Sheet2")
Set rFound = wsData.Columns("A").Find(lJobCode, wsData.Cells(wsData.Rows.Count, "A"), xlValues, xlWhole)

If Not rFound Is Nothing Then
    sFirst = rFound.Address
    Do

        If rFound.Offset(, 2).Value = lCC Then
            matched = True

            'if criteria is met, display msgbox and exit
            If rFound.Offset(, 4).Value = "Exempt" Then
                MsgBox "The business identified this exempt job as being eligible for schedule pay allowance ."
                Exit Sub
            End If

            'if criteria is met, display msgbox and exit
            If rFound.Offset(, 5).Value = "Eligible - Employee Level" Then
                MsgBox "This job is only eligible at the employee level. If you have further questions, please reach out to your HRBP."
                Exit Sub
            End If

            'if non-exempt role, and matched, display msgbox and exit
            MsgBox "Job Code (" & lJobCode & ") is eligible for this cost-center."
            Exit Sub
        End If

        'loop after first address found until column C has lCC value matched
        Set rFound = wsData.Columns("A").FindNext(rFound)
    Loop While rFound.Address <> sFirst

    'lJobCode value matched, lCC value not matched
    If Not matched Then MsgBox "Job Code (" & lJobCode & ") found, but not eligible for this cost-center."
Else

    'lJobCode not matched
    MsgBox "Job Code (" & lJobCode & ") not eligible."

End If

End Sub
选项显式
子findJC_CC()
将wsData设置为工作表
暗光范围
Dim lJobCode作为字符串
作为字符串的Dim lCC
第一个作为字符串
作为布尔值匹配
lJobCode=Application.InputBox(“请提供职务代码”,“职务代码”,类型:=2)
如果lJobCode=“False”则按“取消”退出Sub
lCC=Application.InputBox(“请在成本中心输入”,“抄送”,类型:=2)
如果lCC=“False”,则按“取消”退出子系统
匹配=错误
设置wsData=ThisWorkbook.Worksheets(“Sheet2”)
设置rFound=wsData.Columns(“A”).Find(lJobCode,wsData.Cells(wsData.Rows.Count,“A”),xlValues,xlWhole)
如果没有找到,那就什么都不是了
sFirst=rFound.Address
做
如果rFound.Offset(,2).Value=lCC,则
匹配=真
'如果满足条件,则显示msgbox并退出
如果rFound.Offset(,4).Value=“免税”,则
MsgBox“业务部门认定此免税工作符合计划工资津贴的资格。”
出口接头
如果结束
'如果满足条件,则显示msgbox并退出
如果rFound.Offset(,5).Value=“合格-员工级别”,则
MsgBox“此职位仅适用于员工级别。如果您还有其他问题,请联系您的HRBP。”
出口接头
如果结束
'如果是非豁免角色,并且匹配,则显示msgbox并退出
MsgBox“作业代码(&lJobCode&”)符合此成本中心的条件
出口接头
如果结束
'在找到第一个地址后循环,直到列C的lCC值匹配为止
Set rFound=wsData.Columns(“A”).FindNext(rFound)
查找时循环。地址sFirst
'lJobCode值匹配,lCC值不匹配
如果不匹配,则找到MsgBox“作业代码(&lJobCode&)”,但不符合此成本中心的条件
其他的
'lJobCode不匹配
MsgBox“作业代码(&lJobCode&”)不合格。”
如果结束
端接头

如果我理解正确,变量
rFound
指向a列中要偏移的单元格。如果是这种情况,您可以编写如下代码:

MsgBox "Job Code (" & lJobCode & ") is eligible for this (" & rFound.Offset(,7).Value & ") cost-center.

如果我理解正确,变量
rFound
指向a列中要偏移的单元格。如果是这种情况,您可以编写如下代码:

MsgBox "Job Code (" & lJobCode & ") is eligible for this (" & rFound.Offset(,7).Value & ") cost-center.
您可以添加该行:

Option Explicit
Sub findJC_CC()

Dim wsData As Worksheet
Dim rFound As Range
Dim lJobCode As String
Dim lCC As String
Dim sFirst As String
Dim matched As Boolean

lJobCode = Application.InputBox("Please provide a job code", "Job Code", Type:=2)
If lJobCode = "False" Then Exit Sub 'Pressed cancel
lCC = Application.InputBox("Please enter in a cost-center", "CC", Type:=2)
If lCC = "False" Then Exit Sub 'Pressed cancel

matched = False
Set wsData = ThisWorkbook.Worksheets("Sheet2")
Set rFound = wsData.Columns("A").Find(lJobCode, wsData.Cells(wsData.Rows.Count, "A"), xlValues, xlWhole)

If Not rFound Is Nothing Then
    sFirst = rFound.Address
    Do

        If rFound.Offset(, 2).Value = lCC Then
            matched = True

            'if criteria is met, display msgbox and exit
            If rFound.Offset(, 4).Value = "Exempt" Then
                MsgBox "The business identified this exempt job as being eligible for schedule pay allowance ."
                Exit Sub
            End If

            'if criteria is met, display msgbox and exit
            If rFound.Offset(, 5).Value = "Eligible - Employee Level" Then
                MsgBox "This job is only eligible at the employee level. If you have further questions, please reach out to your HRBP."
                Exit Sub
            End If

            'if non-exempt role, and matched, display msgbox and exit
            MsgBox "Job Code (" & lJobCode & ") is eligible for this (" & rFound.Offset(, 7).Value & ") cost-center."
            Exit Sub
        End If

        'loop after first address found until column C has lCC value matched
        Set rFound = wsData.Columns("A").FindNext(rFound)
    Loop While rFound.Address <> sFirst

    'lJobCode value matched, lCC value not matched
    If Not matched Then MsgBox "Job Code (" & lJobCode & ") found, but not eligible for this cost-center."
Else

    'lJobCode not matched
    MsgBox "Job Code (" & lJobCode & ") not eligible."

End If

End Sub
选项显式
子findJC_CC()
将wsData设置为工作表
暗光范围
Dim lJobCode作为字符串
作为字符串的Dim lCC
第一个作为字符串
作为布尔值匹配
lJobCode=Application.InputBox(“请提供职务代码”,“职务代码”,类型:=2)
如果lJobCode=“False”则按“取消”退出Sub
lCC=Application.InputBox(“请在成本中心输入”,“抄送”,类型:=2)
如果lCC=“False”,则按“取消”退出子系统
匹配=错误
设置wsData=ThisWorkbook.Worksheets(“Sheet2”)
设置rFound=wsData.Columns(“A”).Find(lJobCode,wsData.Cells(wsData.Rows.Count,“A”),xlValues,xlWhole)
如果没有找到,那就什么都不是了
sFirst=rFound.Address
做
如果rFound.Offset(,2).Value=lCC,则
匹配=真
'如果满足条件,则显示msgbox并退出
如果rFound.Offset(,4).Value=“免税”,则
MsgBox“业务部门认定此免税工作符合计划工资津贴的资格。”
出口接头
如果结束
'如果满足条件,则显示msgbox并退出
如果rFound.Offset(,5).Value=“合格-员工级别”,则
MsgBox“此职位仅适用于员工级别。如果您还有其他问题,请联系您的HRBP。”
出口接头
如果结束
'如果是非豁免角色,并且匹配,则显示msgbox并退出
MsgBox“作业代码(&lJobCode&”)符合此(&rFound.Offset(,7).Value&”)成本中心的条件
出口接头
如果结束
'在找到第一个地址后循环,直到列C的lCC值匹配为止
Set rFound=wsData.Columns(“A”).FindNext(rFound)
查找时循环。地址sFirst
'lJobCode值匹配,lCC值不匹配
如果不匹配,则找到MsgBox“作业代码(&lJobCode&)”,但不符合此成本中心的条件
其他的
'lJobCode不匹配
MsgBox“作业代码(&lJobCode&”)不合格。”
如果结束
端接头
您可以添加该行:

Option Explicit
Sub findJC_CC()

Dim wsData As Worksheet
Dim rFound As Range
Dim lJobCode As String
Dim lCC As String
Dim sFirst As String
Dim matched As Boolean

lJobCode = Application.InputBox("Please provide a job code", "Job Code", Type:=2)
If lJobCode = "False" Then Exit Sub 'Pressed cancel
lCC = Application.InputBox("Please enter in a cost-center", "CC", Type:=2)
If lCC = "False" Then Exit Sub 'Pressed cancel

matched = False
Set wsData = ThisWorkbook.Worksheets("Sheet2")
Set rFound = wsData.Columns("A").Find(lJobCode, wsData.Cells(wsData.Rows.Count, "A"), xlValues, xlWhole)

If Not rFound Is Nothing Then
    sFirst = rFound.Address
    Do

        If rFound.Offset(, 2).Value = lCC Then
            matched = True

            'if criteria is met, display msgbox and exit
            If rFound.Offset(, 4).Value = "Exempt" Then
                MsgBox "The business identified this exempt job as being eligible for schedule pay allowance ."
                Exit Sub
            End If

            'if criteria is met, display msgbox and exit
            If rFound.Offset(, 5).Value = "Eligible - Employee Level" Then
                MsgBox "This job is only eligible at the employee level. If you have further questions, please reach out to your HRBP."
                Exit Sub
            End If

            'if non-exempt role, and matched, display msgbox and exit
            MsgBox "Job Code (" & lJobCode & ") is eligible for this (" & rFound.Offset(, 7).Value & ") cost-center."
            Exit Sub
        End If

        'loop after first address found until column C has lCC value matched
        Set rFound = wsData.Columns("A").FindNext(rFound)
    Loop While rFound.Address <> sFirst

    'lJobCode value matched, lCC value not matched
    If Not matched Then MsgBox "Job Code (" & lJobCode & ") found, but not eligible for this cost-center."
Else

    'lJobCode not matched
    MsgBox "Job Code (" & lJobCode & ") not eligible."

End If

End Sub
选项显式
子findJC_CC()
将wsData设置为工作表
暗光范围
Dim lJobCode作为字符串
作为字符串的Dim lCC
第一个作为字符串
作为布尔值匹配
lJobCode=Application.InputBox(“请提供职务代码”,“职务代码”,类型:=2)
如果lJobCode=“False”则按“取消”退出Sub
lCC=Application.InputBox(“请在成本中心输入”,“抄送”,类型:=2)
如果lCC=“False”,则按“取消”退出子系统
匹配=错误
设置wsData=ThisWorkbook.Worksheets(“Sheet2”)
设置