按范围内的行和列内容查找单元格并将其设置为整数

时间:2014-03-11 16:26:46

标签: vba excel-vba worksheet-function excel

我是VBA的新手。 如何识别与包含文本的行交叉的单元格" This Row"和一个包含文本的列#34;此列"并将其内容设置为名为MyContent的整数?这应该在从A1开始的1000行和50列的范围内搜索。

1 个答案:

答案 0 :(得分:0)

试试这个:

Sub test()
    Dim rngCol As Range, rngRow As Range
    Dim myContent As Integer

    With ThisWorkbook.Worksheets("Sheet1")
        Set rngCol = .Cells.Find(What:="This Column", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
        Set rngRow = .Cells.Find(What:="This Row", LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)

        If rngRow Is Nothing Or rngCol Is Nothing Then
            MsgBox "Cell with text 'This Row' in row and with text 'This Column' in column not found"
            Exit Sub
        End If

        myContent = .Cells(rngRow.Row, rngCol.Column).Value
    End With
End Sub