+ Reply to Thread
Results 1 to 12 of 12

Subscript out of range error

  1. #1
    Registered User
    Join Date
    07-26-2013
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Subscript out of range error

    Hi all, I have just started experimenting with vba in excel. I have had some success and a bit of fun! However I'm going nuts trying to write one macro to check a users ID against a worksheet.

    I have written a macro which first checks all drives for a specific user file, then the macro below pulls the contents of three cells and assigns them to variables "strUser", "sSTAMP" and "sACCESS" (all public strings).

    Now the macro checks "sSTAMP" against cells from another worksheet found in another workbook within a row (row 17).

    Please Login or Register  to view this content.
    Whenever I run this I get "subscript out of range" at the IF command. Where am I making the mistake? Is there a better way of looking along a row for an entry?

    BTW I'm using office 2010.

    Many thanks
    Last edited by UTAS_Matt; 08-06-2013 at 02:31 AM.

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,448

    Re: Newb help

    Hi
    please read forum rules and adapt your title accordingly - Thx

  3. #3
    Registered User
    Join Date
    07-26-2013
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Subscript out of range error

    Sorry, I am new. To be fair when quickly scanning through a very busy GUI it is difficult to spot.

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Subscript out of range error

    maybe so
    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Subscript out of range error

    sFileLocation is never declared or given a value in the posted code.
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    07-26-2013
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Subscript out of range error

    Quote Originally Posted by nilem View Post
    maybe so
    Please Login or Register  to view this content.
    Thanks nilem, I tried referencing using Cells (17,i) but got the same error.

    The string sFileLocation is currently set to "P:\Personal\My Documents\MG Matrix.xlsm" from another module, when debugging the value is showing correctly. My "P:\" drive is a network drive which is sync locally for when I am off line. I am using Windows7.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Subscript out of range error

    You don't use the full path to refer to an open workbook via the Worbooks collection.

  8. #8
    Registered User
    Join Date
    07-26-2013
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Subscript out of range error

    Could you explain further? I dropped the Workbooks and worksheets reference and the code ran without error. However it did not find the entry from the worksheet "Matrix" that matched the variable held by sSTAMP.

  9. #9
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Subscript out of range error

    Utas, try it
    Please Login or Register  to view this content.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Subscript out of range error

    You definitely shouldn't drop those.

    To refer to an open workbook called MG Matrix.xlsm you would use Workbooks("MG Matrix.xlsm").

    If sFileLocation contians the path and filename you can get the filename like this.
    Please Login or Register  to view this content.
    So your code would look something like this.
    Please Login or Register  to view this content.
    PS Is the code in the matrix workbook? If it was then you could refer to it without using the name with ThisWorkbook.
    Last edited by Norie; 08-06-2013 at 04:14 AM.

  11. #11
    Registered User
    Join Date
    07-26-2013
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Subscript out of range error

    Thank you Norie!
    That done the job. The code was infact within the active workbook but i wanted to keep everything explicit so that I could move the code at a later date.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Subscript out of range error

    I think nilem beat me to it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. hi newb here
    By DPPHAM in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 04-06-2013, 12:16 AM
  2. newB
    By excelforumpupil in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 06-24-2012, 04:58 PM
  3. Newb
    By hnasir in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 04-17-2012, 05:14 PM
  4. newb needs help!
    By matt inertia in forum Excel General
    Replies: 5
    Last Post: 07-22-2008, 10:39 AM
  5. newb needs help
    By ChickenPecker in forum Excel General
    Replies: 1
    Last Post: 02-17-2005, 03:06 PM

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