Help needed - lookup two conditions within array, return results and add row

bovril33

New Member
Joined
Jan 26, 2015
Messages
2
Hi,

I am fairly new to VBA and am looking for some advice. I am trying to write code that goes through two ranges in two separate columns, checks against a condition and returns a value if true. Crucially, for each value I'd like it to add a row below and return the next value.

For the first part I had the following code:

Sub fillOB1()

nextRow = 5

For x = 6 To 69
If Sheets(1).Cells(x, 5) > "1" And Sheets(1).Cells(x, 15) > "1" Then

Sheets(2).Cells(nextRow, 4).Value = Sheets(1).Cells(x, 2).Value
Sheets(2).Cells(nextRow, 5).Value = Sheets(1).Cells(x, 3).Value

nextRow = nextRow + 1
End If
Next x

End Sub

But this relies on the output table having enough rows to contain all true values. However as there is another table below the output table and I don't want it to spill over, I thought about adding new rows underneath each value. I tried to do this using the ActiveCell function, but ran into compiling issues:

Sub fillOB1()

Sheets(2).Select
Range(D5).Select

For x = 6 To 69
If Sheets(1).Cells(x, 5) > "1" And Sheets(1).Cells(x, 15) > "1" Then
Sheets(2).Cells(ActiveCell, 4).Value = Sheets(1).Cells(x, 2).Value
Sheets(2).Cells(ActiveCell.Offset(0, 1), 5).Value = Sheets(1).Cells(x, 3).Value

ActiveCell.Offset(1).EntireRow.Insert

Range (ActiveCell.Offset(1, -1))

End If
Next x


End Sub

Can anyone tell me what I am doing wrong, or suggest an alternative method?

Thanks!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Welcome to MrExcel.

Does this work for you?

Code:
Sub fillOB1()
    Dim nextrow As Long
    Dim x As Long
    nextrow = 5
    For x = 6 To 69
        If Sheets(1).Cells(x, 5) > "1" And Sheets(1).Cells(x, 15) > "1" Then
            Sheets(2).Cells(nextrow, 4).Value = Sheets(1).Cells(x, 2).Value
            Sheets(2).Cells(nextrow, 5).Value = Sheets(1).Cells(x, 3).Value
            Sheets(2).Cells(nextrow, 1).Offset(1).EntireRow.Insert
            nextrow = nextrow + 1
        End If
    Next x
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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