Insert A New Row Based on a Criteria linked with button

Jafir

New Member
Joined
Jul 27, 2023
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
Hi all,

I've been searching a solution to my issue on the board but could not find a working solution that covers my needs.
I'd really appreciate some help to come up with a VBA code that does the following:
I have two sheets in a workbook like below;

1690454341827.png



In sheet2 I have a cell B contains some value like below;
1690454609833.png

Once I press the Button 3, a new row (based on criteria B2 above) should be added in Sheet1 (below 4) like below.
1690454720188.png


Thanks for your help
 

Attachments

  • 1690454540086.png
    1690454540086.png
    3.3 KB · Views: 9

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this code for your button
VBA Code:
Private Sub btnAddRow_Click()
  Dim wks As Worksheet
  Dim LR As Long
  Dim siteID
  Dim siteIDFound As Boolean
  Set wks = ActiveSheet
  
  LR = wks.Range("A" & Rows.Count).End(xlUp).Row
  
  siteID = wks.Range("$B$1")
  siteIDFound = False
  For r = 2 To LR
    If wks.Cells(r, 1) = siteID And Not siteIDFound Then
      siteIDFound = True
    End If
    
    If siteIDFound And wks.Cells(r, 1) <> siteID Then
      wks.Range("A" & r).Insert xlShiftDown
      Exit For
    End If
  Next r
  
  If Not siteIDFound Then
    MsgBox "Site ID: " & siteID & " not found."
  End If
  
End Sub
 
Upvote 0
Welcome to the MrExcel board!

Hard to see for sure but it looks like that ID number in Sheet2 is in cell B2
Based on that, try attaching this macro to Button 3
Excel Formula:
Sub Insert_Row()
  On Error Resume Next
  Sheets("Sheet1").Columns("A").Find(What:=Range("B2").Value, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious).Offset(1).EntireRow.Insert
End Sub
 
Upvote 0
Welcome to the MrExcel board!

Hard to see for sure but it looks like that ID number in Sheet2 is in cell B2
Based on that, try attaching this macro to Button 3
Excel Formula:
Sub Insert_Row()
  On Error Resume Next
  Sheets("Sheet1").Columns("A").Find(What:=Range("B2").Value, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious).Offset(1).EntireRow.Insert
End Sub
It works perfectly. However, there is another challenge that Newly created rows are not possible to remove.
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,922
Messages
6,175,406
Members
452,640
Latest member
steveridge

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