VBA to extract cell data based on If ... And ... And ...

UseLessFuel

New Member
Joined
Dec 22, 2012
Messages
37
Hello. I am dealing with large variable-length time-series datasets of domestic heating system sensor data. I need to extract specific cells data based on the content of three cells. Only two columns of data (L and N) are of interest in the attached HTML, although the raw data extends from columns A to U. I have added column W to show what I would like final result to appear in column N.

ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1DateTimeOutdoor Set roomRoomSet room (Zone2)Room (Zone2)Set flow FlowReturnSet tankTankConsumedWhDeliveredWhUnitModeDefrData 30Set - Z1Set - Z2Set - FlowFlow - returnUnitModeTotal Mode 0 Consumed Wh First Mode 0 after Mode 2 (Wh con)
201/10/2017 07:27132020.51819.5272726.55047119202-0.5-1.500.5290 1st zero: 8089%
301/10/2017 07:28142020.51819.52726.526.5504713202-0.5-1.50.502NEED LAST 2nd zero:56%
401/10/2017 07:29142020.51819.5272726.55046.522202-0.5-1.500.52ROW FOR 3rd zero: 11%
501/10/2017 07:30132020.51819.52728.52750471214000-0.5-1.5-1.51.53ABOVE!Above uses
601/10/2017 07:31132020.51819.52727275046.51034202-0.5-1.5002Sum (Col:Col) only
701/10/2017 07:32132020.51819.52727275046.5118202-0.5-1.50020
801/10/2017 07:33142020.51819.52726.526.55046.523202-0.5-1.50.502
901/10/2017 07:34132020.51819.5272726.55046.512202-0.5-1.500.52
1001/10/2017 07:35132020.51819.52728.5275046.51314000-0.5-1.5-1.51.53
1101/10/2017 07:36132020.51819.52727275046.5937202-0.5-1.5002
1201/10/2017 07:37132020.51819.52727275046.5111202-0.5-1.5002
1301/10/2017 07:38142020.51819.52726.526.55046.514202-0.5-1.50.502
1401/10/2017 07:39132020.51819.5272726.55046.532202-0.5-1.500.52
1501/10/2017 07:40132020.51819.52729275046.51214000-0.5-1.5-223
1601/10/2017 07:41132020.51819.52727275046.5838202-0.5-1.5002
1701/10/2017 07:42132020.51819.52727275046.514202-0.5-1.5002
1801/10/2017 07:43142020.51819.52726.526.55046.523202-0.5-1.50.502
1901/10/2017 07:44142020.51819.52727.526.5504730202-0.5-1.5-0.512
2001/10/2017 07:45132020.51819.52729275046.51215000-0.5-1.5-223
2101/10/2017 07:46132020.51819.52727275046.5739202-0.5-1.5002
2201/10/2017 07:47142020.51819.52727275046.520202-0.5-1.5002
2301/10/2017 07:48142020.51819.52726.526.55046.513202-0.5-1.50.502
2401/10/2017 07:49142020.51819.52727.526.55046.540202-0.5-1.5-0.512
2501/10/2017 07:50142020.51819.52628.5275046.51321000-0.5-1.5-2.51.53
2601/10/2017 07:51142020.51819.52626.5275046.5538000-0.5-1.5-0.5-0.54
2701/10/2017 07:52142020.51819.52626.526.55046.510000-0.5-1.5-0.505
2801/10/2017 07:53152020.51819.52626.526.55046.513000-0.5-1.5-0.500
2901/10/2017 07:54152020.51819.52626.526.55046.520000-0.5-1.5-0.500
3001/10/2017 07:55152020.51819.52626265046.510000-0.5-1.5000
3101/10/2017 07:56152020.51819.52626265046.510202-0.5-1.5002
3201/10/2017 07:57142020.51819.52627265046.530202-0.5-1.5-112
3301/10/2017 07:58142020.51819.52627.526.55046.51215000-0.5-1.5-1.513
3401/10/2017 07:59142020.51819.5262626.55046.5530202-0.5-1.50-0.52
3501/10/2017 08:00142020.51819.52626265046.510202-0.5-1.5002
3601/10/2017 08:00142020.51819.52626265046.515202-0.5-1.5002
3701/10/2017 08:01142020.51819.52627.5265046.560000-0.5-1.5-1.51.53
3801/10/2017 08:02142020.51819.52626.526.55046.51133202-0.5-1.5-0.502

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1 (2)

Worksheet Formulas
CellFormula
Y2=SUMIF(N2:N38,"=0",L2:L38)
AA2=SUMIF(W:W,"=3",L:L)
AA3=SUMIF(W:W,"=4",L:L)
AA4=SUMIF(W:W,"=5",L:L)
Y7=SUMIFS(L:L,W:W,"=0",W:W,"=3",W:W,"=4",W:W,"=5")

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

