-
Impossible to give you concrete ideas without seeing at least dummy data.
Very very difficult to troubleshoot complex logic statements when you don't use option explicit.
You could definitely benefit from some modularization of your code, a la:
You may be able to fix further just by simplifying some of your structure.
[vba]
Option Explicit
Sub test1()
Dim no_of_row As Long
Dim cnt_Col As Long
Dim cnt_row As Long
Dim cnt_n1 As Double
Dim cnt_n2 As Double
Dim cnt_n3 As Double
Dim cnt_n4 As Double
Dim cnt_w1 As Double
Dim cnt_w2 As Double
Dim cnt_w3 As Double
Dim cnt_w4 As Double
Dim cnt_w1_ok As Double
Dim cnt_w2_ok As Double
Dim cnt_w3_ok As Double
Dim cnt_w4_ok As Double
no_of_row = 10 'Specify total number of rows here
For cnt_Col = 0 To 2
cnt_n1 = 0
cnt_n2 = 0
cnt_n3 = 0
cnt_n4 = 0
cnt_w1 = 0
cnt_w2 = 0
cnt_w3 = 0
cnt_w4 = 0
cnt_w1_ok = 0
cnt_w2_ok = 0
cnt_w3_ok = 0
cnt_w4_ok = 0
For cnt_row = 1 To no_of_row
'***Start of Week 1***
If (Day(Cells(cnt_row, 1 + (3 * cnt_Col))) <= 7) And _
(Day(Cells(cnt_row, 1 + (3 * cnt_Col)) >= 1)) Then
HowToModularize cnt_row, cnt_Col, cnt_w1, cnt_n1, cnt_w1_ok
'***Start of Week 2***
ElseIf (Day(Cells(cnt_row, 1 + (3 * cnt_Col)) <= 14)) And _
(Day(Cells(cnt_row, 1 + (3 * cnt_Col)) >= 8)) Then
HowToModularize cnt_row, cnt_Col, cnt_w2, cnt_n2, cnt_w2_ok
'***Start of Week 3***
ElseIf (Day(Cells(cnt_row, 1 + (3 * cnt_Col)) <= 21)) And _
(Day(Cells(cnt_row, 1 + (3 * cnt_Col)) >= 15)) Then
HowToModularize cnt_row, cnt_Col, cnt_w3, cnt_n3, cnt_w3_ok
'***Start of Week 4***
ElseIf (Day(Cells(cnt_row, 1 + (3 * cnt_Col)) <= 31)) And _
(Day(Cells(cnt_row, 1 + (3 * cnt_Col)) >= 22)) Then
HowToModularize cnt_row, cnt_Col, cnt_w4, cnt_n4, cnt_w4_ok
End If
Next cnt_row
Cells(no_of_row + 5, 2 + (3 * cnt_Col)) = (100 * cnt_w1_ok) / (cnt_w1 - cnt_n1) & "%"
Cells(no_of_row + 6, 2 + (3 * cnt_Col)) = (100 * cnt_w2_ok) / (cnt_w2 - cnt_n2) & "%"
Cells(no_of_row + 7, 2 + (3 * cnt_Col)) = (100 * cnt_w3_ok) / (cnt_w3 - cnt_n3) & "%"
Cells(no_of_row + 8, 2 + (3 * cnt_Col)) = (100 * cnt_w4_ok) / (cnt_w4 - cnt_n4) & "%"
Next cnt_Col
End Sub
Sub HowToModularize(cnt_row As Long, cnt_Col As Long, cnt_w As Double, cnt_n As Double, cnt_w_ok As Double)
cnt_w = cnt_w + 1
If (Cells(cnt_row, 1 + (3 * cnt_Col)) = "NA") Or _
(Cells(cnt_row, 1 + (3 * cnt_Col)) = "Na") Or _
(Cells(cnt_row, 1 + (3 * cnt_Col)) = "na") Or _
(Cells(cnt_row, 2 + (3 * cnt_Col)) = "NA") Or _
(Cells(cnt_row, 2 + (3 * cnt_Col)) = "Na") Or _
(Cells(cnt_row, 2 + (3 * cnt_Col)) = "na") Then
cnt_n = cnt_n + 1
ElseIf (Cells(cnt_row, 1 + (3 * cnt_Col)) = "") Or _
(Cells(cnt_row, 2 + (3 * cnt_Col)) = "") Then
cnt_n = cnt_n + 1
ElseIf Cells(cnt_row, 1 + (3 * cnt_Col)) >= _
Cells(cnt_row, 2 + (3 * cnt_Col)) Then
cnt_w_ok = cnt_w_ok + 1
End If
End Sub
[/vba]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules