sourabh_ajmera
New Member
- Joined
- Jul 17, 2014
- Messages
- 36
Hello Everybody,
I'll try my best to explain this complex situation. There is data in column A and Column D. Data in column A is account no and data in Column D is some % value.
Each account can have multiple % (please refer table below for example)
Now I am trying to write a code to see if the next cell in Column A is empty, if empty than we know that we have to average the % values in Column D to get the average for the account no. This average value is stored in Column E.
E.g: So for account 12345 average % value from column D shoud be 0.5% only displayed in Column E. But, for 56789 the average should be (1.31+2.67)/2 and so on..
<tbody>
</tbody>
Here's the code that I tried but eventually fails at the IsEmpty condition. Please help me for this as I am confused and out of ideas.
Please let me know if I can help further clearing your doubts. Thanks in advance.
I'll try my best to explain this complex situation. There is data in column A and Column D. Data in column A is account no and data in Column D is some % value.
Each account can have multiple % (please refer table below for example)
Now I am trying to write a code to see if the next cell in Column A is empty, if empty than we know that we have to average the % values in Column D to get the average for the account no. This average value is stored in Column E.
E.g: So for account 12345 average % value from column D shoud be 0.5% only displayed in Column E. But, for 56789 the average should be (1.31+2.67)/2 and so on..
Column A (Account no) | Column B...Column C (Random data) | Column D (% values) | Column E (Average %) |
12345 | xyz | 0.5% | 0.5% |
56789 | fgyhd | 1.31% | 1.99% |
2.67% | |||
84289 | dghdfg | 5.4% | 2.33% |
0.034% | |||
1.56% | |||
8936734 | fjdfsgsjkf | 4.9% | 4.9% |
<tbody>
</tbody>
Here's the code that I tried but eventually fails at the IsEmpty condition. Please help me for this as I am confused and out of ideas.
Please let me know if I can help further clearing your doubts. Thanks in advance.
Rich (BB code):
Sub Test()
Dim AvgNIM As Double, AvgNIM1 As Double, AvgNIM2 As Double, FindAvg As Double
Dim VerifyEmpty As Integer
For VerifyEmpty = 2 To 100
Sheets("Rate Calculator").Select
If (Range("A" & VerifyEmpty).Value = Range("B" & VerifyEmpty).Value) Then
If Not Range("A" & VerifyEmpty).Value = "" Then
AvgNIM = Range("D" & VerifyEmpty).Value
End If
If Range("A" & VerifyEmpty + 1).Value = "" Then
AvgNIM1 = Range("D" & VerifyEmpty + 1).Value
If Range("A" & VerifyEmpty + 2).Value = "" Then
AvgNIM2 = Range("D" & VerifyEmpty + 2).Value
End If
End If
If Not IsEmpty(AvgNIM) And Not IsEmpty(AvgNIM1) And Not IsEmpty(AvgNIM2) Then <---- Error (it executes this 'If' block even if the value is empty)
FindAvg = (AvgNIM + AvgNIM1 + AvgNIM2) / 3
Range("E" & VerifyEmpty).Value = FindAvg
ElseIf Not AvgNIM = "" And Not AvgNIM1 = "" Then
FindAvg = (AvgNIM + AvgNIM1) / 2
Range("E" & VerifyEmpty).Value = FindAvg
'End If
Else
Range("E" & VerifyEmpty).Value = AvgNIM
End If
End If
Next VerifyEmpty
End Sub