2016-02-09 3 views
0

Я написал свой первый правильный сценарий Excel, но последний шаг, похоже, не работает. Я в том числе весь код с комментариями, но важный короткий блок является последним блоком кода до конца, для петли только после того, как этот комментарий:Excel VBA - Application.Match не работает (IsInArray)

'-------------------- perform search and write into destination

Я также думал, что «ByVal» в моей функции определение также может иметь какое-то отношение к проблеме?

'==================================================================== 
'This proceedure retrieves 'Amount Spent' per campaign form sheets 
'labeled 'jan', 'feb', 'mar', ... and writes them onto 
'rawData' Sheet. 

'From there, data gets updated in the pivot tables on sheets 'search', 
'gdn', 'youtube'. 
'==================================================================== 

Public Month As String 

Private SourceSheet As Worksheet 
Private CampaignsCount As Integer 
Private arrCampaignsAmounts() As Variant 
Private StringsCount As Long 
Private arrStrings() As Variant 
Private strBaba As String 


Public Function IsInArray(ByVal stringToBeFound As String, arr As Variant) As Boolean 
    IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0)) 
End Function 


Public Sub stringComparison() 

Call OptimizeCode_Begin 


'------------------- get user input, which month (sheet) are we working on 

    ChooseMonthUserform.Show 
    Set SourceSheet = Worksheets(Month) 

     With SourceSheet 

      CampaignsCount = Application.WorksheetFunction.CountA(.Range("F:F")) - 1 
      ReDim arrCampaignsAmounts(1 To CampaignsCount, 1 To 2) 


'------------------- read Campaigns/Amounts from .csv, write into an array 

      For i = 1 To CampaignsCount 
       k = 6 
       For j = 1 To 2 
        arrCampaignsAmounts(i, j) = .Cells(i + 11, k).Value 
         k = 9 

       Next j 
      Next i 

     End With 



'-------------------- load strings to search for into another array 

    Set SourceSheet = Sheet2 

     With SourceSheet 

      lastrow = .Cells(Rows.Count, 1).End(xlUp).Row 
      StringsCount = (lastrow - 1)/12 

      ReDim arrStrings(1 To StringsCount, 1 To 2) 

      k = 1 

       For i = 1 To StringsCount 
         arrStrings(i, 1) = .Range("A" & i + k).Value & "_" & .Range("C" & i + k).Value & "_" & .Range("D" & i + k).Value & "_" & .Range("E" & i + k).Value 
         arrStrings(i, 2) = .Range("A" & i + k).Row 

          k = k + 11 
       Next i 

     'Sheet11.Range("A1").Resize(UBound(arrStrings, 1), UBound(arrStrings, 2)) = _ 
     'arrStrings 



'-------------------- perform search and write into destination 

      For i = 1 To UBound(arrStrings) 
       If IsInArray(arrStrings(i, 1), arrCampaignsAmounts) = True Then 
        SourceSheet.Range("H" & arrStrings(i, 2)) = arrCampaignsAmounts(i, 2) 
       Else 
       End If 
      Next i 

     End With 



    '=============================================================================== 
    'TO DO: 

    '> search for strings and write 'Amounts' back into 'rawData' 
    '> redesign the second array so that it takes into account which month 
    '> it is, which row to start from (jan - row #2, feb - row#3, mar - row #4, ...) 
    '> try and read into arrays all at once without loops 
'=============================================================================== 


    Call OptimizeCode_End 
    End Sub 
+0

Ваш массив поиска состоит из двух столбцов, Match не будет работать с этим. Попробуйте вместо VLookUp –

ответ

0

Добавить функцию excel Index() для получения желаемого массива столбцов многомерного массива.

IsInArray = Not IsError(Application.Match(stringToBeFound, Application.Index(arr, , 2), 0)) 
Смежные вопросы