Enter "Note" below specific text

Panoos64

Well-known Member
Joined
Mar 1, 2014
Messages
890
Hi all,
I would like to create a VBA code in excel 2016, so that to run through col. "AK" and where find the text "NET PROFIT / LOSS" should enter below after a blank row the below note:

"Note: The above Net Profit & Loss arises, thereafter we entered the accounts dividends received, banks interest and dividends payable"

Many thanks in advance
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Will it only occur once on the sheet, or could it occur more than once?

So, if it found on say, row 41, are you saying that it should insert a blank row under row 41, and place that note in row 42?
 
Upvote 0
If my assumptions are correct, try this. This will work for one or more found entries in the cell:
Code:
Sub MyInsertMacro2()

    Dim fRow As Long
    Dim pRow As Long

    pRow = 1
    fRow = 1
    
    Do
    
'       Find value
        Columns("AK:AK").Find(What:="NET PROFIT / LOSS", After:=Cells(pRow, "AK"), LookIn:= _
            xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        
'       Capture row of found value
        fRow = ActiveCell.Row


'       Exit if found row less than previous row
        If fRow < pRow Then Exit Do
    
'       Insert blank row
        Rows(fRow + 1).Insert
    
'       Enter note in next row
        Range("AK" & fRow + 1) = "Note: The above Net Profit & Loss arises, thereafter we entered the accounts dividends received, banks interest and dividends payable"
    
'       Underline word "Note"
        With Range("AK" & fRow + 1).Characters(Start:=1, Length:=5).Font
            .Underline = xlUnderlineStyleSingle
        End With
        
'       Set previous row value
        pRow = fRow
        
    Loop
    
End Sub
If you do not need to insert a blank row, then just remove that part from the code above.
 
Upvote 0
Hi Joe and many thanks for your support!
the code works, just a small amend is require. It fall in loop and it still write the note below again and again. I do not know in which row it stop, but i think it write it till end of excel rows. I was ending by end of task because it reach on phase by which it dosen't respond.
Thank you once again and have a great day
 
Upvote 0
just a small amend is require. It fall in loop and it still write the note below again and again.
Not sure why that would be. In your example, what rows does the phrase "NET PROFIT / LOSS" appear in?

Here is some amended code that will not loop more than 100 times, and handles the error of not finding the value at all:
Code:
Sub MyInsertMacro2()

    Dim fRow As Long
    Dim pRow As Long
    Dim i As Long

    pRow = 1
    fRow = 1
    
    Do Until i = 100
    
        On Error GoTo err_check
'       Find value
        Columns("AK:AK").Find(What:="NET PROFIT / LOSS", After:=Cells(pRow, "AK"), LookIn:= _
            xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        
'       Capture row of found value
        fRow = ActiveCell.Row


'       Exit if found row less than previous row
        If fRow < pRow Then Exit Do
    
'       Insert blank row
        Rows(fRow + 1).Insert
    
'       Enter note in next row
        Range("AK" & fRow + 1) = "Note: The above Net Profit & Loss arises, thereafter we entered the accounts dividends received, banks interest and dividends payable"
    
'       Underline word "Note"
        With Range("AK" & fRow + 1).Characters(Start:=1, Length:=5).Font
            .Underline = xlUnderlineStyleSingle
        End With
        
'       Set previous row value
        pRow = fRow
        
'       Loop counter
        i = i + 1
        
    Loop
    On Error GoTo 0
    
    Exit Sub
    
'error handling
err_check:
    If Err.Number = 91 Then
        MsgBox "Cannot find desired string", vbOKOnly, "ERROR!"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
    
End Sub
 
Upvote 0
Hi Joe, it works perfect now and i want to say a great thanks to you for all support you done for my project.

Hv a great lovely day!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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