首页 > 解决方案 > 如何在 Excel VBA 中的 .Range 中使用字符串?

问题描述

我需要你的帮助来解决我遇到的问题。

基本上我想做一个变量iRow,通过找到一个具有值OBRC的单元格,具有这个值的单元格由命令检索find,然后我从行中获取值并将其视为一个字符串操作字符串以仅获取行号作为出口,直到这部分没什么大不了的。但是当我需要将此字符串变量CellString用作范围值的一部分时,它不起作用。

我需要这个来使我的脚本工作,因为我需要这个值来在电子表格上写入信息,所以我想知道如何解决它?我正在考虑将此字符串转换为双精度,但我不知道该怎么做。

这是我的代码:

Function FindCell()

 Dim CellLocation As Range
 Dim CellString As String

  
    Set CellLocation = Sheets("Sheet1").Range("A1048576").Find("OBRC") ' retrieves the value OBRC + the row.

        CellString = Right(CellLocation.Text, 0) ' Manipulate the entire value OBEC + Row and retrieves only the row.
    
    With ThisWorkbook.Sheets("Sheet1")
            .Range("A" & CellString).Value = "Teste" 'it concatenate the Column + the row, it should be working but is not
    End With
 
End Function

随意给我任何建议。

标签: excelvba

解决方案


见注释:

Function FindCell()
    Dim CellLocation As Range
    Set CellLocation = Sheets("Sheet1").Range("A:A").Find("OBRC") 'Returns the cell in which OBRC is found

    If Not CellLocation Is Nothing Then
        Dim CellString As Long
        CellString = CellLocation.Row 'Get the Row.

        With ThisWorkbook.Sheets("Sheet1")
            .Range("A" & CellString).Value = "Teste"
        End With
    End If
End Function

我将其保留为函数,但如果不打算将值返回给外部源,那么这应该是Sub()


推荐阅读