Copy and Insert Row Based on Cell Time Value - excel

My main problem is that I am trying to add a row directly beneath another row based on the time value of that row. Here's an example of what I'm trying to do:
column F ========> new column F
2 1
2
2 1
2
1 1
1 1
2 1
2
To better explain, if the value in the first column F is a 2, that represents a time value that is greater than 0:59:00 and another row is added beneath it. If it is a 1, then it represents a time value that is equal to or less than 0:59:00and no row gets added.
I have multiple coding attempts at fixing this, and this first one is by someone more well-versed in VBA than I and includes some of his comments:
Public Sub ExpandRecords()
Dim i As Long, _
j As Long, _
LR As Long
'set variable types
LR = Range("A" & Rows.Count).End(xlUp).Row
'setting variable LR as number of rows with data
Application.ScreenUpdating = False
Columns("F:F").NumberFormat = "hh:mm:ss"
'sets number format in column b to text
For i = LR To 1 Step -1
'Executes following code from last row with data to row 1 working backwards
'If CLng(Left(Range("F" & i).Value, Len(Range("F" & i).Value) - 6)) > 0 Then
If CLng(Hour(Range("F" & i))) > 0 Then
'If the hour value in column F is greater than 1, then...
With Range("F" & i)
'starting with column F, loop through these statements...
'.Offset(1, 0).Resize(CLng(Left(Range("F" & i).Value, Len(Range("F" & i).Value) - 6)) - 1, 1).EntireRow.Insert Shift:=xlDown
.Offset(1, 0).Resize(CLng(Hour(Range("F" & i))).Value, Len(Range("F" & i).Value) - 1, 1).EntireRow.Insert Shift:=xlDown
'return the value of column F's hour value, change the range to insert the number of rows below based on hour value
'.Resize(CLng(Left(Range("F" & i).Value, Len(Range("F" & i).Value) - 6)), 1).EntireRow.Value = Range("A" & i).EntireRow.Value
.Resize(Hour(Range("F" & i)), 1).EntireRow.Value = Range("A" & i).EntireRow.Value
'Get value of row to be copied
'For j = 0 To CLng(Left(Range("F" & i).Value, Len(Range("F" & i).Value) - 6))
For j = 0 To Hour(Range("F" & i))
Range("H" & i).Offset(j - 1, 0).Value = Application.Text(j, "0")
Next j
End With
Else
Range("H" & i).Value = Application.Text(1, "0")
End If
Next i
Application.ScreenUpdating = True
End Sub
Here is a similar question from a previous user
Any help would be greatly appreciated.

Use this instead:
Public Sub ExpandRecords()
Dim i As Long, s As String
Const COL = "F"
For i = 1 To Cells(Rows.Count, COL).End(xlUp).Row
If Cells(i, COL) = 2 Then s = s & "," & Cells(i, COL).Address
Next
If Len(s) Then
Range(Mid$(s, 2)).EntireRow.Insert
For i = 1 To Cells(Rows.Count, COL).End(xlUp).Row
If Cells(i, COL) = vbNullString Then Cells(i, COL) = 1
Next
End If
End Sub

Related

To Get only one raw of result for a particular Range

Following code is suggested by a helpful user, this works well to Calculate "From", "To", "MAX" etc values of a range. But this code gives results in every row of a range. I want to get the results in only first row of each row. Please help with this.
Sub trial()
Dim lastrow As Long
Dim destSht As Worksheet
Dim i As Long, j As Long
Set destSht = Worksheets("Final")
With Worksheets("Source")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 2 To lastrow + 1 'loop whole range (column C)
If .Cells(i, "C") <> "" Then 'If column C is not empty then
For j = i To lastrow + 1 'Loop "group" range to find next empty cell. Start from current loop i to last row and add one row to get to next empty cell.
If .Cells(j, "C") = "" Then 'When next empty cell is found (i.e. end of small group range) then apply formulas
.Cells(i, "E").Value = .Cells(i, "B").Value 'From
.Cells(i, "F").Value = .Cells(j - 1, "B").Value 'To
.Cells(i, "G").Formula = "=MAX(C" & i & ":C" & j - 1 & ")" 'MAX
.Cells(i, "H").Formula = "=MIN(C" & i & ":C" & j - 1 & ")" 'MIN
.Cells(i, "I").Formula = "=AVERAGE(C" & i & ":C" & j - 1 & ")" 'AVG
Exit For
End If
Next j
End If
Next I
End With
End Sub
This Code gives following result
Desired Result
Try changing this line:
If .Cells(i, "C") <> "" Then 'If column C is not empty then
To this line:
If .Cells(i, "C") <> "" AND .Cells(i-1, "C") = "" Then 'If column C is not empty AND the column C above is empty then

