arrays and labeling

M

Matt S

Hi all,

I have 50 hours of data, second by second in excel. I am trying to label
each point as 1 of 4 modes based on certain criteria. My 'Oxygen' is a
column that says if my oxygen is on or not. My UEGO is another column that
is either at 1 or 0.9. These are the criteria for the modes.

Mode 1 (~10sec):
Oxygen = 0
UEGO ~ 1

Mode 2 (~10 sec):
Oxygen = 0
UEGO ~ 0.9

Mode 3 (~5 sec):
Oxygen = 1
UEGO ~ 0.9

Mode 4 (~10 sec):
Oxygen = 1
UEGO ~1

I'd like to use arrays to label the data to make this process fast. My
array knowlege is a little weak.

Any help would be greatly appreciated!
Thanks!
Matt
 
M

Matt S

Ok,

I tried my luck and I've got it up to the point where I need to paste the
data into excel. What I'm tracking with the Watch on arrLabel is not
matching what I'm getting when pasted into excel.

ReDim arrLabel(1 To LastRow) As Variant
ReDim arrFUEGO(1 To LastRow) As Variant
ReDim arrOxygen(1 To LastRow) As Variant

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For j = 1 To LastRow
If Round(arrFUEGO(j, 1), 1) = 1 Then
If arrOxygen(j, 1) > 0 Then
arrLabel(j) = "Mode 4"
Else
arrLabel(j) = "Mode 1"
End If
Else
If arrOxygen(j, 1) > 0 Then
arrLabel(j) = "Mode 3"
Else
arrLabel(j) = "Mode 2"
End If
End If
Next

ActiveSheet.Range("X8:X" & LastRow).Value = arrLabel

What's wrong with that last line?
Thanks!
Matt
 
M

Matt S

I cleaned up the code a bit, but it still will not paste the array correctly
in excel! I can see using the watch in VB that the array is being created
correctly. It is cycling through all the modes, but when pasting all excel
says down the entire column is "Mode 1." This is infuriating. My data
starts at row 8, so that is why I have the array start there. It does the
same thing if I start the array at 1.

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

'Declare Arrays
ReDim arrLabel(8 To LastRow) As String
ReDim arrFUEGO(8 To LastRow) As Double
ReDim arrOxygen(8 To LastRow) As Double

For j = 8 To LastRow
arrFUEGO(j) = Range("P" & j).Value
arrOxygen(j) = Range("E" & j).Value
Next j

For j = 8 To LastRow
If Round(arrFUEGO(j), 1) <= 0.95 Then
If arrOxygen(j) > 0 Then
arrLabel(j) = "Mode 3"
Else
arrLabel(j) = "Mode 2"
End If
Else
If arrOxygen(j) > 0 Then
arrLabel(j) = "Mode 4"
Else
arrLabel(j) = "Mode 1"
End If
End If
Next


ActiveSheet.Range("Y8:Y" & LastRow).Value = arrLabel

If I look at the array in the watch it looks like this:
arrLabel(8) = "Mode 1"
...
...
arrLabel(55) = "Mode 2"
...
arrLabel(60) = "Mode 3"
...
arrLabel(70) = "Mode 4"
 
D

Dave Peterson

ActiveSheet.Range("Y8:Y" & LastRow).Value = arrLabel
should be:
ActiveSheet.Range("Y8:Y" & LastRow).Value = application.transpose(arrLabel)

I like to qualify my ranges, too. I'd use:

Option Explicit
Sub testme()

Dim LastRow As Long
Dim j As Long
Dim arrLabel() As String
Dim arrFUEGO() As Double
Dim arrOxygen() As Double

With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

'redim Arrays
ReDim arrLabel(8 To LastRow)
ReDim arrFUEGO(8 To LastRow)
ReDim arrOxygen(8 To LastRow)

For j = 8 To LastRow
arrFUEGO(j) = .Range("P" & j).Value
arrOxygen(j) = .Range("E" & j).Value
Next j

For j = LBound(arrFUEGO) To UBound(arrFUEGO)
If Round(arrFUEGO(j), 1) <= 0.95 Then
If arrOxygen(j) > 0 Then
arrLabel(j) = "Mode 3"
Else
arrLabel(j) = "Mode 2"
End If
Else
If arrOxygen(j) > 0 Then
arrLabel(j) = "Mode 4"
Else
arrLabel(j) = "Mode 1"
End If
End If
Next j

'.Range("Y8:Y" & LastRow).Value = Application.Transpose(arrLabel)

'or
'I like this syntax so I don't have to care or know about the
'upper bound
.Range("Y8").Resize(UBound(arrFUEGO) - LBound(arrFUEGO) + 1).Value _
= Application.Transpose(arrLabel)

End With

End Sub
 
D

Dave Peterson

I like all my declarations at the top.

I'm not sure if it makes a difference technically, but it makes more sense to me
that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top