Help with if statement in VBA code!

ktsmith21

New Member
Joined
Feb 25, 2015
Messages
16
Can somebody help me figure out the code below? I'm trying to insert rows as part of a macro. I have the code inserting lines working, but I need it only to insert lines where the value in column B is greater than 1000.

VBA Code:
    Dim ws As Worksheet
    Dim rowsn As Integer, count2 As Integer
    Dim line As Range
    Dim partners As Integer
    Set ws = ActiveWorkbook.Sheets("UNIT_PRICE_COMP")
        ws.Activate
    Dim i As Long
    partners = Worksheets("Settings").Range("C13").Value
    
        For i = Range("B" & Rows.count).End(xlUp).Row To 8 Step -1
            'If Cells(count, 2).Value > 1000 Then
                Rows(i).Resize(partners - 1).Insert
            'End If
        Next i

Currently, the code is producing this:
Closing Workbook_02.24.2020.jpg


Finally, I need the value in column B of each inserted row to match the value of the cell being evaluated as part of the loop.

Thank you in advanve for any help!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this

VBA Code:
      For i = Range("B" & Rows.Count).End(xlUp).Row To 8 Step -1
        If Cells(i, 2).Value >= 1000 Then
            Rows(i + 1).Resize(partners - 1).Insert
        End If
      Next i
 
Upvote 0
Dante,
Worked great! Thank you. You even changed it to be >= to 1000, good catch there (y)

Any idea on how to pull the value in column B into each of the inserted rows? For example, if I am inserting three rows under value 1000, I need each of those three rows to = 1000 also.
 
Upvote 0
I need each of those three rows to = 1000 also.
Try:

VBA Code:
  For i = Range("B" & Rows.Count).End(xlUp).Row To 8 Step -1
    If Cells(i, 2).Value >= 1000 Then
      Rows(i + 1).Resize(partners - 1).Insert
      Cells(i + 1, 2).Resize(partners - 1).Value = Cells(i, 2).Value
    End If
  Next i
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,664
Messages
6,126,101
Members
449,292
Latest member
Mario BR

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