在二维数组中查找现有字符串

时间:2018-09-13 15:35:05

标签: excel vba excel-vba

我正在从电子表格中收集数据并将其存储在2-D数组中,其想法是,一旦脚本检测到正在从特定列中读取数据,它就不会读取整行数据(因为这样做会被视为重复)。

代码:

Private Sub LoadData()

   cDOC_DEBUG "Loading document data..."
   Dim x As Long  'Column Data - there is another function that reads when x = 0 = header; else every other value is considered "data"
   Dim y As Long 

   With dataWS
      For x = 1 To LR - 1
         For y = 1 To LC - 1
            If (IsInArray(.Cells(x + 1, y + 1).value, pData())) Then
               cDOC_DEBUG "Added: " & .Cells(x + 1, y + 1).value
               pData(x, y) = Trim(.Cells(x + 1, y + 1).value)
            End If
         Next y
      Next x
   End With

End Sub

Private Function IsInArray(stringToBeFound As String, arrString As Variant) As Boolean
   IsInArray = (UBound(Filter(arrString, stringToBeFound)) > -1)
End Function

Private Sub cDOC_DEBUG(debugText As String)
   If (ThisWorkbook.Worksheets("Settings").Cells(3, 2)) Then
      Debug.Print debugText
   End If
End Sub

一切都很好地加载到数组中,直到我开始实现我的IsInArray函数为止。我看到它与以下事实有关:它正在搜索一维数组,而我的数组是二维数组。因此,它会收到类型不匹配错误。

电子表格中的每一行都是与其自身相关的信息段。

电子表格中的初始数据:

        A           B           C           D
1    header1     header2     header3     header4
2       a           b           c           d
3       w           x           y           z
4       a           h           j           j
5       a           b           j           d
6       w           x           u           z

2x2最终数组:

        0           1           2           3
0    header1     header2     header3     header4
1       a           b           c           d
2       w           x           y           z
3       a           h           j           j

由于Excel第5和6行的Header1&Header2&Header4与Excel第2和3行具有相同的值,因此不会将其读入数组。

问题:

我如何匹配上面的条件以不包括一行中的重复项。

  

Sudo代码示例:

     

如果(添加的值与Header1&Header2&Header3_列中的所有值匹配,则

     

不要添加到数组

我知道的另一个问题是,此数组中将有空白数据。我可以做些什么来1删除这些,还是必须为阵列插槽设置另一个索引来跟踪?

1 个答案:

答案 0 :(得分:1)

您可以循环行/列,并使用-- Perform upsert and return all inserted/updated ids WITH upsert(id) AS ( INSERT INTO target_table (first, second, third, fourth) SELECT first, second, third, fourth FROM source_table ON CONFLICT (id) UPDATE SET first = excluded.first, second = excluded.second, third = excluded.third, fourth = excluded.fourth RETURNING id ) -- Delete any records in target that aren't in source DELETE FROM target_table WHERE id NOT IN (SELECT id FROM upsert); 从数组中切出行/列,并使用Index测试搜索值是否在该列中。与Match结合使用以测试重复项。如果计数等于列数,则忽略值(或列计数-1 ...请参阅下一条注释==>)。对此假想的专栏并不完全确定。您是否打算在开始时使用额外的空白列进行标注?

行版本:

存在:

Count

重复项:

Option Explicit
Public Sub CheckRow()
    Dim arr(), i As Long
    arr = [A1:D6].Value                          '<==2D array created

    For i = LBound(arr, 1) To UBound(arr, 1)     '<== loop rows
        'look in each row for x and if found exit loop and indicate row where found
        If Not IsError(Application.Match("x", Application.WorksheetFunction.Index(arr, i, 0), 0)) Then
            Debug.Print "value found in column " & i
            Exit For
        End If
    Next
End Sub

存在:


列版本:

存在:

Option Explicit
Public Sub CheckRow()
    Dim arr(), i As Long
    arr = [A1:D6].Value                          '<==2D array created

    For i = LBound(arr, 1) To UBound(arr, 1)     '<== loop rows
        'look in each row for more than one "B" and if found exit loop and indicate row where found
         If Application.Count(Application.Match(Application.WorksheetFunction.Index(arr, i, 0), "B", 0)) > 1 Then
            Debug.Print i
            Exit For
        End If
    Next
End Sub

重复项:

您可以使用Option Explicit Public Sub CheckColumn() Dim arr(), i As Long arr = [A1:D6].Value '<==2D array created For i = LBound(arr, 2) To UBound(arr, 2) '<== loop columns 'look in each column for x and if found exit loop and indicate column where found If Not IsError(Application.Match("x", Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Index(arr, 0, i)), 0)) Then Debug.Print "value found in column " & i Exit For End If Next End Sub 检查整列中是否有重复项,并再次用Count切片:

Index

使用工作表中的样本数据:

data