(Please ignore the Y7 formula above. Still getting used to the HTMLMaker!)

I will try to explain:
In column N, if a UnitMode = 2 is immediately followed by a UnitMode =0 AND if (in Column L) the ConsumedWh is >0, I wish to change the UnitMode = 0 to the number 3, otherwise leave the UnitMode as is. For the UnitMode in cell N2, an expression for this would be:

=IF(AND(N1=2,N2=0,L2>0),place 3 in N2,N2) which, in this case, would result in the UnitMode for cell N2 remaining as 2.

If a second UnitMode = 0, follows a UnitMode =2, AND the ConsumedWh>0 (as in Row 26) I wish to change the UnitMode = 0 to the number 4. Finally, if a third consecutive UnitMode = 0 follows a UnitMode =2, AND the ConsumedWh>0, I wish to change the UnitMode = 0 to the number 5 (as in Row 27). See column W for what I would like to see in Column N (without formatting).

I would also like to add some summary results, shown in the range Y1:AB4.
The equation in Y1 is: =SUMIF(W:W,"=0",L:L)+AA2+AA3+AA4
In AA2, the equation is SUMIF(W:W,"=3",L:L)
In AA3, the equation is=SUMIF(W:W,"=4",L:L)
In AA4, the equation is=SUMIF(W:W,"=5",L:L)


My attempt at coding for the first Mode = 0 following a Mode = 2, is shown below, where I had tried to create a new Column called FirstZeroWh to store the results in.

Rich (BB code):
Sub ZeroModeCaptureWh() 
' get the last row
Dim startRow As Long, lastRow As Long
startRow = 2
lastRow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row


Dim i As Long, UnitMode As Long, UnitModeNext As Long, ConsumedWh As Long
Dim FirstZeroWh As String


' Go through the UnitMode column
For i = startRow To lastRow
UnitMode = Sheet1.Range("N" & i).Value
ConsumedWh = Sheet1.Range("L" & i).Value
UnitModeNext = Sheet1.Range("N" & (i + 1)).Value


' Check UnitMode and classify accordingly
If UnitMode = 2 And UnitModeNext = 0 And ConsumedWh > 0 Then
FirstZeroWh = "7"
Else
' For all other cases
FirstZeroWh = UnitMode
End If


' Write out the class to column W
Sheet1.Range("W" & i).Value = FirstZeroWh
Next


End Sub



I was doing okay until I added:
[ConsumedWh = Sheet1.Range("L" & i).Value
UnitModeNext = Sheet1.Range("N" & (i + 1)).Value]

which was a bit of a guess!

Any help would be very much appreciated.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This only addresses the data extraction portion of your question and not the summary formulas at the bottom.

Code:
Sub ZeroModeCaptureWh()
    Dim ZCnt As Long
    ZCnt = 0
    
    Dim WS As Worksheet
    Set WS = Worksheets("Sheet1")

    ' get the last row
    Dim startRow As Long, lastRow As Long
    startRow = 2
    lastRow = WS.Cells(WS.Rows.Count, 1).End(xlUp).Row

    Dim i As Long, UnitMode As Long, UnitModePrev As Long, ConsumedWhPrev As Long
    Dim FirstZeroWh As String

    WS.Range("W" & startRow).Value = WS.Range("N" & startRow).Value    'start row special case

    ' Go through the UnitMode column
    For i = startRow + 1 To lastRow
        UnitMode = WS.Range("N" & i).Value
        UnitModePrev = WS.Range("N" & (i - 1)).Value
        ConsumedWhPrev = WS.Range("L" & (i - 1)).Value

        If UnitMode = 0 And ZCnt > 0 Then
            ZCnt = ZCnt + 1
        Else
            ZCnt = 0
        End If

        ' Check UnitModePrev and classify accordingly
        If UnitModePrev = 2 And UnitMode = 0 And ConsumedWhPrev > 0 Then
            ZCnt = ZCnt + 1
            FirstZeroWh = "3"
        Else
            ' For all other cases
            Select Case ZCnt
            Case 2
                FirstZeroWh = "4"
            Case 3
                FirstZeroWh = "5"
            Case Else
                FirstZeroWh = UnitMode
            End Select
        End If

        ' Write out the class to column W
        Debug.Print i
        WS.Range("W" & i).Value = FirstZeroWh
    Next i
End Sub
 
Upvote 0
rlv01, that's great. Thanks for spending the time on this. I made one minor change, replacing the variable ConsumedWhPrev with ConsumedWh and the following code (so that the same row as UnitMode is checked, as opposed to the previous row):

Code:
ConsumedWh = WS.Range("L" & i).Value

It works perfectly.
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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