Help required with error message please...

Hi there,

Please can someone tell me why I might get the following error from the code below?

Can’t get item 5 of {}

It’s really strange because when I watch the values of (item c of list2 as number) prior to the line below all seems ok, however the error then occurs?

set item totalsPos of timeTotals to (item totalsPos of timeTotals) + (item c of list2 as number)

Without the test data this is a little tricky I guess, though any educated guesses would be appreciated.

Thanks.

Here’s the code:-

set timeTotals to {}

repeat with i from 1 to 31
	
	tell application "Microsoft Excel"
		activate
		activate object worksheet i of active workbook
		set list1 to value of every cell of range ("B12:B31")
		set list2 to value of every cell of range ("F12:F31")
		set list3 to value of every cell of range ("G12:G31")
	end tell
	
	set c to 1
	
	repeat with ThisItem in list1
		set customerCodes to {"AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH", "III"}

		repeat with thisCode in customerCodes
			set totalsPos to list_position(thisCode as text, customerCodes)
			if (ThisItem as text) = thisCode as text then
				set item totalsPos of timeTotals to (item totalsPos of timeTotals) + (item c of list2 as number)
			end if
		end repeat
		
		set c to c + 1
		
	end repeat
	
end repeat

on list_position(this_item, this_list)
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then return i
	end repeat
	return 0
end list_position

AAA & BBB & CCC & DDD & EEE & FFF & GGG & HHH & III

Thanks in advance,

Nick

set timeTotals to {}
.

.
	if (ThisItem as text) = thisCode as text then
		set item totalsPos of timeTotals to (item totalsPos of timeTotals) + (item c of list2 as number)
	end if
.

at the point of the first match of customerCodes and an item of list1
the variable timeTotals is still an empty list. This causes the error

Thanks for the help Stefan.
It worked a treat! :slight_smile:

Instead of:-

set timeTotals to {}

I replaced it with this and the script works:-

set timeTotals to {0, 0, 0, 0, 0, 0, 0, 0, 0}

I assume that’s what it needed?

Is there a better way to achieve what I’m trying to do?

Once again, thank you for your time on this, it much appreciated.

Regards,

Nick

this script looks good.
It’s hard to judge something without the source data

Yeah, it is a little tricky without the data source.

Thanks again Stefan.

Best Regards,

Nick