Excel VBA - 查找范围中的值并将相邻单元格存储在变量

时间:2017-06-14 19:06:38

标签: excel vba excel-vba

我的代码现在能够获取一系列值并找到最高,次高等值,并将它们作为变量粘贴到不同的工作簿中。

我现在要做的是找到与特定值相邻的字符串(在本例中为name),并将该字符串粘贴到另一个工作簿中。

我知道我需要用什么来做到这一点,但我说得不对。

评论行之间的部分是问题所在,因为其他所有内容都可以自行运作。

Dim sipRng As Range: Set sipRng = [G1]
Dim rng As Range: Set rng = [C4:C16]
Dim firstVal As Double, secondVal As Double, thirdVal As Double, fourthVal As Double, cel As Range
Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long

For Each cel In sipRng
    If InStr(cel.Value, "Inbound AHT") > 0 Or InStr(cel.Value, "Valid Quality Count") > 0 Or InStr(cel.Value, "Valid Quality %") > 0 Then
        firstVal = Application.Small(rng, 1)
        secondVal = Application.Small(rng, 2)
        thirdVal = Application.Small(rng, 3)
        fourthVal = Application.Small(rng, 4)
    ElseIf InStr(cel.Value, "Outbound Calls Made") > 0 Or InStr(cel.Value, "FCR %") > 0 Or InStr(cel.Value, "FLR %") > 0 Or InStr(cel.Value, "Corrected CSAT Average") > 0 Then
        firstVal = Application.Large(rng, 1)
        secondVal = Application.Large(rng, 2)
        thirdVal = Application.Large(rng, 3)
        fourthVal = Application.Large(rng, 4)
    End If
Next cel

'''''''''''''''''
iRowL = Cells(Rows.Count, 2).End(xlUp).Row
For iRow = 1 To iRowL
    If Not IsEmpty(Cells(iRow, 1)) Then
        For iSheet = ActiveSheet.Index + 1 To Worksheets.Count
            var = Application.Match(Cells(iRow, 2).Value, Worksheets(iSheet).Columns(2), 0)
        Debug.Print var
        Next iSheet
    End If
Next iRow
''''''''''''''

nngr2.Activate
Range("B4") = firstVal
Range("B11") = secondVal
Range("B9") = thirdVal
Range("B6") = fourthVal

这就是我的开始

enter image description here

这是目标,只取代价值,我想要名称(在本例中为字母)

enter image description here

1 个答案:

答案 0 :(得分:1)

这是一个起点,它会加载带有值的二维数组,并将这些值粘贴到活动工作表的E4中:

Sub Macro1()

Dim rng As Range
Dim arr(1 To 4, 1 To 2)

Set rng = [C4:C16]

For i = 1 To 4
    arr(i, 2) = Application.WorksheetFunction.Large(rng, i)
    arr(i, 1) = rng.Cells(Application.WorksheetFunction.Match(arr(i, 2), rng, 0)).Offset(, -1).Value
Next i

nngr2.Range("B4").value = arr(1,1)
nngr2.Range("B11").value = arr(2,1)
nngr2.Range("B9").value = arr(3,1)
nngr2.Range("B6").value = arr(4,1)

End Sub

enter image description here