Adding a row above a cell with a specific character string (to add row to bottom of table) using VBA code in excel and a macros button.

e_495

New Member
Joined
Oct 24, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, I have seen a few thread on this, however none have worked specifically for what I am trying to do.



I am create a template for a form that users will fill out in excel. The form is divided in sections and a few of these sections require an "Add New Row" button. I have the button set up and I am trying to program the code to add a new row at the end of the section. To do this I am referencing the cell below that has a specific text string in the cell, so that the new row is added above that cell. I have the following VBA code as of now. However, when I try to run the code by clicking the button, I get an error at the 8th line. I have posted a picture with the line highlighted. I am a beginner at all of this and am still learning VBA code. Is anyone able to help me with this please?



Thank you!



CODE:



Private Sub CommandButton1_Click()

Dim mySheets

Dim i As Long

Dim NewRow As Long

Const CharString As String = "Development Goals (Training, Career Growth)"



mySheets = Array("Career Plan 2022")



For i = LBound(mySheets) To UBound(mySheets)

If mySheets(i).Value = CharString Then

NewRow = mySheets(i).Row

With Sheets(mySheets(i))

.Range("Ai").EntireRow.Insert shift:=xlDown

Rows("i-1:i-1").Copy Range("i")

Range("i:i").ClearContents

End With

End If

Exit For

Next i

End Sub




PICTURE: FIND ATTACHED
 

Attachments

  • 2022-10-24_14-33-18.png
    2022-10-24_14-33-18.png
    35.5 KB · Views: 6

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I forgot to mention that the new added row should have the same formatting as the ones above it.
 
Upvote 0

Forum statistics

Threads
1,214,896
Messages
6,122,132
Members
449,066
Latest member
Andyg666

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