Excel 如何用函数输出替换范围,并覆盖范围值?

Excel 如何用函数输出替换范围,并覆盖范围值?,excel,vba,Excel,Vba,我有一个vba代码,看起来像这样,试图用FuzzyVLookup函数的输出替换一个单元格,目前,我可以读取单元格mAr(I,1),并有一个FuzzyVLookup(mAr(I,1),inRng1,1)的输出,但我无法覆盖初始mAr(I,1) 提前谢谢你的帮助 Public Sub Test() Dim StartTime As Double Dim MinutesElapsed As String 'Remember time when macro starts StartTime = Ti

我有一个vba代码,看起来像这样,试图用FuzzyVLookup函数的输出替换一个单元格,目前,我可以读取单元格mAr(I,1),并有一个FuzzyVLookup(mAr(I,1),inRng1,1)的输出,但我无法覆盖初始mAr(I,1)

提前谢谢你的帮助

Public Sub Test()

Dim StartTime As Double
Dim MinutesElapsed As String

'Remember time when macro starts
StartTime = Timer
    '---------------------------------------------------------------
    '-- Select Range A for comparison to be compared with Range B --
    '---------------------------------------------------------------
    Const LBLS  As String = "Names to be matched "
    Const xNAME As String = "Fuzzy Match Lead"

    Set inRng = Application.Selection
    Set inRng = Application.InputBox(LBLS, xNAME, inRng.Address, Type:=8)

    '-----------------------------------------------
    '-- Select Range B for Range A to be compared --
    '-----------------------------------------------
    Const LBLS1  As String = "Names to be matched to "
    Const xNAME1 As String = "Fuzzy Match Source"

    Set inRng1 = Application.Selection
    Set inRng1 = Application.InputBox(LBLS1, xNAME1, inRng.Address, Type:=8)

    If inRng.Columns.Count > 1 Or inRng.Areas.Count > 1 Then
        MsgBox "Please select a single (continuous) column"
        Exit Sub
    End If

    allRows = inRng.Rows.Count
    If inRng.Count = 1 Then     'if only one cell selected force mAr to array
        ReDim mAr(1, 1)
        mAr(1, 1) = inRng.Value2
    Else
        mAr = inRng.Value2
    End If

    For i = 1 To allRows
        mAr(i, 1) = FuzzyVLookup(mAr(i, 1), inRng1, 1)
    Next

    inRng = mAr                 'place memory array back to range

'Determine how many seconds code took to run
MinutesElapsed = Format((Timer - StartTime) / 86400, "hh:mm:ss")

'Notify user in seconds
MsgBox "This code ran successfully in " & MinutesElapsed & " minutes", vbInformation


End Sub

你到底面临什么问题?将数组写入范围?或者MsgBox mAr(i,1)是否显示空值。尝试Dim mAr(1对1,1对1)。您的范围是一个单元格,但数组默认从索引0开始。@Christov您好,谢谢您的评论,我在放置Dim mAr(1到1,1到1)后尝试运行代码,我遇到了一个编译错误:数组被标注尺寸,我是否做错了smth?我希望这些行已经重写并替换为inRng1中的值。然而,即使这两行代码都实现了(1)mAr(i,1)=FuzzyVLookup(mAr(i,1),inRng1,1)和(2)inRng=mArSo replace
ReDim-mAr(1,1)
ReDim-mAr(1到1,1到1)@Christov hi,inRng也没有改变inRng。它仍然不起作用。只是好奇,只有当我在inRng右侧选择一行时,才会应用redim?还是会应用于整个代码?