1

I'm writing a small class in Excel VBA with a sub that takes Range as an argument. But when I try to call it using Cells I get type mismatch error. (Calling using Range works fine)

Code excerpt:

Dim Com, Gua As Worksheet
Set Com = Worksheets(cCom)
Set Gua = Worksheets(cGua)

Dim myFirm As clsFirm
Set myFirm = New clsFirm
Call myFirm.Init(Gua.Cells(3, 4))  '<--ERROR HERE
Call myFirm.Init(Gua.Range("E3"))  ' this line works

And here is the sub:

Public Sub Init(nameCell As Range)
    pAddress = nameCell.Address
    pName = nameCell.Value2
    pContract = nameCell.Offset(0, -3).Value2
    pAmount = nameCell.Offset(0, 2).Value2
    pRate = nameCell.Offset(0, 1).Value2
    pStartDate = CDate(nameCell.Offset(0, -2).Value2)
    pEndDate = CDate(nameCell.Offset(0, -1).Value2)
End Sub
5
  • Does Gua.Range(Cells(3, 4)) works? I'm not sure how a ByVal argument may affect your procedure, but give a try to see, sometimes it's pretty helpful!
    – R3uK
    Jun 9, 2015 at 9:52
  • Maybe this would work Call myFirm.Init(Gua.Cells(3, 4).Range) Jun 9, 2015 at 10:02
  • hmm, i'd like to know the answer to this. from my understanding, they should both return a range.
    – tea_pea
    Jun 9, 2015 at 10:40
  • 2
    What content is in the cell "D3" which causes the error? I suppose that Call myFirm.Init(Gua.Cells(3, 5)) works, right?
    – gembird
    Jun 9, 2015 at 10:53
  • 3
    I agree with @dee. The tests in your sample code are using two different cells (Gua.Cells(3, 4) is not E3) and both work fine if tested on an empty sheet. I suspect you have an issue with the values in your cells. Have you stepped through the code using breakpoints and F8 to double-check which line causes the error. Jun 9, 2015 at 12:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.