VBA to insert row on certain condition

MiniFav

Board Regular
Joined
Mar 10, 2020
Messages
85
Office Version
  1. 365
Platform
  1. Windows
Hey everyone, Formulas have taken me so far but now feel like i need VBA to help me out.

With the table below I have data in column H and along side there may be data in Column I.
I wish to have a new row created if there is data in Column I and then the value inserted into the column H space created.
This may sometimes have several references where ideally I would like new rows for each or the ability to re-run the code until there is nothing left in column I.

Anyone have any help for this?

MPD with formulas MF.xls
HI
385000-28-210-700-A01
386000-28-520-711-A0100-28-620-711-A01
387000-28-520-711-A0100-28-620-711-A01
388000-28-520-712-A0100-28-620-712-A01
389000-28-520-712-A0100-28-620-712-A01
390000-28-520-713-A0100-28-520-713-A02 000-28-620-713-A01 000-28-620-713-A02
Sheet1
 
Hello,

I'm sorry I cannot figure out what you want. Could you provide the expected output for the example you posted? Thank you.
 
Upvote 0
Hi @MiniFav
Try the below code on a copy of your workbook

VBA Code:
Option Explicit
Option Base 1
Sub test()
   Dim w As Worksheet, r As Range, l As Long, j As Long, k As Long
   Dim H() As Variant, I() As Variant
   Set w = ActiveWorkbook.ActiveSheet
   l = w.Cells(Rows.Count, "I").End(xlUp).Row
   Set r = w.Range(Cells(1, "H"), Cells(l, "I"))
   ReDim H(l): ReDim I(l)
   For j = 1 To l
      H(j) = r(j, 1)
      I(j) = r(j, 2)
   Next j
   k = 1
   For j = 1 To UBound(I)
      If I(j) <> "" Then
         w.Cells(k, "H") = ""
         w.Cells(k, "I") = ""
         w.Cells(k, 1).EntireRow.Insert (xlShiftDown)
         w.Cells(k, "H") = H(j)
         w.Cells(k + 1, "H") = I(j)
         k = k + 2
      Else
         k = k + 1
      End If
   Next j
End Sub
 
Upvote 0
Is this what you mean?
Test with a copy of your workbook.

VBA Code:
Sub Test()
  Dim a As Variant, b As Variant, itm As Variant
  Dim i As Long, k As Long
  
  ReDim b(1 To Rows.Count, 1 To 2)
  With Range("H385:I" & Range("H" & Rows.Count).End(xlUp).Row)
    a = .Value
    For i = 1 To UBound(a)
      k = k + 1: b(k, 1) = a(i, 1)
      If Len(a(i, 2)) > 0 Then
        For Each itm In Split(a(i, 2), Chr(10))
          k = k + 1: b(k, 1) = itm
        Next itm
      End If
    Next i
    .Resize(k).Value = b
  End With
End Sub

Before:

MiniFav.xlsm
HI
385000-28-210-700-A01
386000-28-520-711-A0100-28-620-711-A01
387000-28-520-711-A0100-28-620-711-A01
388000-28-520-712-A0100-28-620-712-A01
389000-28-520-712-A0100-28-620-712-A01
390000-28-520-713-A0100-28-520-713-A02 000-28-620-713-A01 000-28-620-713-A02
391
Sheet1


After:

MiniFav.xlsm
HI
385000-28-210-700-A01
386000-28-520-711-A01
38700-28-620-711-A01
388000-28-520-711-A01
38900-28-620-711-A01
390000-28-520-712-A01
39100-28-620-712-A01
392000-28-520-712-A01
39300-28-620-712-A01
394000-28-520-713-A01
39500-28-520-713-A02
396000-28-620-713-A01
397000-28-620-713-A02
398
Sheet1
 
Upvote 0
Looking at the question again, this may be more what is wanted? (Again test with a copy)

VBA Code:
Sub Test_v2()
  Dim Bits As Variant
  Dim r As Long, rws As Long
  
  Const TopRow As Long = 385 '<- Edit starting row as required
  
  Application.ScreenUpdating = False
  For r = Range("I" & Rows.Count).End(xlUp).Row To TopRow Step -1
    Bits = Split(Range("I" & r).Value, Chr(10))
    rws = UBound(Bits) + 1
    If rws > 0 Then
      Rows(r + 1).Resize(rws).Insert
      Range("H" & r + 1).Resize(rws).Value = Application.Transpose(Bits)
      Range("I" & r).ClearContents
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Looking at the question again, this may be more what is wanted? (Again test with a copy)

VBA Code:
Sub Test_v2()
  Dim Bits As Variant
  Dim r As Long, rws As Long
 
  Const TopRow As Long = 385 '<- Edit starting row as required
 
  Application.ScreenUpdating = False
  For r = Range("I" & Rows.Count).End(xlUp).Row To TopRow Step -1
    Bits = Split(Range("I" & r).Value, Chr(10))
    rws = UBound(Bits) + 1
    If rws > 0 Then
      Rows(r + 1).Resize(rws).Insert
      Range("H" & r + 1).Resize(rws).Value = Application.Transpose(Bits)
      Range("I" & r).ClearContents
    End If
  Next r
  Application.ScreenUpdating = True
End Sub
Thank you, this worked exactly as I had hoped for.
Can i just ask if this is a standard code? as in if I search for each bit I will be able to teach myself how each part works for future use?
 
Upvote 0

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