PDA

View Full Version : Excel VBA problem with an "IF"



pedrovarela
09-12-2008, 08:43 AM
Hi,

I wrote a simple macro to compare the MarketShare of three products in each region and write down the name of the market leader in each region. The source data is in a sheet in percentages. SOMETHING VERY WEIRD HAPPENS. Sometimes, the ">" doesn't work properly. I have add watches and I can see that the majority of the comparisons work well but sometimes not. It shows that "5% > 20%" ! ARe you familiar with this bug. ANy help will be welcome cause I'm really desperate. The source data is just a list of % and this is my macro. I know I could simplify the "IF" structure but I wanted to make sure I didn't make any error.

Sub ImportFromClickLeader()
Application.ScreenUpdating = False
Dim ARR(3)
For j = 11 To 769
Worksheets("Ugadata").Select
For i = 5 To 3410
ARR(1) = Range("G" & i).Value
ARR(2) = Range("J" & i).Value
ARR(3) = Range("L" & i).Value
If ((ARR(1) > ARR(2)) And (ARR(1) > ARR(3))) Then
Worksheets("Model").Range("E" & j).Value = "Product1"
ElseIf ((ARR(2) > ARR(1)) And (ARR(2) > ARR(3))) Then
Worksheets("Model").Range("E" & j).Value = "Product2"
ElseIf ((ARR(3) > ARR(1)) And (ARR(3) > ARR(2))) Then
Worksheets("Model").Range("E" & j).Value = "Product3"
End If
Exit For
Next i
Next j
Application.ScreenUpdating = True
End Sub


Thnaks!!!!!!!

Oorang
09-12-2008, 06:57 PM
Try replacing this:
Dim ARR(3)
With this:
Dim ARR(3) As Double