+ Reply to Thread
Results 1 to 3 of 3

Run time error ‘1004’

Hybrid View

  1. #1
    Registered User
    Join Date
    07-13-2011
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    21

    Run time error ‘1004’

    Hi there was wondering if anybody could help I have this code which I am trying to set up so as depending on the month the rest of the code will use a different sheet. My sheets are called TPM1, TPM2 and so on up to TPM 12. the code below for instance works fine but It is fixed on TPM10.

    dKArray(y, x) = Range("TPM10!" + sCol + CStr(y + 9)).Value

    therefore I changed this to read.

    [code]iMonth = Worksheets("Clear").Range("E4").Value[code]

    dKArray(y, x) = Range("'TPM" + CStr(iMonth) + sCol + CStr(y + 9)).Value
    where iMonth is the number of month located in cell E4 and set as integer but for this I get a

    Run time error ‘1004’: Method “Range of object_Global failed error

    Can anybody help me/ tell me how to do what I want to. I am new to this and Visual basic for dummies isn’t much use.

    Cheers David
    Last edited by mdavid800; 07-27-2011 at 08:28 AM.

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Run time error ‘1004’

    Hi David,

    Your code is missing the closing ' around the sheet name and an exclamation mark:
    dKArray(y, x) = Range("'TPM" & CStr(iMonth) + "'!" + sCol + CStr(y + 9)).Value
    In VBA, it is better to use & to concatenate strings rather than +.

    Also, rather than using syntax like this:
    Range("SomeWorksheet!RangeAddress").Value
    It is better to use this:
    Worksheets("SomeWorksheet").Range("RangeAddress").Value

    So, taking into account the above suggestions, your code would then become:
    dKArray(y, x) = Worksheets("TPM" & CStr(iMonth)).Range(sCol & CStr(y + 9)).Value
    Last edited by Colin Legg; 07-27-2011 at 08:38 AM. Reason: OP added code tags
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    07-13-2011
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    21

    Re: Run time error ‘1004’

    Thank you Colin

    worked like a charm

    also thanks for the advise on correct syntax etc as i said im new to this and am trying to learn of some other examples and was picking up other peoples bad habits

    once again thanks

    David

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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