Help to fix the VBA code to use the above line when the condition is =1

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone. I need to filter in the range B2:D100 only when column B has the value =1 , and when outputting the first row results for the rows below (As the image describes). Thank you

1682263827799.png


VBA Code:
Sub FILLTEST()
On Error Resume Next
Dim sArr(), dArr(), I As Long, K As Long, R As Long, Col As Long
sArr = Range("B2:D1000").Value
R = UBound(sArr)
ReDim dArr(1 To R, 1 To 3)
For I = 1 To R
    If sArr(I, 1) = 1 Then
        K = K + 1
        For Col = 1 To 3
            dArr(K, Col) = sArr(I, Col)
        Next Col
    End If
Next I
Range("G2:I31").ClearContents
Range("G2").Resize(K, 3) = dArr
End Sub

 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Perhap something like this.
VBA Code:
Sub FILLTEST()
    'On Error Resume Next
    Dim sArr(), dArr(), I As Long, K As Long, R As Long, Col As Long
    Dim LastX As Variant
    
    sArr = Range("B2:D1000").Value
    R = UBound(sArr)
    ReDim dArr(1 To R, 1 To 3)
    For I = 1 To R
        If sArr(I, 1) = 1 Then
            K = K + 1
            For Col = 1 To 3
                dArr(K, Col) = sArr(I, Col)
            Next Col
            If Not IsEmpty(dArr(K, 3)) Then
                LastX = dArr(K, 3)
            Else
                dArr(K, 3) = LastX
            End If
        End If
    Next I
    Range("G2:I31").ClearContents
    Range("G2").Resize(K, 3) = dArr
End Sub
 
Upvote 0
Solution
Perhap something like this.
VBA Code:
Sub FILLTEST()
    'On Error Resume Next
    Dim sArr(), dArr(), I As Long, K As Long, R As Long, Col As Long
    Dim LastX As Variant
   
    sArr = Range("B2:D1000").Value
    R = UBound(sArr)
    ReDim dArr(1 To R, 1 To 3)
    For I = 1 To R
        If sArr(I, 1) = 1 Then
            K = K + 1
            For Col = 1 To 3
                dArr(K, Col) = sArr(I, Col)
            Next Col
            If Not IsEmpty(dArr(K, 3)) Then
                LastX = dArr(K, 3)
            Else
                dArr(K, 3) = LastX
            End If
        End If
    Next I
    Range("G2:I31").ClearContents
    Range("G2").Resize(K, 3) = dArr
End Sub
Thank You.
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,132
Members
449,098
Latest member
Doanvanhieu

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