如果在一列中满足Excel条件,则将匹配条件旁边的列的值记录到一个范围中

If an Excel criteria is met in one column, record the value of the column next to the matching criteria into a range

也许我只是没有找到正确的地方,但我认为这并不像我发现的那样困难。

我有一个无法排序的跨页sheet(它在生产线上,连接到个人 sheet,这些个人 sheet 会引入其他数据,由许多不同的人填写)。进来的数据基本上是随机的,看起来像这样:

''    Type    Sieve #40
''
''    Truck       55%
''    Truck       55%
''    CoA         48%
''    Basement    55%
''    Bin2        55%
''    Bin1        55%
''    Hopper      57%
''    Basement    58%
''    Bin2        54%
''    Bin1        58%
''    Hopper      56%
''    Truck       56%
''    CoA         47%
''    Basement    55%
''    Bin2        57%
''    Bin1        61%
''    Hopper      50%

现在,我需要一个可以找到样本类型(卡车、bin1 等)并将对应于该样本类型的每个值放入一个范围内的宏。然后我可以在图表上绘制范围。

例如,"Truck" 范围的数字为 55%、55%、56%。

所以有 6 种不同的样本类型,这意味着 6 个不同的范围,这意味着我的图表上有 6 个不同的系列。

我编写了绘制图表的所有代码,以及将数据收集到这两列中的所有代码。我只是缺少这篇文章。

理想情况下,例如,可以设置一个 For 循环,从第 1 行到 sheet 的最后一行,当它找到 "truck" 时,它赋值"truck" 旁边列中的数字到新数组中的第一个位置。然后 "truck" 的下一个实例填充 "Truck" 数组中的下一个位置,依此类推。

添加 sheet 对我来说几乎是不可能的,因为操作员必须为每个样本添加一个新的 sheet,然后将一些数据拉入此摘要 sheet。

我将您的数据集复制到空白工作簿的 Sheet1 中,将文本用于列,确保 Sheet2 和 Sheet3 存在,并使用此代码将其拆分。您可以根据需要对其进行返工。本质上,我将数据集设置为数组,将值循环到每种类型的单独数组中,然后将每个数组加载到打印数组中。打印数组匹配 value2 维度。中间数组是参差不齐的,因为你不能 redim preserve 多维数组。

Sub SplitSet()
  Dim vInput() As Variant, vTruck() As Variant, vHopper() As Variant, vPrint() As Variant
  Dim l As Long

  'Get values into array
  vInput = Sheet1.UsedRange.Value2

  'Loop through array and load type arrays
  ReDim vTruck(1 To 1)
  ReDim vHopper(1 To 1)
  For l = 1 To UBound(vInput)
    Select Case vInput(l, 2)
      Case "Truck"
        If IsEmpty(vTruck(1)) = False Then ReDim Preserve vTruck(1 To UBound(vTruck) + 1)
        vTruck(UBound(vTruck)) = Array(vInput(l, 2), vInput(l, 3))
      Case "Hopper"
        If IsEmpty(vHopper(1)) = False Then ReDim Preserve vHopper(1 To UBound(vHopper) + 1)
        vHopper(UBound(vHopper)) = Array(vInput(l, 2), vInput(l, 3))
    End Select
  Next l

  'Print
  vPrint = TypeArrayToPrintArray(vTruck)
  Sheet2.Activate
  Sheet2.Range(Cells(1, 1), Cells(UBound(vPrint), 2)).Value2 = vPrint

  vPrint = TypeArrayToPrintArray(vHopper)
  Sheet3.Activate
  Sheet3.Range(Cells(1, 1), Cells(UBound(vPrint), 2)).Value2 = vPrint
End Sub

Function TypeArrayToPrintArray(ByRef vArray() As Variant) As Variant()
  Dim vPrint() As Variant
  Dim l As Long
  ReDim vPrint(1 To UBound(vArray), 1 To 2)
  For l = 1 To UBound(vArray)
    vPrint(l, 1) = vArray(l)(0)
    vPrint(l, 2) = vArray(l)(1)
  Next l

  TypeArrayToPrintArray = vPrint
End Function

编辑 根据 OP 的最后说明

试试这个

Option Explicit

Sub main()

Dim dataRng As Range, dbRng As Range, helperRng As Range, cell As Range, found As Range
Dim rangeArray() As Range
Dim iRng As Long

With Worksheets("MySheet") '<= change it to your actual worksheet name
    Set dbRng = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row).Resize(, 2) '<= change "A1" to your actual data first up-left cell address
    Set dataRng = dbRng.Offset(1).Resize(dbRng.Rows.Count - 1) 'extrapolate data only (headers off)
    'Set helperRng = dbRng.Offset(.UsedRange.Rows.Count + 1, .UsedRange.Columns.Count + 1).Cells(1, 1) ' localize "helper" range out of sheet used range
    Set helperRng = .Range("AA1") ' localize "helper" range from cell "AA1" 
End With
dataRng.Columns(1).SpecialCells(xlCellTypeConstants, xlTextValues).Copy Destination:=helperRng ' copy relevant data into "helper" rang

With helperRng
    If .CurrentRegion.Rows.Count > 1 Then .CurrentRegion.RemoveDuplicates Columns:=Array(1), Header:=xlNo ' take only samples unique values
    With .CurrentRegion
        ReDim rangeArray(1 To .Rows.Count) 'size the array to sample unique values number
        For iRng = 1 To .Rows.Count 'loop through sample unique values
            dbRng.AutoFilter field:=1, Criteria1:=helperRng(iRng, 1) ' filter data accordingly to current sample value
            Set rangeArray(iRng) = dataRng.Columns(2).SpecialCells(xlCellTypeVisible) 'store filtered rows columns 2 ranges
            .AutoFilter
        Next iRng
    End With
'    .ClearContents '<== remove the comment once you're done with the example
End With

' here follows an example of exploiting rangeArray array
' once you're done with it, remove this example and uncomment the ".ClearContents" statement by the end of preceeding "With ... End With" block
For iRng = 1 To UBound(rangeArray)
    rangeArray(iRng).Copy
    helperRng.Offset(iRng - 1, 1).Resize(1, rangeArray(iRng).Count).PasteSpecial xlPasteAll, Transpose:=True
Next iRng

End sub

它将适用于任何不同值的样本