+ Reply to Thread
Results 1 to 9 of 9

For Next Loop in column - type mismatch error

Hybrid View

TC1980 For Next Loop in column -... 07-03-2013, 05:32 AM
JosephP Re: For Next Loop in column -... 07-03-2013, 05:51 AM
TC1980 Re: For Next Loop in column -... 07-03-2013, 06:21 AM
TMS Re: For Next Loop in column -... 07-03-2013, 06:25 AM
TC1980 Re: For Next Loop in column -... 07-03-2013, 07:24 AM
JosephP Re: For Next Loop in column -... 07-03-2013, 06:25 AM
TC1980 Re: For Next Loop in column -... 07-03-2013, 07:27 AM
TMS Re: For Next Loop in column -... 07-03-2013, 08:59 AM
Norie Just curious, why don't you... 07-03-2013, 09:14 AM
  1. #1
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    For Next Loop in column - type mismatch error

    Hi,

    I'm in the process of reworking some code to make it more easily accept future changes and avoid using public variables where possible.
    I have code in the workbook open event that finds specific text in the "Tracker" worksheet and asigns that column number to a cell in another worksheet - this will be hidden. I have then named each of these cells as a named range, just to make the code a bit easier to read. The idea behind this is that should the layout of the "Tracker" sheet change (quite likely) it will not effect the existing code.

    Anyways, I have a section of code (a for next loop) that used to work fine refering to public variable for the column index number but now I have stored those variables within cells in a worksheet I'm getting a type mismatch. Perhaps i'm not referencing it properly...

        
    Dim c As Range
    
    Sub CaseSummary()
    
    With Worksheets("Tracker")
            
            For Each c In .Columns(Range("PersonNoCol")) 'this range contains a cell with the value 6 in it.
            
                If c.Value = Me.TextBox5 Then 'this is where the type mismatch error is coming up
                
                    'code for message box here
    
                End If
    
    End With
    
    End Sub
                
            Next c
    The Textbox5 definately has a value in it, I can see this when I mouse over, but I'm not sure the 'c' is...

    Any thoughts??? I'm being daft aren't I?

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: For Next Loop in column - type mismatch error

    do you have any error values in any cells? for clarity I would suggest you use
    For Each c In .Columns(Range("PersonNoCol")).cells
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: For Next Loop in column - type mismatch error

    Hi JP,

    No error values in any of the cells - just a list of numbers - however I added .cells as you suggest and I'm no longer getting a type mismatch. Yeay.

    But there appears to be another probelm in that when it reaches a cell where the contents match the contents of the textbox - it's not registering the match and just skips to end if. Therefore not triggering the warning message to the user. No so yeay.

    The c.value is showing as 665250055 and the textbox value is showing as "665250055" - would the quotations cause a problem?

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,544

    Re: For Next Loop in column - type mismatch error

    Maybe try CStr(c.Value)


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: For Next Loop in column - type mismatch error

    Thanks Trevor that works - guess it was not registering a match as it was comparing a numerical value to a string. Again weird that it worked okay before but it's sorted now and i'll use this code again in the future. Thanks again.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: For Next Loop in column - type mismatch error

    if testing numbers it is safer to use an explicit conversion
    If c.Value = CLng(Me.TextBox5)

  7. #7
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: For Next Loop in column - type mismatch error

    Thanks JP,

    Re: [QUOTE=JosephP;3306998]if testing numbers it is safer to use an explicit conversion/[QUOTE]

    It's not always numbers in the case, but is for others so will bear this in mind. Thanks again.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,544

    Re: For Next Loop in column - type mismatch error

    You're welcome. Thanks for the rep.


    I guess you're always going to get text out of a text box, so converting to a string is perhaps the best option in this case.

    Regards, TMS

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    Just curious, why don't you name the Tracker column instead of storing it on a worksheet?

    Then you could use that name in the code rather than using Columns and referring to a cell on a worksheet.

    If you need the column number for anything you can get it using the Column property of the named range.
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1