+ Reply to Thread
Results 1 to 7 of 7

Thread: reference to multiple columns not working in a file

  1. #1
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Exclamation reference to multiple columns not working in a file

    hi there,

    this is the weirdest thing ever

    i often refer to a number of rows or columns so I got tired of writting
    ws.rows(firstrow & ":" & lastrow & ")"
    and specially for columns

    so I made a couple user functions to get me through (see below)

    however this functions have stoped working for a single file
    in which it returns "Type mismatch" when evaulating a row such as

    sub public test()
    dim ws as worksheet
    dim firstcol,lastcol as integer
    set ws = activesheet
    firstcol = 1
    lastcol = 2
    
    ws.columns(rgc(firstcol,lastcol))
    
    end sub
    actually it does not accept anymore references like ws.columns("A:B") or even ws.range("A:B") or ws.rows("1:2") for that matter

    what could it be???

    please help!

    it just stopped working, I tried restarting and even from a different pc


    number to letter
    Public Function N2L(num As Integer) As String
        'returns letter corresponding to column number
        N2L = Split(Cells(1, num).Address, "$")(1)
    End Function
    to declare a range of rows
    Function rgr(ByVal firstrow As Integer, ByVal lastRow As Integer) As String
        'returns the string to refer to a range of rows as in ws.rows(string)
        rgr = firstrow & ":" & lastRow
    End Function
    to declare a range of columns
    Function rgc(ByVal firstcolumn As Integer, ByVal lastColumn As Integer) As String
        'returns the string to refer to a range of columns as in ws.columns(string)
        rgc = N2L(firstcolumn) & ":" & N2L(lastColumn)
    End Function
    Last edited by bagullo; 02-27-2011 at 07:29 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    Posts
    597

    Re: reference to multiple columns not working in a file

    Hi there,

    Stopped working? I don't think this code could EVER have worked

    I've corrected a few errors and the following code works for me:

    
    
    '   sub public test()                   <<  Syntax Error
    Public Sub test()
    
        Dim ws As Worksheet
    '    Dim firstcol, lastcol As Integer   '   Note: This statement declares firstcol as a VARIANT
        Dim firstcol As Integer, lastcol As Integer
    
        Set ws = ActiveSheet
        firstcol = 1
        lastcol = 2
    
    '    ws.Columns (rgc(firstcol, lastcol))    '   This defines only a range - you must also specify a METHOD
        ws.Columns(rgc(firstcol, lastcol)).Select
    
    End Sub


    I hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: reference to multiple columns not working in a file

    Quote Originally Posted by Greg M View Post
    Hi there,

    Stopped working? I don't think this code could EVER have worked

    I've corrected a few errors and the following code works for me:

    
    
    '   sub public test()                   <<  Syntax Error
    Public Sub test()
    
        Dim ws As Worksheet
    '    Dim firstcol, lastcol As Integer   '   Note: This statement declares firstcol as a VARIANT
        Dim firstcol As Integer, lastcol As Integer
    
        Set ws = ActiveSheet
        firstcol = 1
        lastcol = 2
    
    '    ws.Columns (rgc(firstcol, lastcol))    '   This defines only a range - you must also specify a METHOD
        ws.Columns(rgc(firstcol, lastcol)).Select
    
    End Sub


    I hope this helps - please let me know how you get on.

    Regards,

    Greg M
    thank you greg,
    actually the sub where the function is called i wrote it on the spot, did not copy it from my code... the point is that the row "ws.Columns(rgc(firstcol, lastcol)).Select" should work always right??
    it does not make any sense that it says "type mismatch"
    why would that happen only in one file

    I hope that tomorrow, once i get back to work the problem has solved by itself :D I'll let you know anyway.

    thnks!

  4. #4
    Valued Forum Contributor
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    Posts
    597

    Re: reference to multiple columns not working in a file

    Hi again,

    Thanks for your feedback. Yes, a Type Mismatch error certainly seems strange, but as you know, it's very hard to debug code in isolation - it's usually necessary to see the entire situation e.g. workbook & worksheet names, usedranges etc. etc. in order to fully understand (or at least try to understand!) what's actually happening rather than what should be happening.

    It will be a very happy day when a computer does what we WANT it to do instead of what we TELL it to do!

    Anyway, let's hope the problem has disappeared at your end - please keep me informed.

    Regards,

    Greg M

  5. #5
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: reference to multiple columns not working in a file

    problem still there...

    "runtime error '13'
    type mismatch"

    even on the immediate window

    print activesheet.Columns("A:B").address

    produces the same error

    however if open another file with the exact same line in the immediate window
    produces a result with no fuss...

    $A:$B




    is this file damned or what...?!

    the problem seems to be that .columns() does not accept an string as argument --- if I use a single integer, there is no problem.

    and the problem is there for this single file for sure

    anything to try or better I start over copying code in another sheet and see when things turn bad'?

  6. #6
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: reference to multiple columns not working in a file

    one more thing...

    apparently calling Cells by itself (referring then to activesheet) and not calling it as a method of some sheet in particular fails as well

    Public Function N2L(num As Integer) As String
        'returns letter corresponding to column number
        N2L = Split(Cells(1, num).Address, "$")(1)
    End Function
    maybe that's the key to know what is going on... only I have no idea

  7. #7
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2003
    Posts
    128

    Re: reference to multiple columns not working in a file

    FOUND IT!!!
    it looks more like a bug than anything,
    but found out that error happened because I had selected a graphobject by code (!!)
    for some reason when that's the selection, ws.column("A:B") style reference does not work!

    gotcha!

+ 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.2.0