Results 1 to 10 of 10

Thread: [RESOLVED]newbie - help with syntax error

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    [RESOLVED]newbie - help with syntax error

    Trying to sum a range of cells on different sheet, where the upper and lower rows changes. I'm getting a the syntax error "run-time error '1004': application-defined or object-defined error" for the following line of code:


    total_inven_lbs = Application.WorksheetFunction.Sum(Sheets("batchDetails").Range(Cells(newRow_batchDetails, 8), Cells(latestBatchrow_batchDetails, 8)))

    Been a few hours already trying to research and solve.

    Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: newbie - help with syntax error

    What do you have

    "newRow_batchDetails"
    and
    "latestBatchrow_batchDetails"

    set to?

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Re: newbie - help with syntax error

    Many thanks for the reply.

    I have them set to a cell containing a value:

    newRow_batchDetails = Worksheets("batch details").Range("A2").Value + 1

    latestbatchrow_batchDetails = Worksheets("batch details").Range("A10").Value

    I have also attached a stripped down version of the workbook - i'm learning VB on the fly, so perhaps not the most elegant.
    Attached Files Attached Files

  4. #4
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: newbie - help with syntax error

    Your problem is more than likely within;
    Range(Cells(newRow_batchDetails, 8)

    Either use the Range or Cells, don't combine them. From what it looks like, you need to just use Range.

  5. #5
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: newbie - help with syntax error

    Try it like:
    vb Code:
    1. Dim rangestart as string
    2.      Dim rangestop as string
    3.  
    4.      rangestart = "H" + newRow_batchDetails
    5.      rangestop = "H" + latestBatchrow_batchDetails
    6.  
    7.      total_inven_lbs = Application.WorksheetFunction.Sum(Sheets("batchDetails").Range(rangestart, rangestop))

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Re: newbie - help with syntax error

    I tried the code (copied and pasted exactly as in spreadsheet) - but got a "run-time error '13': type mismatch"

    Dim rangestart As String
    Dim rangestop As String

    rangestart = "H" + newRow_batchDetails
    rangestop = "H" + latestbatchrow_batchDetails

  7. #7
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: newbie - help with syntax error

    On which line did you get the error?
    Quote Originally Posted by newkarakara View Post
    I tried the code (copied and pasted exactly as in spreadsheet) - but got a "run-time error '13': type mismatch"
    Did you paste my code in a sub by itself?? It was designed to work you your variables:
    newRow_batchDetails
    latestBatchrow_batchDetails

    So in order for it to work it must be able to utilize those variables.
    So you can add the variables in the beginning of my code and set them to what they are supposed to be, or you can change the lifecycle of the variable so that my code can access it, but you still need to set the variables before you can run my code...

    At the bottom of this tutorial it has some more info about dealing with variables:
    http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Re: newbie - help with syntax error

    Many thanks again. I got the original code to work, not as elegant as your solution but "many to miles to go": seems the issue was including "value" at the end of the .cells reference, i.e. Cells(newRow_batchDetails, 8).value

    total_inven_cost = Application.Sum(Worksheets("batch details").Range _
    (Worksheets("batch details").Cells(newRow_batchDetails, 8), Worksheets("batch details").Cells _
    (latestbatchrow_batchDetails, 8)))

    Regarding the code you provided --yes, it was posted along with the definitions of the newRow_batchDetails, and
    latestBatchrow_batchDetails variables.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: newbie - help with syntax error

    your solution is correct, you must specify cells within the same sheet as the range

    to make more elegant try like
    vb Code:
    1. with Worksheets("batch details")
    2.   total_inven_cost = Application.Sum(.Range _
    3.   (.Cells(newRow_batchDetails, 8), .Cells _
    4.   (latestbatchrow_batchDetails, 8)))
    5. end with
    or set a worksheet object variable
    it is always better to work with fully qualified ranges, even where not required
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    5

    Re: newbie - help with syntax error

    thanks again. i'll try this approach.

Posting Permissions

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



Click Here to Expand Forum to Full Width