Results 1 to 10 of 10

Insert rows based on start/end values

Threaded View

  1. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Insert rows based on start/end values

    Here is a code that should work. At least it does with the sample date you supplied. Also, look at attached file.
    
    Option Explicit
    Public User_no(10, 2) As Variant
    Public User_Start As Integer
    Public User_End As Integer
    
    Public Sub test()
    Dim U_ser As String, C_ell As Range, i As Integer, M_onth As Integer, D_ay As Integer
    Dim First_row As Boolean, Current_hour As Integer, Last_row As Boolean, First_time As Boolean
    'store Users values in array
    Worksheets("Users").Select
    i = 0
    For Each C_ell In Range("A2", Cells(Rows.Count, 1).End(xlUp))
        User_no(i, 0) = C_ell
        User_no(i, 1) = C_ell.Offset(0, 1)
        User_no(i, 2) = C_ell.Offset(0, 2)
        i = i + 1
    Next
    
    'Scan whole sheet1 to insert rows where appropriate
    Worksheets("Sheet1").Select
    First_row = True
    Last_row = False
    First_time = True 'Variable only there to get through first row when hour equal start time
                      'otherwise you get an error because of the -1 offset
    For Each C_ell In Range("A2", Cells(Rows.Count, 1).End(xlUp))
        'Assign value to variables
        U_ser = C_ell.Offset(0, 3)
        M_onth = C_ell.Offset(0, 4)
        D_ay = C_ell.Offset(0, 5)
        
        'check if NEXT row is a new entry (User, Month or day)
        If U_ser <> C_ell.Offset(1, 3) Or M_onth <> C_ell.Offset(1, 4) Or D_ay <> C_ell.Offset(1, 5) Then
            Last_row = True
        End If
            If First_row = True Then
                F_user (C_ell.Offset(0, 3)) 'Get start and end time for the user
            End If
            Current_hour = C_ell.Offset(0, 6) 'This is the hour of current row
            
            If First_row = True Then
                If Current_hour > User_Start And Current_hour <= User_End Then
                    For i = User_Start To Current_hour - 1
                      Rows(C_ell.Row).Select
                      Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                      C_ell.Offset(-1, 3) = U_ser
                      C_ell.Offset(-1, 4) = C_ell.Offset(0, 4)
                      C_ell.Offset(-1, 5) = C_ell.Offset(0, 5)
                      C_ell.Offset(-1, 6) = i
                      C_ell.Offset(-1, 7) = 0
                    Next i
                    First_row = False
                ElseIf Current_hour = User_Start Then
                  First_row = False
                End If
            End If
            
            If Last_row = True Then
                If Current_hour < User_End Then
                    For i = User_End To Current_hour + 1 Step -1
                      Rows(C_ell.Row + 1).Select
                      Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                      C_ell.Offset(1, 3) = U_ser
                      C_ell.Offset(1, 4) = C_ell.Offset(0, 4)
                      C_ell.Offset(1, 5) = C_ell.Offset(0, 5)
                      C_ell.Offset(1, 6) = i
                      C_ell.Offset(1, 7) = 0
                    Next i
                    Last_row = False
                Else
                    Last_row = False
                    First_row = True
                End If
            ElseIf First_time = False Then
              If (Current_hour > C_ell.Offset(-1, 6) + 1) And Current_hour <= User_End Then
                For i = C_ell.Offset(-1, 6) + 1 To Current_hour - 1
                    Rows(C_ell.Row).Select
                    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                    C_ell.Offset(-1, 3) = U_ser
                    C_ell.Offset(-1, 4) = C_ell.Offset(0, 4)
                    C_ell.Offset(-1, 5) = C_ell.Offset(0, 5)
                    C_ell.Offset(-1, 6) = i
                    C_ell.Offset(-1, 7) = 0
                Next i
              End If
            End If
            First_time = False
    Next
    End Sub
    
    Public Function F_user(User_to_F As String) As String
    Dim I_1 As Long
    For I_1 = 0 To UBound(User_no())
        If User_to_F = User_no(I_1, 0) Then
            User_Start = User_no(I_1, 1)
            User_End = User_no(I_1, 2)
            Exit For
        End If
    Next I_1
    
    End Function
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1