在VBA中使用单元格引用公式返回要在VBA代码中使用的单元格

时间:2018-08-24 07:02:56

标签: excel vba excel-vba indexing match

我正在尝试使用具有INDEX和MATCH的公式来返回单元格引用,以使用VBA输入TEXT。 我在A列中有一个供应商列表,要找到它右边的单元格,可以使用以下

print("Hello, world!")

但是,我在如何在VBA代码中获取该代码方面很挣扎。 (请注意,值***会发生变化,因为我需要为不同的供应商多次运行sub。 我可以为此使用Function sub吗?我尝试了以下方法,但没有运气:

=CELL("address";INDEX(A29:C42;MATCH("***";A29:A42;0);2))

3 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,您想匹配A列中的值/供应商,并将单元格的地址返回其右侧吗?

您可以这样做:

Cell2WorkWith = Cells(Application.Match(Searchvalue, Searchrange, 0),2).Address

如果您不希望使用“ $”,请像这样替换它们:

Cell2WorkWith = Replace(Cells(Application.Match(Searchvalue, Searchrange, 0),2).Address,"$","")

或者甚至更好,例如:

Cell2WorkWith = Cells(Application.Match(Searchvalue, Searchrange, 0),2).Address(0,0)

如果我误解了,那是右边那个单元格中的值,那么下面的代码将起作用:

Value2WorkWith = Cells(Application.Match(Searchvalue, Searchrange, 0),2).Value

记住,仅当可以在范围内找到该值时才使用match,否则您将不得不捕获一个错误。

答案 1 :(得分:0)

一种选择是使用“查找”查找单元格:

Option Explicit

Sub CellRef()

Dim SearchString As String
Dim ra, cell, VendorsRange As Range
Dim k As Integer


Set VendorsRange = Range("E1:E10")
k = 1

For Each cell In VendorsRange

SearchString = cell.Value

    Set ra = Range("A29:A42").Find(What:=SearchString, LookIn:=xlValues, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox "String not available"
    Else
        Range("D" & k).Value = ra.Offset(0, 1).Address           'Change "D1" to whereever you want to put your result in
    End If
k = k + 1

Next cell

End Sub

该代码将检查每个供应商(在我的代码范围“ E1:E10”中),范围A29:A42中的单元格在哪里,并返回其旁边的单元格地址。

答案 2 :(得分:0)

某些人更喜欢查找所有出现的被搜索项,然后更改值或公式,或者执行其他操作。这是一些允许使用数组的极大灵活性的代码。

'**************************************************************************************************************************************************************
'To return an array of information (value, formula, address, row, and column) for all the cells from a specified Range that have the searched item as value
'Returns an empty array if there is an error or no data
'**************************************************************************************************************************************************************
Public Function makeArrayFoundCellInfoInRange(ByVal itemSearched As Variant, ByVal aRange As Variant) As Variant
Dim cell As Range, tmpArr As Variant, x As Long

tmpArr = Array()
If TypeName(aRange) = "Range" Then
    x = 0
    For Each cell In aRange
        If itemSearched = cell.Value Then
            If x = 0 Then
                ReDim tmpArr(0 To 0, 0 To 4)
            Else
                tmpArr = reDimPreserve(tmpArr, UBound(tmpArr, 1) + 1, UBound(tmpArr, 2))
            End If
            tmpArr(x, 0) = cell.Value
            tmpArr(x, 1) = cell.Formula
            tmpArr(x, 2) = cell.Address(0, 0) 'Without the dollar signs
            tmpArr(x, 3) = cell.Row
            tmpArr(x, 4) = cell.Column
            x = x + 1
        End If
    Next cell
End If
makeArrayFoundCellInfoInRange = tmpArr
Erase tmpArr

End Function
相关问题