Excel - VBA - Concatenate - Multiple Cells in a column

I have a code to concatenate three columns in a difference sheet for one cell -
Sheets("Sheet2").Range("D2") = Sheets("Sheet2").Range("A2") &
Sheets("Sheet2").Range("B2") & Sheets("Sheet2").Range("C2")
I want to repeat this cell for n number of time in the excel sheet - not able to do that
Let's suppose that you have stored your numbers in columns A, B, C.
First, use this code to determine last row (1 stands for A column):
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Then use For loop to achieve what you after (1 stands for A column, 2 - B, 3 - C, D - 4):
For i = 1 To lastRow
Cells(i, 4).Value = Cells(i, 1).Value + Cells(i, 2).Value + Cells(i, 3).Value
Next
In this routine n is the last row you want to be filled:
Sub KonKat()
Dim n As Long, i As Long
n = 5
With Sheets("Sheet2")
For i = 2 To n
.Range("D" & i).Value = .Range("A" & i).Value & .Range("B" & i).Value & .Range("C" & i).Value
Next i
End With
End Sub

VBA - Insert Merged Row in between gaps in data

I currently have a macro that inserts 3 rows when the value in Column E changes (Course Department). In the 3 rows I am trying to merge the middle row and add the department into this row. I can't work out how to get it to merge, any help would be appreciated.
With Range("e" & myHeader + 2, Range("e" & Rows.Count).End(xlUp)).Offset(, 1)
.Formula = _
"=if(and(r[-1]c[-1]<>"""",rc[-1]<>"""",r[-1]c[-1]<>rc[-1])," & _
"if(r[-1]c=1,""a"",1),"""")"
.Value = .Value
On Error Resume Next
For i = 1 To 3
.SpecialCells(2, 1).EntireRow.Insert
.SpecialCells(2, 2).EntireRow.Insert
Next
This is how it is currently:
This is what I would like to have:
When inserting or deleting rows, work from the bottom up. Some simple offsets and resizing should be sufficient to insert the three rows, merge the cells and transfer the values.
Option Explicit
Sub insertDept3()
Dim i As Long
With Worksheets("sheet10")
For i = .Cells(.Rows.Count, "E").End(xlUp).Row - 1 To 1 Step -1
If .Cells(i, "E").Value <> .Cells(i + 1, "E").Value Or i = 1 Then
.Cells(i + 1, "A").Resize(3, 5).Insert shift:=xlDown
.Cells(i + 2, "A").Resize(1, 5).Merge
.Cells(i + 2, "A") = .Cells(i + 4, "E").Value
End If
Next i
End With
End Sub
I will leave the cell alignment and font formatting to you.
The below code loop column E, import three lines when the value change, merger Column A to column E , import and format value in the middle line.
Try:
Option Explicit
Sub test()
Dim i As Long, Lastrow As Long
Dim Department As String, NextDepartment As String
With ThisWorkbook.Worksheets("Sheet1")
Lastrow = .Cells(.Rows.Count, "E").End(xlUp).Row
For i = Lastrow To 2 Step -1
Department = .Range("E" & i).Value
NextDepartment = .Range("E" & i).Offset(-1, 0).Value
If Department <> NextDepartment Then
.Rows(i).EntireRow.Resize(3).Insert
.Range("A" & i + 1 & ":E" & i + 1).Merge
With .Range("A" & i + 1)
.Value = Department
.Font.Bold = True
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
End With
End If
Next i
End With
Output:

Combine data from different rows base on column value

I have a excel file that contain daily order record and I need to summarize the order detail base on different staff.
I would like to combine the rows base on same Staff ID and divide to different group.
All order with the same Staff ID(Column B) will divide to a same group, the quantity of item A to item X of the order within a group will sum individual, each group will retain the first order record only and the other order id(Column A) within the group will mark in the remark column(Column G).
I have a macro with many for-loop & if statement to finish the task, but I don't have any idea how to simplify or modified it. Could someone can give me some suggestion?
Please let me know if I can clarify anything for you.
Private Sub test()
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
'sum the quantity of item
For Z = 2 To lastrow
If Range("F" & Z) <> "" Then
ordercount = 2
For c = 2 To lastrow
If Z <> c Then
If Range("F" & Z) = Range("F" & c) Then
For i = 0 To 9
temp = Cells(Z, 3 + i) + Cells(c, 3 + i)
If temp <> 0 Then
Cells(Z, 3 + i) = temp
End If
Next i
Range("G" & Z) = Range("G" & Z).Value & "No." & ordercount
& " " & Range("A" & c).Value & Chr(10)
ordercount = ordercount + 1
End If
End If
Next c
End If
orderno = Range("G" & Z).Value
If orderno <> "" Then
Range("G" & Z) = Left(orderno, Len(orderno) - 1)
End If
Next Z
'delete the other record within the same group
For Z = 2 To lastrow
If Range("F" & Z) <> "" Then
For c = 2 To lastrow
If Z <> c Then
If Range("F" & Z) = Range("F" & c) Then
Rows(c).Delete
c = c - 1
End If
End If
Next c
End If
Next Z
End Sub
Sample:
The following will do what you expect, but the loop starts from the bottom of the data and moves to the second row, as when deleting rows it is advisable to work upwards, so as not to miss the comparison against any row when deleting rows:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'get the last row with data on Column A
For i = LastRow To 2 Step -1 'loop from last row to second (missing headers)
CheckID = ws.Cells(i, 2) 'get the Staff ID to check
For x = (i - 1) To 2 Step -1 'second loop for comparison
If ws.Cells(x, 2) = CheckID Then 'if ID's match
ws.Cells(i, 3) = ws.Cells(i, 3) + ws.Cells(x, 3) 'add values for Item A
ws.Cells(i, 4) = ws.Cells(i, 4) + ws.Cells(x, 4) 'add values for Item B
ws.Cells(i, 5) = ws.Cells(i, 5) + ws.Cells(x, 5) 'add values for Item C
ws.Cells(i, 6) = ws.Cells(i, 6) 'get the Group Number
ws.Cells(i, 7) = ws.Cells(i, 7) & Chr(10) & ws.Cells(x, 1) 'Add remark
ws.Rows(x).Delete Shift:=xlUp 'delete row
i = i - 1 'adjust counter
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'get new last row
End If
Next x
Next i
End Sub

Delete rows if multiple cells have zero

I have multiple Excel workbooks that contain about 8,000 rows so it would be nice to use a macro.
Basically, if any row has a zero (0) in all columns (at the same time) B, D, E, I, J, and K it will delete.
Here is what I have so far...way too new with VB to figure out.
Sub DeleteRowsZeros()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
If (Range("B") = "0" And Range("D" & i) = "0" And Range("E" & i) = "0" And Range("I" & i) = "0" _
And Range("J" & i) = "0" And Range("K" & i) = "0") Then Rows(i).Delete
Next i
End Sub
Try this :
Sub DeleteRowsZeros()
Dim cell As Range, notZeroColumns As Range, row As Range
Set row = Range("A" & Rows.Count).End(xlUp).EntireRow.Offset(1, 0)
Set notZeroColumns = Range("B:B,D:E,I:k")
While row.row <> 1
Set row = row.Offset(-1, 0)
For Each cell In Intersect(row, notZeroColumns)
If cell.Text <> "0" Then GoTo continueLbl
Next
row.Offset(1, 0).Delete
continueLbl:
Wend
End Sub
EDIT : bugfixe

Resources