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:
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...
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
your solution is correct, you must specify cells within the same sheet as the range
to make more elegant try like
vb Code:
with Worksheets("batch details")
total_inven_cost = Application.Sum(.Range _
(.Cells(newRow_batchDetails, 8), .Cells _
(latestbatchrow_batchDetails, 8)))
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