VBA Find value, insert row above, format

exo33

New Member
Joined
Oct 29, 2014
Messages
33
Hello All,

I found below code on the forum and modified it slightly. Sadly, I keep having issues with it. Long story short, I have a set of text values in column I. I want Excel to find the first value "Good", insert a blank row right above it, then go 4 cells to the left and label it "Above good".
For example:
If "Good" value is found in cell I200, I want a new row inserted - row 199 and "Above good" text in cell E199. If no "Good" values are found in column I, do nothing.

The issue I'm having is that sometimes value "Good" will pop up in column E and Excel will insert row above it, instead of searching in column "I" ONLY.

Code:
Sub EETC()Sheets("Template").Select
    Dim cl As Range
    With Worksheets("Template").Cells
        Set cl = .Find("Good", After:=.Range("I2"), LookIn:=xlValues)
        If Not cl Is Nothing Then
            cl.Select
        End If
    End With


    ActiveCell.Offset(0).EntireRow.Insert
    ActiveCell.Offset(0, -4).FormulaR1C1 = "Above good"
    ActiveCell.Offset(0, -4).Select
    Selection.Font.Bold = True



End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Change this:
Code:
With Worksheets("Template").Cells

To this:
Code:
With Worksheets("Template").Range("I:I")
 
Upvote 0
Change this:
Rich (BB code):
With Worksheets("Template").Cells

To this:
Rich (BB code):
With Worksheets("Template").Range("I:I")

Runtime error '13' Type mismatch

Code:
Sub EETC()

Sheets("Template").Select
    Dim cl As Range
    With Worksheets("Template").Range("I:I")
        [COLOR=#ff0000][B]Set cl = .Find("Good", After:=.Range("I2"), LookIn:=xlValues)[/B][/COLOR]
        If Not cl Is Nothing Then
            cl.Select
        End If
    End With


    ActiveCell.Offset(0).EntireRow.Insert
    ActiveCell.Offset(0, -4).FormulaR1C1 = "Above good"
    ActiveCell.Offset(0, -4).Select
    Selection.Font.Bold = True



End Sub
 
Last edited:
Upvote 0
Change the line that causes the error to this:
Code:
 Set cl = .Find("Good", After:=.Cells(2), LookIn:=xlValues)
 
Upvote 0
Change the line that causes the error to this:
Code:
 Set cl = .Find("Good", After:=.Cells(2), LookIn:=xlValues)
Now it inserts empty row above a cell I had selected prior to running the macro. i.e. if I click on cell D12 before running macro, new row 11 is created above it.
 
Upvote 0
Fixed by simplifying the code (see below). I'm assuming that using "Select" is not the best practice?

Code:
Sub Macro1()
'
' Macro1 Macro
Sheets("Template").Select
Dim cl As Range
Set cl = Range("I:I").Find("Good")
If Not cl Is Nothing Then cl.Select
    ActiveCell.Offset(0).EntireRow.Insert
    ActiveCell.Offset(0, -4).FormulaR1C1 = "Above good"
    ActiveCell.Offset(0, -4).Select
    Selection.Font.Bold = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,548
Members
449,038
Latest member
Guest1337

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