Norman
Essentially I want to run across a sheet putting values into an array. Then
move down a line and run across again, this time adding the value to the
value in the array.
e.g.
0 0 0 0 0
1 0 0 1 1
2 0 0 0 2
there are 2 columns to check. I need to do this for each customer in another
list.
I have the following code from Sam which I have put into a test button.
However, this throws up a Run time error 13 - Type Mismatch error.
Private Sub CommandButton1_Click()
Dim lList As Variant
Dim iCol, i As Integer
Worksheets("Export").Select
For iCol = 12 To 31
For i = 0 To 20
ReDim Preserve lList(i)
lList(i) = Cells(2, iCol).Value
Next i
Next iCol
End Sub
> Hi Steve,
>
[quoted text clipped - 43 lines]
> >
> > Steve
Steve - 30 May 2008 15:45 GMT
As you said, easy when you know how
Thank you
> Norman
>
[quoted text clipped - 79 lines]
> > >
> > > Steve
Sam Wilson - 30 May 2008 15:45 GMT
It should be "dim |List() as variant" , you need the brackets in the
declaration for an array
> Norman
>
[quoted text clipped - 79 lines]
> > >
> > > Steve
Steve - 30 May 2008 17:17 GMT
And I thought it was all going so well.
Trying to get a routine to add up a number of columns and a number of rows
with the following routine:
Dim lList() As Integer
Dim iCol, i, rw As Integer
For iRow = 2 To 3
i = 0
For iCol = 12 To 13
ReDim Preserve lList(i)
lList(i) = lList(i) + Worksheets("Export").Cells(iRow, iCol).Value
i = i + 1
Next iCol
Next iRow
for some reason it seems to put the first row into teh array and even adds
up the first one of the second row.
This is what I have:
6 3 2 4
1 2 5 7
So my array after the routine should have the following in it:
7 5 7 11
No so luck
Where am i going wrong?
Thanks
Steve
> It should be "dim |List() as variant" , you need the brackets in the
> declaration for an array
[quoted text clipped - 82 lines]
> > > >
> > > > Steve
Gary Keramidas - 30 May 2008 17:42 GMT
this works for me
Sub test()
Dim irow As Long
Dim lList() As Integer
Dim icol, i, rw As Long
For irow = 2 To 3
i = 0
For icol = 12 To 15
ReDim Preserve lList(0 To 3)
lList(i) = lList(i) + Worksheets("sheet1").Cells(irow, icol).Value
i = i + 1
Next
Next
End Sub

Signature
Gary
> And I thought it was all going so well.
>
[quoted text clipped - 127 lines]
>> > > >
>> > > > Steve
Steve - 30 May 2008 17:53 GMT
This also works for me.
The only difference I can see is the Dim statements.
You learn something every day.
Thanks for your help.
Best Regards
Steve
> this works for me
>
[quoted text clipped - 145 lines]
> >> > > >
> >> > > > Steve
Steve - 30 May 2008 17:57 GMT
Ah, just seen the ReDim statement. This I think makes the real difference.
My problem here is that i'm not sure how big the array will be.
Some more experimenting me thinks.
This is a big help though.
Cheers
Steve
> this works for me
>
[quoted text clipped - 145 lines]
> >> > > >
> >> > > > Steve
Gary Keramidas - 30 May 2008 19:26 GMT
there's not enough information to really help. here i showed an example of how
to do it with variables, but i don't know how your data is laid out and how
you're determining which columns and rows to use.
there may be better ways to do this:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim irow As Long
Dim lList() As Integer
Dim startcol As Long
Dim endcol As Long
Dim icol, i, rw As Long, x As Long
ReDim lList(0)
Set ws = Worksheets("Sheet1")
startcol = 12
endcol = ws.Cells(2, Columns.Count).End(xlToLeft).Column
For irow = 2 To 3
i = 0
For icol = startcol To endcol
x = 1
ReDim Preserve lList(0 To endcol - startcol)
lList(i) = lList(i) + Worksheets("sheet1").Cells(irow,
icol).Value
i = i + 1
Next
Next
End Sub

Signature
Gary
> Ah, just seen the ReDim statement. This I think makes the real difference.
>
[quoted text clipped - 159 lines]
>> >> > > >
>> >> > > > Steve