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

时间:2019-05-29 14:15:14

标签: excel vba

我有一个脚本,该脚本实现dataLabels: { enabled: true, useHTML: true, style: { textShadow: 'none',fontSize: '10px',color:'black' }, formatter: function() { return this.point.name; }, } ,以便在工作表中搜索一行以匹配用户通过FindNext输入的两个列值。完美地工作。但是在某些情况下,我希望其中一个msgbox无需用户输入即可在该行中显示另一个单元格值。

示例:用户输入在A列中找到的职务代码Application.InputBox,在C列中找到成本中心000001

msgbox会匹配它并说:100100010001

如果我还希望该消息框添加在另一列中自动找到的值,该怎么办?就像,没有用户输入,因为他们不会立即知道此信息。

新的MsgBox示例:MsgBox "Job Code (" & lJobCode & ") is eligible for this cost-center.

我当时在想类似MsgBox "Job Code (" & lJobCode & ") is eligible for this (" & Column H Value for this row & ") cost-center.的变量并引用该列,但是在行匹配后,很难让VBA触发该值。

其他脚本:

str

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

您可以添加该行:

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