Consulting

Results 1 to 10 of 10

Thread: Type Mismatch in Excel 2003

  1. #1
    VBAX Regular mgh_mgharish's Avatar
    Joined
    Feb 2005
    Location
    Harihar
    Posts
    7
    Location

    Unhappy Type Mismatch in Excel 2003

    A code like cells(1,4).End(xlUp)
    works fine in Excel XP but not in 2003

    Sub a()
        For i = 2 To Cells(1, 4).End(xlUp)
            If (Cells(i, 4).Value = 3.9) Then
                Range(Cells(i, 4), Cells(i, 4).End(xlToRight)).Copy Cells(i - 1, 6)
                Rows(i).Delete
            End If
        Next
    End Sub

    What may be the problem? What should be done?

  2. #2
    VBAX Regular zilpher's Avatar
    Joined
    Nov 2004
    Location
    Swindon, UK
    Posts
    30
    Location
    Cells(1, 4).End(xlUp) will return a range object, yet you are using this in a For Next loop. You probably want to add .Row on the end, or maybe .Value, depends what property you want to use of the cell that Cells(1, 4).End(xlUp) returns.

    You also probably want to do some checks to make sure you do get a number, and gracefully exit if you don't.

    HTH

  3. #3
    VBAX Regular mgh_mgharish's Avatar
    Joined
    Feb 2005
    Location
    Harihar
    Posts
    7
    Location
    Yes.. but that works great in XP

  4. #4
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hi, mgh. I have run your code in both 2002 and 2003, and I do not get an error in either. ?
    ~Anne Troy

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    What is the error message that you get?

  6. #6
    VBAX Regular mgh_mgharish's Avatar
    Joined
    Feb 2005
    Location
    Harihar
    Posts
    7
    Location
    Type Mismatch
    Run time error

  7. #7
    VBAX Regular mgh_mgharish's Avatar
    Joined
    Feb 2005
    Location
    Harihar
    Posts
    7
    Location
    OK.. I have solved that.. That depends on the data in D1 cell..
    If it is a string, it gives error

  8. #8
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Ahh. Try this.

    If Val(Cells(i, 4).Value) = 3.9 Then

  9. #9
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Cool.

    Yo, mgh. I saw a really cool macro you put over there at that other site that would make a nice KB entry here. You know...the one where we "force" the user to enter dates into a cell?

    Also, you can mark your own thread solved here by using Thread Tools at the top of this Q.
    ~Anne Troy

  10. #10
    VBAX Regular mgh_mgharish's Avatar
    Joined
    Feb 2005
    Location
    Harihar
    Posts
    7
    Location
    Thanks one and all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •