Create a vba code to insert row based on two-column condition

Ganeshan

New Member
Joined
Dec 15, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi Everyone,
I am new to this forum and have been trying to learn some VBA coding to automate some data. My question is -

I would like to insert a row when there is a value change in two-column cells in an excel
For example, columns F and G where F has a string value of "Buy" and "Sell" and column G has string "D" and "S".

My code should add a row when the column is sorted the last value is
"Buy" - "D"
"Buy" - "S"
"Sell" - "D"
"Sell" - "S"
 

Attachments

  • Screen Shot 2021-12-15 at 2.33.08 PM.png
    Screen Shot 2021-12-15 at 2.33.08 PM.png
    201.2 KB · Views: 28
Try this where rows are only shifted down at the end of the process (if there are any) and no helper column is used:

VBA Code:
Option Explicit
Sub Macro1()

    Dim TimeArray As Variant
    Dim i As Long
    Dim TimeCheck As String
    Dim rng As Range
  
    Application.ScreenUpdating = False
  
    TimeArray = Array("8:00", "8:20", "9:10", "10:00", "10:20", "11:10", "12:05", "12:30", "13:00", "13:50", "14:40", "15:00", "15:50")
  
    For i = 4 To 15
        If Len(Cells(i, "A")) > 0 And InStr(Cells(i, "A"), Chr(10)) > 0 Then
            TimeCheck = Split(Cells(i, "A"), Chr(10))(0)
            If IsNumeric(Application.Match(TimeCheck, TimeArray, 0)) = False Then
                If rng Is Nothing Then
                    Set rng = Cells(i, "A")
                Else
                    Set rng = Union(rng, Cells(i, "A"))
                End If
            End If
        End If
    Next i
  
    If Not rng Is Nothing Then
        rng.EntireRow.Delete Shift:=xlDown
    End If
  
    Application.ScreenUpdating = False

End Sub
Thanks, Trebor! I gave it a try, but nothing happened. (I removed the ScreenUpdating function, too, so that's not the problem.) I ran it with the rest of the code (for the other formatting things that happen before it), and I ran it on its own, and it gave the same result. (The other formatting things did happen, but the insert rows did not happen.)

I tried also changing the rng.EntireRow element to .Insert (instead of .Delete), but that didn't do anything either.

I'm not familiar (yet) with the IsNumeric command, but I suppose from looking at it that it's checking to see if the variables are ...well...numeric. Would the fact that the elements in the array are strings (with a : in them) be interfering with this?

I'm also wondering if having the "8:00" element in the array is problematic. The times start in A3 with 8:00 (sometimes shown as 8:05), but it's irrelevant because no row ever needs to be inserted before A3. Thus it can start checking in A4 with the time 8:20.

I just realised as well that I never posted the data from my sheet. Of course, it's slightly different for each person's timetable, so I'll just give two examples. But hopefully this will help visualise what's going on in the sheet itself.

First, here's a "full" timetable (when no rows need to be inserted):
21-22 January_3112_6.4.3.3001_Leerkrachten TEST.xlsx
ABCDEF
38:00 8:20
48:20 9:10FRA 5 HUM 5 LAT-MT 6 LAT-MTMEETING MIS
59:10 10:00FRA 3 HUM 3 LAT
610:00 10:20TOEZ. pauze
710:20 11:10FRA 3 HUM 3 LATFRA 3 HUM 3 LAT
811:10 12:00
912:05 12:30MEETING Lunch
1012:30 13:00
1113:00 13:50FRA 4 HUM 4 LATFRA 4 HUM 4 LAT
1213:50 14:40
1314:40 15:00TOEZ. pauze
1415:00 15:50FRA 5 HUM 5 LAT-MT 6 LAT-MT
1515:50 16:40
BTH


And here's one that has missing rows. In this example, a row would need to be inserted before rows 6, 10, and 12. (This one also gives an example of 8:05 in A3, but again, it's irrelevant.)
21-22 January_3112_6.4.3.3001_Leerkrachten TEST.xlsx
ABCDEF
38:05 8:20TOEZ. pauzeTOEZ. pauze
48:20 9:10AARD 3 HUM 3 LATFYS 4 HUM 4 LATMEETING MIS
59:10 10:20
610:20 11:10AARD 4 HUM 4 LAT
711:10 12:00AARD 6 LAT-MT
812:05 12:30MEETING Lunch
912:30 13:50
1013:50 14:40CHM 3 HUM 3 LATAARD 5 HUM 5 LAT-MT
1114:40 15:50
1215:50 16:40FYS 3 HUM 3 LATCHM 4 HUM 4 LAT
DHE


Thanks again! I really appreciate your help!
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this where the entry in Col. A is matched to the array position:

VBA Code:
Option Explicit
Sub Macro1()

    Dim strTimes() As String
    Dim i As Long, j As Long
    Dim TimeCheck As String
    Dim rng As Range
  
    Application.ScreenUpdating = False
  
    strTimes = Split("8:00,8:20,9:10,10:00,10:20,11:10,12:05,12:30,13:00,13:50,14:40,15:00,15:50", ",") 'Array should just probably start at 8:20??
    j = 1 'This would be zero if we were checking from the first item in the 'strTimes' array
  
    For i = 3 To 15 'Should just start at Row 4??
        If i > 3 And Len(Cells(i, "A")) > 0 Then
            If Left(Cells(i, "A"), 1) = "1" Or Left(Cells(i, "A"), 1) = "2" Then
                TimeCheck = Left(Cells(i, "A"), 5)
            Else
                TimeCheck = Left(Cells(i, "A"), 4)
            End If
            If TimeCheck <> strTimes(j) Then
                If rng Is Nothing Then
                    Set rng = Cells(i, "A")
                Else
                    Set rng = Union(rng, Cells(i, "A"))
                End If
                i = i - 1
            End If
            j = j + 1
        End If
    Next i
  
    If Not rng Is Nothing Then
        rng.EntireRow.Insert Shift:=xlDown
    End If
  
    Application.ScreenUpdating = True

End Sub

The code also inserted a Row before 12 as there is no 15:00 time stamp.

If this is still wrong please start a new thread with a link to this one if you think it will help provide a solution.

Thanks,

Robert
 
Upvote 0
Try this where the entry in Col. A is matched to the array position:

VBA Code:
Option Explicit
Sub Macro1()

    Dim strTimes() As String
    Dim i As Long, j As Long
    Dim TimeCheck As String
    Dim rng As Range
 
    Application.ScreenUpdating = False
 
    strTimes = Split("8:00,8:20,9:10,10:00,10:20,11:10,12:05,12:30,13:00,13:50,14:40,15:00,15:50", ",") 'Array should just probably start at 8:20??
    j = 1 'This would be zero if we were checking from the first item in the 'strTimes' array
 
    For i = 3 To 15 'Should just start at Row 4??
        If i > 3 And Len(Cells(i, "A")) > 0 Then
            If Left(Cells(i, "A"), 1) = "1" Or Left(Cells(i, "A"), 1) = "2" Then
                TimeCheck = Left(Cells(i, "A"), 5)
            Else
                TimeCheck = Left(Cells(i, "A"), 4)
            End If
            If TimeCheck <> strTimes(j) Then
                If rng Is Nothing Then
                    Set rng = Cells(i, "A")
                Else
                    Set rng = Union(rng, Cells(i, "A"))
                End If
                i = i - 1
            End If
            j = j + 1
        End If
    Next i
 
    If Not rng Is Nothing Then
        rng.EntireRow.Insert Shift:=xlDown
    End If
 
    Application.ScreenUpdating = True

End Sub

The code also inserted a Row before 12 as there is no 15:00 time stamp.

If this is still wrong please start a new thread with a link to this one if you think it will help provide a solution.

Thanks,

Robert
Thanks, Robert! It's very close--it works fine generally, but it doesn't insert two rows next to each other when needed. I started a new thread here, as you suggested.
 
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