• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Inserting 2 rows if a cell meets certain criteria|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Inserting 2 rows if a cell meets certain criteria|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosInserting 2 rows if a cell meets ce…
sp_PrintTopic sp_TopicIcon
Inserting 2 rows if a cell meets certain criteria
Avatar
mreevies
Member
Members
Level 0
Forum Posts: 6
Member Since:
June 25, 2016
sp_UserOfflineSmall Offline
1
November 3, 2020 - 9:22 pm
sp_Permalink sp_Print

Hi,

I have the following code that inserts a row when a cell begins with "GS", and this works fine, however I need it to insert 2 rows, but any amendments to the code do not seem to work:

Sub GSRowsAdd()
Dim r As Range
Dim I As Long

I = 6

Do While Range("A" & I).Value <> ""
If Left(Range("A" & I), 2) = "GS" Then
I = I + 1
Rows(I).Insert
End If
I = I + 1
Loop

End Sub

 

any help greatly appreciated.

many thanks

Martin

Avatar
Miguel Santos
Member
Members
Level 0
Forum Posts: 80
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
2
November 3, 2020 - 11:37 pm
sp_Permalink sp_Print

Hello,

Without changing your code, I just made a change (highlighted in red)
this is what you want?

Sub GSRowsAdd()

Dim r As Range
Dim I As Long

I = 6

Do While Range("A" & I).Value <> ""
     If Left(Range("A" & I), 2) = "GS" Then
          I = I + 1
          Rows(I & ":" & I + 1).Insert
     End If
     I = I + 1
Loop

End Sub

 

Regards,

Miguel

Avatar
mreevies
Member
Members
Level 0
Forum Posts: 6
Member Since:
June 25, 2016
sp_UserOfflineSmall Offline
3
November 4, 2020 - 1:14 am
sp_Permalink sp_Print

Hi,

Thanks for the response.

I've tried the code and it enters the first 2 rows but then does not carry on moving down the list to the next instance where it finds "GS", but seems to start again at the beginning of the list again.

Regards

Martin

Avatar
Miguel Santos
Member
Members
Level 0
Forum Posts: 80
Member Since:
February 20, 2020
sp_UserOfflineSmall Offline
4
November 5, 2020 - 12:39 am
sp_Permalink sp_Print

Hello,

sorry for the late reply

here are several ways to get what you want, I will leave a simple example

(solution 1)

Option Explicit

Private Sub CommandButton1_Click()

Application.ThisWorkbook.Worksheets("Sheet1").Activate

Dim x As Integer
Dim str As String

str = "GS"

Application.ScreenUpdating = False
For x = Cells(Rows.Count, "A").End(xlUp).Row To 6 Step -1
     If Left(Cells(x, "A"), 2) = str Then
          Cells(x + 1, "A").EntireRow.Insert
          Cells(x + 1, "A").EntireRow.Insert
     End If
Next x
Application.ScreenUpdating = True

End Sub

(solution 2) another example, more complex

Private Sub CommandButton3_Click()

Application.ThisWorkbook.Worksheets("Sheet1").Activate

Dim x As Range
Dim str As String
Dim ArrInput() As Integer, i As Integer, a As Integer
Dim j As Long
Dim sht As Worksheet

Set sht = ActiveSheet

str = "GS"
i = 0

On Error Resume Next
ReDim ArrInput(0)
On Error GoTo 0

For Each x In Range(sht.[A6], sht.[A100].End(xlUp))
      If Left(x, 2) = str Then
           ArrInput(i) = x.Cells.Row
           i = i + 1
           ReDim Preserve ArrInput(i)
      End If
Next x

If i > 0 Then
      Call ArrayInverted(ArrInput)
     For j = LBound(ArrInput) To UBound(ArrInput)
          a = ArrInput(j)
          Debug.Print a
          If a <> 0 Then
               On Error Resume Next
               sht.Range("A" & a + 1 & ":" & "A" & (a + 2)).EntireRow.Insert
               On Error GoTo 0
          Else
               ' Cancel is true
          End If
     Next j
End If

End Sub

Public Sub ArrayInverted(originalArray As Variant)

Dim ArrOutput As Variant
Dim x As Long, endArray As Long, iniArray As Long

endArray = UBound(originalArray)
iniArray = (UBound(originalArray) - LBound(originalArray)) \ 2 + LBound(originalArray)

For x = LBound(originalArray) To iniArray
     ArrOutput = originalArray(endArray)
     originalArray(endArray) = originalArray(x)
     originalArray(x) = ArrOutput
     endArray = endArray - 1
Next x

End Sub

is it something like what you want? if it needs to be different or adapted, you'd better upload a file with only test data, it would be easier

 

Regards,

Miguel

Avatar
KENNETH BATILO
Member
Members
Level 0
Forum Posts: 27
Member Since:
January 30, 2020
sp_UserOfflineSmall Offline
5
November 5, 2020 - 6:12 am
sp_Permalink sp_Print

While using the code you provided, try this out.  Changes I've made are in Red.

Dim r As Range
Dim l As Long

l = 6

Do While Range("A" & l).Value <> ""
     If Left(Range("A" & l), 2) = "GS" Then
     l = l + 1
     Rows(l & ":" & l + 1).Insert
     l = l + 1
End If

l = l + 1
Loop

 

Ken

Avatar
mreevies
Member
Members
Level 0
Forum Posts: 6
Member Since:
June 25, 2016
sp_UserOfflineSmall Offline
6
November 5, 2020 - 9:57 pm
sp_Permalink sp_Print

Many thanks for the responses Miguel and Kenneth, and both solutions have worked well thank you.

Regards

Martin

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Scott Miller, Roy Lutke, Jeff Krueger
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27236

 

Member Stats:
Guest Posters: 49
Members: 31889
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.