Challening time question

PiPaPils

New Member
Joined
Feb 7, 2018
Messages
9
Hi all!

There is something I'd like to do, but unfortunately am not able to do so myself...

I have several sheets with entries of trades (in the example it is the "Trades" sheet). A trade is opened at date X, time Y and it closes at date Z and time Q. On another sheet ("Exposure" sheet in the example) I have a long list with all possible dates with steps of 1 hour. What I want to do is to check each entry in the Trades sheet, and modify the cells in the Exposure sheet if the times are between open and close date. An example might illustrate this best:

eQQ7bU
eQQ7bU
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The first sheet looks like this:

8oex3tndg.jpg
[/IMG]



So in the second sheet I have a list that looks like this. The first entry runs from 20-01-10 10:46 to 21-01-10 0:35. I'd like to modify all cells in that range:



eQQ7bU
mschsqma4za.jpg
[/IMG]
 
Upvote 0
With formula? VBA? Conditional formatting?

In your trade open column you can simply do

Code:
=IF(AND(B6>=$C$2,B6<=$D$2),1,0)
 
Last edited:
Upvote 0
Hi Jumbo!

I tried that if statement, but the issue is that C2 and D2 need to go for the next cell, so C3 and D3 after they checked for C2 and D2. Was thinking of a For Each... Cell in range loop, but I do not really know to properly build a loop in a loop...

Kind regards,

Koen
 
Upvote 0
this code should do it for you:
Code:
Sub test()Dim cnt As Variant


With Worksheets("Sheet1")
 lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
 inarr = Range(.Cells(1, 2), .Cells(lastrow, 3))
End With
With Worksheets("Sheet2")
 lastdate = .Cells(Rows.Count, "B").End(xlUp).Row
 datearr = Range(.Cells(1, 2), .Cells(lastdate, 2))
 Range(.Cells(1, 3), .Cells(lastdate, 3)) = ""
 outarr = Range(.Cells(1, 3), .Cells(lastdate, 3))
cnt = 0


For i = 7 To lastdate
   For j = 6 To lastrow
    If inarr(j, 1) >= datearr(i - 1, 1) And inarr(j, 1) < datearr(i, 1) Then
     cnt = cnt + 1
    End If
  
    If inarr(j, 2) >= datearr(i - 1, 1) And inarr(j, 2) < datearr(i, 1) Then
     cnt = cnt - 1
    End If
   Next j
   
   Range(.Cells(i, 3), .Cells(i, 3)) = cnt
 Next i


 
 End With


End Sub
 
Last edited:
Upvote 0
this code should do it for you:
Code:
Sub test()Dim cnt As Variant


With Worksheets("Sheet1")
 lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
 inarr = Range(.Cells(1, 2), .Cells(lastrow, 3))
End With
With Worksheets("Sheet2")
 lastdate = .Cells(Rows.Count, "B").End(xlUp).Row
 datearr = Range(.Cells(1, 2), .Cells(lastdate, 2))
 Range(.Cells(1, 3), .Cells(lastdate, 3)) = ""
 outarr = Range(.Cells(1, 3), .Cells(lastdate, 3))
cnt = 0


For i = 7 To lastdate
   For j = 6 To lastrow
    If inarr(j, 1) >= datearr(i - 1, 1) And inarr(j, 1) < datearr(i, 1) Then
     cnt = cnt + 1
    End If
  
    If inarr(j, 2) >= datearr(i - 1, 1) And inarr(j, 2) < datearr(i, 1) Then
     cnt = cnt - 1
    End If
   Next j
   
   Range(.Cells(i, 3), .Cells(i, 3)) = cnt
 Next i


 
 End With


End Sub


Ah superb! This is exactly what I need!

Thanks a lot!!!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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