【问题标题】:Type Mismatch Error, declaring Range Variable in Excel VBA类型不匹配错误,在 Excel VBA 中声明范围变量
【发布时间】:2016-04-19 02:06:31
【问题描述】:

这应该是一个简单的修复,但我已经搜索和搜索,无法找出问题所在。

我收到“运行时错误 13:类型不匹配”:

Dim rng As Range    
Dim endRW As Integer, endCol As Integer
endRW = 30
endCol = 10
Set rng = Sheets("Sheet1").Range(Sheets("Sheet1").Cells(2,3),Sheets("Sheet1").Cells(endRW,endCol)) 

^^^最后一行导致错误^^^^

【问题讨论】:

    标签: vb.net excel vba basic


    【解决方案1】:

    试试

    Set rng = Sheets("Sheet1").Range(Cells(2, 3), Cells(endRW, endCol))
    

    证据。

    【讨论】:

    • 那么你正在做一些与我发布的不同的事情。见截图。代码运行良好。
    • 是的,你是对的。问题实际上出在我用来查找错误的 MsgBox 上,哈哈。抱歉,感谢您的帮助。
    • 那么请接受tour中解释的答案
    【解决方案2】:

    试试这个

    Sub temp()
        Dim rng As Range
        Dim endRW As Long, endCol As Long
        endRW = 30
        endCol = 10
        Set rng = Worksheets("Sheet1").Range(Cells(2, 3), Cells(endRW, endCol))
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-25
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 2018-08-11
      相关资源
      最近更新 更多