将数组传递给.Find函数

时间:2018-01-25 13:58:32

标签: arrays excel vba

我有这个宏,我迫切希望将数组传递给.Find函数(该数组被设置为保存列中的值)。代码不断丢弃错误消息,我无法弄清楚原因。 我需要在工作表中查找存储在数组中的那些值,并检查它们出现的那些行。有了这部分我就没有问题了。我似乎没有得到的是,如果我传递给定索引(strArray(5))的数组一切都很好,但当我试图推广过程并获取所需的所有值时,弹出错误消息.. ..我想我搞砸了循环...我不能把它们弄好

enter image description here

Sub test()


Dim fnd As String, FirstFound As String
Dim FoundCell As Range, rng As Range
Dim myRange As Range, LastCell As Range
Dim total As Integer
Dim mysum As Integer
Dim myavg As Integer
Dim mymedi As Integer
Dim mystdev As Integer
Dim mycrows As Integer
Dim mydistro As Integer
Dim strArray() As String
Dim TotalRows As Long
Dim i As Integer

    TotalRows = Sheets("Template").Rows(Rows.Count).End(xlUp).Row
    ReDim strArray(4 To TotalRows)

    For i = 4 To TotalRows
        strArray(i) = Sheets("Template").Cells(i, 1).Value
    Next

    MsgBox "Loaded " & UBound(strArray) & " items!"
    MsgBox strArray(5)

'search tag

For i = 4 To 135
fnd = strArray(i)
Next

Set myRange = ActiveSheet.UsedRange
Set LastCell = myRange.Cells(myRange.Cells.Count)
Set FoundCell = myRange.Find(what:=fnd, after:=LastCell)

'Test to see if anything was found
  If Not FoundCell Is Nothing Then
    FirstFound = FoundCell.Address
  Else
    GoTo NothingFound
  End If

Set rng = FoundCell

'Loop until cycled through all unique finds
  Do Until FoundCell Is Nothing
    'Find next cell with fnd value
      Set FoundCell = myRange.FindNext(after:=FoundCell)

    'Add found cell to rng range variable
      Set rng = Union(rng, FoundCell)

    'Test to see if cycled through to first found cell
      If FoundCell.Address = FirstFound Then Exit Do
  Loop

For i = 4 To 135
'sum test for signedamount
    total = rng.EntireRow.Columns("N")
    mysum = WorksheetFunction.Sum(total)
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("B").Value = mysum
'avg test for signedamount
    total = rng.EntireRow.Columns("N")
    myavg = WorksheetFunction.Average(total)
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("C").Value = myavg
'medi test for signedamount
    total = rng.EntireRow.Columns("N")
    mymedi = WorksheetFunction.Median(total)
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("E").Value = mymedi
'stdev test for signedamount
    total = rng.EntireRow.Columns("N")
    mystdev = WorksheetFunction.StDev(total)
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("F").Value = mystdev
'crows
    mycrows = WorksheetFunction.CountIf(Range("H2:H1000"), strArray(i))
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("V").Value = mycrows

'sum test for amount1
    total = rng.EntireRow.Columns("O")
    mysum = WorksheetFunction.Sum(total)
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("L").Value = mysum
'avg test for amount1
    total = rng.EntireRow.Columns("O")
    myavg = WorksheetFunction.Average(total)
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("M").Value = myavg
'medi test for amount1
    total = rng.EntireRow.Columns("O")
    mymedi = WorksheetFunction.Median(total)
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("O").Value = mymedi
'stdev test for amount1
    total = rng.EntireRow.Columns("O")
    mystdev = WorksheetFunction.StDev(total)
    Sheets("Template").Columns("A").Find(strArray(i)).Rows.Columns("P").Value = mystdev
'distro
Next

Exit Sub

'Error Handler
NothingFound:
      MsgBox "No values were found in this worksheet"

    End Sub

0 个答案:

没有答案