excel 基于数组中的所有值填充单元格,如果单元格区域中不存在值,则追加这些值

eh57zj3b  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(234)

我有一个值数组result,它是从REST API调用中获得的。result = [1,2,3,4,5]
"开始时"
我想输入数组result中的每个值,以填充从A1A5的单元格(该范围是动态的,基于数组中值的数量,因此可能不是始终为A5)。
因此,如果范围(A1-A100)为空,我们将正常填充单元格。

随着结果数组的增长

由于再次运行宏时结果会增加,例如,15分钟后result变为[1,2,3,4,5,6,7,8]
因此,如果范围(A1-A5)不为空,如果数组的附加项未出现在范围中(意味着它们是附加项),则将它们追加到单元格范围的后面
我在想,如果范围(A1-A5)是空的,我可能应该这样做**:
给定result = [1,2,3,4,5]

'the beginning part' 
i = 1
Set rng = Range(“A1:A5”)

If WorksheetFunction.CountA(Range("A1:A5")) = 0 Then
    For Each cel In rng:
        result(i) = cel.Value
        i = i + 1
    Next cel

但是,我认为代码中存在一个主要问题&数组增长时缺少一些部分,因为
1.数组中的项数是不确定的,所以不应该硬编码值,应该使其动态
1.当结果数组增长时,我不确定如何仅将额外的项追加到单元格列表的后面,这包括(1)过滤掉数组中未出现在范围中的项(2)将项追加到正确的位置
任何帮助将不胜感激,提前感谢。

omhiaaxx

omhiaaxx1#

附加唯一值

用法

Sub AppendUniqueTest()
    
    Dim Arr() As Variant: Arr = Array(1, 2, 3, 4, 5)
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Sheets("Sheet1")
    
    AppendUnique Arr, ws, "A1"

End Sub

"方法"

Sub AppendUnique( _
        Arr() As Variant, _
        ByVal ws As Worksheet, _
        ByVal FirstCellAddress As String, _
        Optional ByVal OverWrite As Boolean = False)
   
    ' Write the data from the source range to the source array ('sData').
    ' Reference the first destination cell ('fCell').

    If ws.FilterMode Then ws.ShowAllData
    
    Dim fCell As Range: Set fCell = ws.Range(FirstCellAddress)
    
    Dim sData() As Variant, srCount As Long
    
    With fCell
        Dim lCell As Range: Set lCell = .Resize(ws.Rows.Count - .Row + 1) _
            .Find("*", , xlFormulas, , , xlPrevious)
        If Not lCell Is Nothing Then
            srCount = lCell.Row - .Row + 1
            If srCount = 1 Then
                ReDim sData(1 To 1, 1 To 1): sData(1, 1) = .Value
            Else
                sData = .Resize(srCount).Value
            End If
            If Not OverWrite Then Set fCell = lCell.Offset(1)
        End If
    End With
            
    ' Write the unique data from the source array to a dictionary.
    
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare
    
    Dim sr As Long
    
    For sr = 1 To srCount: dict(CStr(sData(sr, 1))) = Empty: Next sr
    
    Erase sData
    
    ' Define the destination array ('dData').
    
    Dim lb As Long: lb = LBound(Arr)
    Dim ub As Long: ub = UBound(Arr)
    
    Dim dData() As Variant: ReDim dData(1 To ub - lb + 1, 1 To 1)
                 
    ' Check the values from the given array ('Arr') against the values
    ' in the dictionary and write the non-matches to the destination array.
    
    Dim dr As Long, c As Long, cString As String
                 
    For c = lb To ub
        cString = CStr(Arr(c))
        If Len(cString) > 0 Then ' is not blank
            If Not dict.Exists(cString) Then ' is not in the dictionary
                dict(cString) = Empty ' prevent dupes from the given array
                dr = dr + 1
                dData(dr, 1) = cString
            End If
        End If
    Next c
    
    If dr = 0 Then
        MsgBox "No new values found.", vbExclamation
        Exit Sub
    End If
    
    ' Write the values from the destination array to the destination range.
    
    fCell.Resize(dr).Value = dData
    If OverWrite Then ' clear below
        fCell.Resize(ws.Rows.Count - fCell.Row - dr + 1).Offset(dr).Clear
    End If
        
    ' Inform.
        
    MsgBox "Data appended.", vbInformation
         
End Sub

相关问题