multi if VBA statment returns wrong values

Fishkins

New Member
Joined
May 28, 2014
Messages
1
Hi everybody, <o:p></o:p>
Hope that one can help with my silly problem. <o:p></o:p>
As a part of a big process I'm working on, I need a macro that evaluatesconditions and populate cells accordingly. The task seems to be straightforward, but something goes wrong.
I use the code below.
Code:
[CODE]Dim rng As Range
For Each rng In Sheets("B").Range("C2:AK366")
For y = 2 To 6
If Cells(rng.Row, 1) >= Sheets("DB").Cells(y, 5) And Cells(rng.Row, 1) <= Sheets("DB").Cells(y, 6) _
And Cells(1, rng.Column).Value >= Sheets("DB").Cells(y, 7) And Cells(1, rng.Column).Value <= Sheets("DB").Cells(y, 8) _
And Cells(rng.Row, 2).Value < Sheets("DB").Cells(y, 9) Then
rng.Value = Sheets("DB").Range("K" & y).Value
 
End If
Next y
 

If IsEmpty(rng) Then
rng.Value = Sheets("DB").Range("K7")
End If
Next rng
Code:
[/COLOR][/SIZE]Below is output and layout samples [HTML] 
      Start, day  End Day  Start, time incl  End, time incl  Day END    01/11/2014  28/02/2015  33  40  6    01/06/2014  31/08/2014  15  40  6    01/10/2014  28/02/2015  15  32  6    01/04/2014  31/03/2015  15  40  6    01/04/2014  31/03/2015  1  14  8        1  48  8  [/HTML]   [HTML]
     Day    1    01/04/2014  2      02/04/2014  3      03/04/2014  4      04/04/2014  5    [/HTML]
[/FONT][FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Franklin Gothic Book][SIZE=3][COLOR=#000000]The code basically works (no error's messages), but it returns wrong values for some days . It’s definitely not a problem withdate formatting: it was tested and ruled out. [/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Franklin Gothic Book][SIZE=3][COLOR=#000000]It seems to me that at some stage the code failed torecognise conditions, particularly with values in columns .  [/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT]<o:p>[FONT=Franklin Gothic Book][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Franklin Gothic Book][SIZE=3][COLOR=#000000]It would be really appreciated if you give some ideas how tomodify the code to prevent overwriting (?) .   If there are other possible solutions, they are welcomed [/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Franklin Gothic Book][SIZE=3][COLOR=#000000] <o:p></o:p>[/COLOR][/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT]<o:p>[FONT=Franklin Gothic Book][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT]</o:p> 
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][SIZE=3][COLOR=#000000][FONT=Franklin Gothic Book]Excel formula  withnested if statements returns right values, but R1C1 references are not an option– all ranges must remain variables and can’t be predefined, not mention that to get results I'd need to redefine 100+ formulas.  Thank you[/FONT][/COLOR][/SIZE]
[FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT]
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,221,443
Messages
6,159,907
Members
451,601
Latest member
terrynelson55

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top