Find or Loop?

Jameo

Active Member
Joined
Apr 14, 2011
Messages
270
Good day all,

I have a series of data consisting of a date column, and a value column. The values relate to actual flow rates of a gas at minute intervals, with the date column in the form of dd/mm/yyyy hh:mm.

I then have a seperate set of data, on a seperate sheet relating to the nominations (the amount of gas that acutally should have flowed). This data is again set up in two columns, a date column in the same format, and another value column.

This data is only given on the hour.

To align my data, I need to insert the nomination next to the actual flow. Obviously this is going to consist of the same nomination value against all actual flows in the same hour (60 readings).

Now normally I would just accomplish this with a coulple of loops. For each actual time and value I would cycle through the nominations and find the value for the matching time. However, this is not very efficient, and possibly be better using the find function?

Any input into the best way to procede would be appreicated.

Thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Copy down the formula and copy | paste-special | Values?
Excel Workbook
ABCDEF
1date/timeflownom.flowdate/timehourly nominal
223/09/2011 08:001.123/09/2011 08:001.1
323/09/2011 08:011.123/09/2011 09:002.2
423/09/2011 08:021.123/09/2011 10:003.3
523/09/2011 08:031.123/09/2011 11:004.4
623/09/2011 08:041.123/09/2011 12:005.5
723/09/2011 08:511.123/09/2011 13:006.6
823/09/2011 08:521.123/09/2011 14:007.7
923/09/2011 08:531.123/09/2011 15:008.8
1023/09/2011 08:541.123/09/2011 16:009.9
1123/09/2011 08:551.123/09/2011 17:0011
1223/09/2011 08:561.123/09/2011 18:0012.1
1323/09/2011 08:571.123/09/2011 19:0013.2
1423/09/2011 08:581.123/09/2011 20:0014.3
1523/09/2011 08:591.123/09/2011 21:0015.4
1623/09/2011 09:002.223/09/2011 22:0016.5
1723/09/2011 09:012.223/09/2011 23:0017.6
1823/09/2011 09:022.224/09/2011 00:0018.7
1923/09/2011 09:572.224/09/2011 01:0019.8
2023/09/2011 09:582.224/09/2011 02:0020.9
2123/09/2011 09:592.224/09/2011 03:0022
2223/09/2011 10:003.324/09/2011 04:0023.1
2323/09/2011 10:013.324/09/2011 05:0024.2
2423/09/2011 10:023.3
2523/09/2011 10:583.3
2623/09/2011 10:593.3
2723/09/2011 11:004.4
2823/09/2011 11:014.4
2923/09/2011 11:584.4
3023/09/2011 11:594.4
3123/09/2011 12:005.5
3223/09/2011 12:015.5
3324[/B]/09/2011 00:5818.7
3424/09/2011 00:5918.7
3524/09/2011 01:0019.8
3624/09/2011 01:0119.8
3724/09/2011 01:0219.8
3824/09/2011 01:0319.8
3924/09/2011 01:0419.8
4024/09/2011 01:0519.8
4124/09/2011 01:0619.8
4224/09/2011 01:0719.8
4324/09/2011 01:0819.8
4424/09/2011 01:0919.8
Sheet28
 
Last edited:
Upvote 0
Thanks, but I have to do this for multiple data sets now and in the future. It really needs to be a VBA method.

I could easilly do this with a couple of loops. One to cycle through the actual values, and another to cycle through the nom values, and then copy and paste into the relevent position. However, if we get to the point where I have to do this for a years worth of data the inefficiency in the code would start to become an issue. I am really looking for a different methodology if possible.

Cheers for your help though
 
Upvote 0
With the same ranges as in my first response:
Code:
Sub blah()
With Range("C2:C44")
    .FormulaR1C1 = "=VLOOKUP(RC[-2],R1C5:R23C6,2)"
    '.FormulaR1C1 = "=VLOOKUP(RC[-2],NomFlow,2)"'instead, if you give you nominal flow table a name.
    .Value = .Value
End With
End Sub
 
Upvote 0
Not to sound ungrateful p45cal, but I am not happy with the idea of using vlookups purely because of the shear amount of data my workbook will eventually contain. The systems at work are not the best, and I can envisage the time when the calculations performed reach a point where they become a serious issue.
 
Upvote 0
For instance, the following code does the job that I want, just not ver efficeintly. Is there any other methods anyone could reccomend that would do the job better?

Code:
Option Explicit
Public Sub LOP()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim IntRow As Long, i As Long
Dim LRow1 As Long, LRow2 As Long
Dim CuTime As Long
Dim LOPCol As Long, LOPCol1 As Long
Set ws1 = Worksheets("IoG - Flow Total - 22nd Sep")
Set ws2 = Worksheets("IoG - LOP - 22nd Sep")
LRow2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
LRow1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
IntRow = 1
LOPCol = 1
LOPCol1 = 4
    Do
    LOPCol = LOPCol + 1
    LOPCol1 = LOPCol1 + 1
    
        For i = 2 To LRow1
        IntRow = 1
        CuTime = Hour(ws1.Cells(i, 1).Value)
        
            Do
                IntRow = IntRow + 1
                If Hour(ws2.Cells(IntRow, 1).Value) = CuTime Then
                    ws2.Cells(IntRow, LOPCol).Copy Destination:=ws1.Cells(i, LOPCol1)
                End If
                
                
        
            Loop Until IntRow >= LRow2
        Next i
        
    Loop Until LOPCol = 3
End Sub
 
Upvote 0
Again, with the same ranges as my first response, try:
Code:
Sub blah()
With Range("A2:A44")
    xxx = .Value
    yyy = Range("$E$2:$F$23").Value    'your hourly nominal table
    Dim Result()
    ReDim Result(1 To UBound(xxx), 1 To 1)
    For i = 1 To UBound(xxx)
        lngxxx = Int(xxx(i, 1) + 0.0000000001)
        xxHour = Hour(xxx(i, 1))
        For j = 1 To UBound(yyy)
            lngyyy = Int(yyy(j, 1) + 0.0000000001)
            If lngyyy = lngxxx Then
                If Hour(yyy(j, 1)) = xxHour Then
                    Result(i, 1) = yyy(j, 2)
                    Exit For
                End If
            End If
        Next j
        If IsEmpty(Result(i, 1)) Then Stop
    Next i
    .Offset(, 2) = Result
End With    'Range("A2:A44")
End Sub
It tries to do much of the work in memory. I had a struggle with my dates; I try to compare both the integer parts of two dates and the Hour portion of the same two dates and if they're both the same, put the nominal flow value into the results array. This should be straightforward, however it turns out that when you try in vba to int() a date which is exactly midnight (cell E18 in my example above) you get the day before! On the worksheet you get the right date. Hence the ugly + 0.0000000001 parts of the code.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,219
Members
452,895
Latest member
BILLING GUY

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