首页 > 解决方案 > 引用具有多个变量的单元格

问题描述

我分别定义了 2 个变量字符串和整数。使用需要的变量来引用单元格并提取数据。有人可以帮忙吗?

With Page 3

.activate
Dim OracleProjType as String -> stores the value "C"
Dim SearchProjNameRow as Integer -> Stores the value "6"
Dim OracleProjTypeData as string -> Stores the data of "C6" cell

OracleProjTypeData = .Range(" " & OracleProjType & ":" & SearchProjNameRow).Value  -> Getting an error here as "Run time error 1004, Application - Object defined error"

End with

标签: vbaexcel

解决方案


尝试

Option Explicit

Public Sub TEST()

    Dim OracleProjType As String
    Dim SearchProjNameRow As Long
    Dim OracleProjTypeData As String

    OracleProjType = "C" '<==Assign the value to the variable
    SearchProjNameRow = 6

    With Worksheets("Sheet3") '<== Work with that sheet using with so no activate
        OracleProjTypeData = .Range(OracleProjType & SearchProjNameRow).Value '<==Concatenate values to create range reference
        Debug.Print OracleProjTypeData
    End With
End Sub

推荐阅读