PDA

View Full Version : Looping Through an Array



Djblois
08-01-2007, 07:20 AM
I am trying to use Arrays to shorten my code. Here is what it looks like before the Array Loop:

PT.AddFields RowFields:=Array("Customer", "Cust#", "SlsPrsn", "Date", "Invoice#", "Product", "Item#", "Whse", "Price$", "Del")

ptNoTotal "Cust#"
ptNoTotal "SlsPrsn"
ptNoTotal "Date"
ptNoTotal "Invoice#"
ptNoTotal "Product"
ptNoTotal "Item#"
ptNoTotal "Cases"
ptNoTotal "Whse"
ptNoTotal "Price$"
ptNoTotal "Del"

This is what I tried with the Array loop:

aRowFields = Array("Cust#", "SlsPrsn", "Date", "Invoice#", "Product", "Item#", "Whse", "Price$", "Del")

PT.AddFields RowFields:=Array("Customer", "Cust#", "SlsPrsn", "Date", "Invoice#", "Product", "Item#", "Whse", "Price$", "Del")

For NoTotalRow = LBound(aRowFields, 1) To UBound(aRowFields, 1)
ptNoTotal aRowFields(NoTotalRow, 0)
Next

The first code works but the second code doesn't. It crashes on the second code. ptNoTotal is a sub that I have created with the ability to pass the rowfield name. If there is anything else you need to help just ask?

Bob Phillips
08-01-2007, 07:27 AM
It is a single dimension



For NoTotalRow = LBound(aRowFields, 1) To UBound(aRowFields, 1)
ptNoTotal aRowFields(NoTotalRow)
Next

Djblois
08-01-2007, 07:49 AM
xld,

THat worked perfectly.
Is it possible to start with the second value in the array? What if I wanted to start with "SlsPrsn" instead of "Cust#"?

Bob Phillips
08-01-2007, 08:02 AM
For NoTotalRow = LBound(aRowFields, 1) + 1 To UBound(aRowFields, 1)
ptNoTotal aRowFields(NoTotalRow)
Next

Djblois
08-01-2007, 08:38 AM
xld,

You are the man. last thing: I have never used multi Dimensional arrays or at least have little experience with them. Is it possible to turn this into a multi Dimensional array:

ptAddDataField "Cases", xlSum, "* #,##0;[Red]-#,##0;* ""-"";_(@_)", "Qty"
ptAddDataField "Units", xlSum, "#,##0;[Red]-#,##0;* ""-"";_(@_)", "Unit"
ptAddDataField "Amt", xlSum, "#,##0;[Red]-#,##0;* ""-"";_(@_)", "Sls$$"
ptAddDataField "Weight", xlSum, "* #,##0;[Red]-#,##0;* ""-"";_(@_)", "lbs."
ptAddDataField "Total Cost", xlSum, "#,##0;-#,##0;* ""-"";_(@_)", "Ttl Cost"
ptAddDataField "Profit", xlSum, "#,##0;[Red]-#,##0;* ""-"";_(@_)", "GP$$"

Bob Phillips
08-01-2007, 08:56 AM
This might work (It might not witha string that length), so give it a try



ptAddDataField [{"Cases", xlSum, "* #,##0;[Red]-#,##0;* ""-"";_(@_)", "Qty";"Units", xlSum, "#,##0;[Red]-#,##0;* ""-"";_(@_)", "Unit" ;"Amt", xlSum, "#,##0;[Red]-#,##0;* ""-"";_(@_)", "Sls$$" ;"Weight", xlSum, "* #,##0;[Red]-#,##0;* ""-"";_(@_)", "lbs.";"Total Cost", xlSum, "#,##0;-#,##0;* ""-"";_(@_)", "Ttl Cost" ;"Profit", xlSum, "#,##0;[Red]-#,##0;* ""-"";_(@_)", "GP$$"}]