Insert row above if cell in column B partially contains 2 (e.g. A2,B2,C2...)

VictoriaExcel

New Member
Joined
Nov 15, 2018
Messages
13
I need a macro that inserts a row above each cell in column B that partially contains 2 e.g. A2, B2, C2 - W2.

Many thanks in advance!
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
796
Office Version
2007
Platform
Windows
Hi there. This code will do it:
Code:
Sub Macro1()
'
' Macro1 Macro
'

'
With ActiveSheet
    lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
    For rownum = lastrow To 1 Step -1
    If InStr(.Range("B" & Format(rownum)).Text, "2") > 0 Then
        .Rows(rownum).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    End If
    Next rownum
End With
End Sub
 

VictoriaExcel

New Member
Joined
Nov 15, 2018
Messages
13
Hello,

Thank you for your help with this! The macro almost works but unfortunately it inserts rows above A12, B12, C12 etc as well as the intended A2, B2, C2.

Can it work around this with an exception?

Many thanks,

Victoria
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
796
Office Version
2007
Platform
Windows
Hi Victoria

OK, so if I understand it, you only want blanks above rows in column B which only contain 2 - so A12, C21, D22 etc would not want a blank row. If thats the case, try this:
Code:
Function GetNumeric(CellRef As String)
Dim StringLength As Integer
StringLength = Len(CellRef)
For i = 1 To StringLength
If IsNumeric(Mid(CellRef, i, 1)) Then Result = Result & Mid(CellRef, i, 1)
Next i
GetNumeric = Result
End Function
Sub Macro1()
'
' Macro1 Macro
'

'
With ActiveSheet
    lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
    For rownum = lastrow To 1 Step -1
    If GetNumeric(.Range("B" & rownum).Text) = 2 Then
        .Rows(rownum).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    End If
    Next rownum
End With
End Sub
I must admit to lifting the getnumeric code from another forum post - what it does is extract digits only from the entire cell. Hope this helps.

John
 
Last edited:

Forum statistics

Threads
1,082,131
Messages
5,363,337
Members
400,726
Latest member
Shahzad Taimoor

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top