VBA For Loop populating last entry until end - excel

first post and also new to VBA so I apologize for anything that is unclear. I have created a code to generate a daily printout of employees, equipment, and subcontractors. The loop is looking for "S" (subcontractors) each day. There is only one day where "S" is present and there are 4 on that day. The issue is that the loop begins correctly and populates the correct information when it finds "S" and lists the 4 separate subcontractors, but every day before and after that it continues to list the first subcontractor even though no "S" is found on those dates. How can I get it to clear that entry if no other "S" are found? I hope that makes sense and I have included the code. Thank you!
Screesnhot
Sub WriteReport_Click()
Dim EachName(1 To 5000) As Variant
Dim NameHours(1 To 5000) As Variant
Dim NamePhase(1 To 5000) As Variant
Dim EquipHours(1 To 5000) As Variant
Dim EquipPhase(1 To 5000) As Variant
Dim EachDate(1 To 5000) As Date
Dim EachEquip(1 To 5000) As Variant
Dim EachSub(1 To 5000) As Variant
Dim SubAmount(1 To 5000) As Variant
Dim i As Long 'loop through records
Dim k As Integer 'count employees
Dim h As Integer 'count equipment
Dim t As Integer 'count subcontractor
Dim m As Integer 'count dates
Dim j As Integer
Dim x As Integer
Dim lr, s, p, StartBorder, EndBorder As Integer 'keeps row counts Start & Finish
Dim TestString As String
Sheets("Data").Activate
k = 1 'counts EachName
h = 1 'counts EachEquip
t = 1 'counts EachSub
m = 1 'counts dates
lr = 1
p = 0
For i = 1 To Rows.Count
If Cells(i, 3) = "L" Then
EachName(1) = Cells(i, 11)
Exit For
End If
Next i
For i = 1 To Rows.Count
If Cells(i, 3) = "E" Then
EachEquip(1) = Cells(i, 12)
Exit For
End If
Next i
For i = 1 To Rows.Count
If Cells(i, 3) = "S" Then
EachSub(1) = Cells(i, 9)
Exit For
End If
Next i
NameHours(1) = 0
EquipHours(1) = 0
EachDate(1) = Cells(1, 1)
SubAmount(1) = 0
Dim LastRow As Integer
For i = 1 To 5000
If EachDate(m) <> Cells(i, 1) Then
m = m + 1 'setting array for next new date
EachDate(m) = Cells(i, 1)
lr = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row + 1
StartBorder = lr
Sheets("Report").Cells(lr, 1) = Format(EachDate(m - 1), "mm/dd/yy") 'prints date
Sheets("Report").Cells(lr, 1).Interior.ColorIndex = 4 'highlights date
For j = 1 To k 'prints employees, hours and phase
Sheets("Report").Cells((lr + j), 1) = EachName(j)
Sheets("Report").Cells((lr + j), 2) = NameHours(j)
Sheets("Report").Cells((lr + j), 4) = NamePhase(j)
Sheets("Report").Cells((lr + j), 5).Formula = _
"=IF(A" & CStr(lr + j) & "<>"""",VLOOKUP(A" & CStr(lr + j) & ",Employee,2,FALSE),"""")"
Next j
k = 1
lr = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row + 1
For s = i To 5000 'getting first employee for next date
If Cells(s, 1) = EachDate(m) And Cells(s, 3) = "L" Then
EachName(1) = Cells(s, 11)
Exit For
End If
Next s
Erase NameHours 'clearing manhours for next date
For j = 1 To h
Sheets("Report").Cells((lr + j), 1) = Trim(EachEquip(j))
Sheets("Report").Cells((lr + j), 3) = EquipHours(j)
Sheets("Report").Cells((lr + j), 4) = EquipPhase(j)
Sheets("Report").Cells((lr + j), 5).Formula = _
"=LEFT(IF(A" & CStr(lr + j) & "<>"""",VLOOKUP(A" & CStr(lr + j) & ",EquipList,2,FALSE),""""),20)"
Next j
h = 1
For s = i To 5000 'getting first equipment for next date
If Cells(s, 1) = EachDate(m) And Cells(s, 3) = "E" Then
EachEquip(1) = Cells(s, 12)
Exit For
End If
Next s
Erase EquipHours ' clearing equipment hours for next date
lr = Sheets("Report").Cells(Rows.Count, 1).End(xlUp).Row + 1
For x = 1 To t
Sheets("Report").Cells((lr + x), 1) = EachSub(x)
Sheets("Report").Cells((lr + x), 3) = SubAmount(x)
Next x
For x = i To 5000 'getting subcontractor for next date
If Cells(x, 1) = EachSub(m) And Cells(x, 3) = "S" Then
EachSub(1) = " "
Exit For
End If
Next x
EndBorder = lr + x
t = 1
With Worksheets("Report") 'draws borders
.Range(.Cells(StartBorder, 1), .Cells(EndBorder, 8)).BorderAround ColorIndex:=1, Weight:=xlThick
End With
End If
Select Case Cells(i, 3).Value
Case "L"
If Cells(i, 11) = EachName(k) Then
If Cells(i, 7) = 0 Then
p = p + 1 'adding up per diem
End If
NamePhase(k) = Cells(i, 2)
NameHours(k) = NameHours(k) + Cells(i, 7)
Else
k = k + 1
EachName(k) = Cells(i, 11)
NamePhase(k) = Cells(i, 2)
If Cells(i, 7) = 0 Then
p = p + 1
End If
NameHours(k) = NameHours(k) + Cells(i, 7)
End If
Case "E"
If Cells(i, 12) = EachEquip(h) Then
EquipPhase(h) = Cells(i, 2)
EquipHours(h) = EquipHours(h) + Cells(i, 7)
Else
h = h + 1
EachEquip(h) = Cells(i, 12)
EquipPhase(h) = Cells(i, 2)
EquipHours(h) = EquipHours(h) + Cells(i, 7)
End If
Case "S"
If Cells(i, 9) = EachSub(t) Then
EachSub(t) = Cells(i, 9)
SubAmount(t) = SubAmount(t) + Cells(i, 8)
Else
t = t + 1
EachSub(t) = Cells(i, 9)
SubAmount(t) = SubAmount(t) + Cells(i, 8)
End If
End Select
Next i
MsgBox "Report Completed !!!"
End Sub

You will find your code easier to debug/maintain if you separate the collection of the data and the report generation into 2 discrete steps, preferably in subroutines. For example
Option Explicit
Dim EachName(0 To 5000, 1 To 3) As Variant '1=name 2=hours 3=phase
Dim EachEquip(0 To 5000, 1 To 3) As Variant '1=name 2=hrs 3=phase
Dim EachSub(0 To 5000, 1 To 2) As Variant ' 1=name 2=amount
Dim k As Long 'count employees
Dim h As Long 'count equipment
Dim t As Long 'count subcontractor
Sub WriteReport_Click()
' specify book and sheets to process
Dim wb As Workbook, wsData As Worksheet, wsRep As Worksheet
Set wb = ThisWorkbook ' or ActiveWorkBook
' determine extent of data
Dim LastRow As Long, iRow As Long
Set wsData = wb.Sheets("Data")
LastRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row
' clear report sheet
Set wsRep = wb.Sheets("Report")
wsRep.Cells.Clear
' scan data for first date
Dim RepDate As Date
RepDate = wsData.Cells(1, 1)
Call GetData(RepDate, wsData)
' scan data for more dates
For iRow = 1 To LastRow
If wsData.Cells(iRow, 1) <> RepDate Then
' report existing date
Call ReportData(RepDate, wsRep)
' get data for new date
RepDate = wsData.Cells(iRow, 1)
Call GetData(RepDate, wsData)
End If
Next
' report last date
Call ReportData(RepDate, wsRep)
'end
wsRep.Columns("A:E").AutoFit
MsgBox "Report Completed", vbInformation, LastRow & " rows scanned"
End Sub
Sub ReportData(d As Date, ws As Worksheet)
Debug.Print "ReportData", d
Dim lr As Long, StartBorder As Long, EndBorder As Long, j As Long
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
StartBorder = lr
ws.Cells(lr, 1) = Format(d, "mm/dd/yy") 'prints date
ws.Cells(lr, 1).Interior.ColorIndex = 4 'highlights date
'prints employees, hours and phase
For j = 1 To k
ws.Cells((lr + j), 1) = EachName(j, 1) 'empoyee name
ws.Cells((lr + j), 2) = EachName(j, 2) 'hrs
ws.Cells((lr + j), 4) = EachName(j, 3) 'phase
ws.Cells((lr + j), 5).Formula = _
"=IF(A" & CStr(lr + j) & "<>"""",VLOOKUP(A" & CStr(lr + j) & ",Employee,2,FALSE),"""")"
Next j
' report equipment
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
For j = 1 To h
ws.Cells((lr + j), 1) = EachEquip(j, 1) 'equip name
ws.Cells((lr + j), 3) = EachEquip(j, 2) 'hours
ws.Cells((lr + j), 4) = EachEquip(j, 3) 'phase
ws.Cells((lr + j), 5).Formula = _
"=LEFT(IF(A" & CStr(lr + j) & "<>"""",VLOOKUP(A" & CStr(lr + j) & ",EquipList,2,FALSE),""""),20)"
Next j
' report sub contractors
lr = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
For j = 1 To t
ws.Cells((lr + j), 1) = EachSub(j, 1) 'sub name
ws.Cells((lr + j), 3) = EachSub(j, 2) 'amount
Next j
' draws borders
EndBorder = ws.Cells(Rows.Count, 1).End(xlUp).Row
ws.Range(ws.Cells(StartBorder, 1), ws.Cells(EndBorder, 8)) _
.BorderAround ColorIndex:=1, Weight:=xlThick
End Sub
Sub GetData(d As Date, ws As Worksheet)
Debug.Print "GetData", d
Dim LastRow As Long, i As Long
LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
' clear global arrays
Erase EachName
Erase EachEquip
Erase EachSub
k = 0: h = 0: t = 0
For i = 1 To LastRow
If ws.Cells(i, 1) = d Then
Select Case ws.Cells(i, 3)
Case "L" ' Employee
If ws.Cells(i, 11) <> EachName(k, 1) Then
k = k + 1
End If
EachName(k, 1) = ws.Cells(i, 11)
EachName(k, 2) = ws.Cells(i, 7) + EachName(k, 2) ' hours
EachName(k, 3) = ws.Cells(i, 2) ' phase
Case "E" ' Equipment
If ws.Cells(i, 12) <> EachEquip(h, 1) Then
h = h + 1
End If
EachEquip(h, 1) = Trim(ws.Cells(i, 12)) ' equip name
EachEquip(h, 2) = ws.Cells(i, 7) + EachEquip(h, 2) ' hours
EachEquip(h, 3) = ws.Cells(i, 2) ' phase
Case "S" ' Subcontractor
If ws.Cells(i, 9) <> EachSub(t, 1) Then
t = t + 1
End If
EachSub(t, 1) = ws.Cells(i, 9) ' sub name
EachSub(t, 2) = ws.Cells(i, 8) + EachSub(t, 2) ' amount
Case Else
MsgBox "Unknown code at row " & i, vbExclamation
End Select
End If
Next
End Sub

Related

Match rows for value in specific columns and paste matched/unmatched rows in new sheet

I got data in sheet1 and sheet2, which i want to copy and paste in sheet3. That is already done. So next i want to match rows, by checking column C, D, E, H and I. The C and H column value is integer and the rest is text/strings.
If two rows match, then i want to copy and paste one of the lines in a new third sheet, and add the integer difference from column H in column H (The difference will be 0 if the lines match in all columns)
If the two rows dont match, copy and paste one of the lines in a new fourth sheet, and add the integer difference from column H in column H
The code so far:
Sub CopyPasteSheet()
Dim mySheet, arr
arr = Array("Sheet1", "Sheet2")
Const targetSheet = "Sheet3"
Application.ScreenUpdating = False
For Each mySheet In arr
Sheets(mySheet).Range("A1").CurrentRegion.Copy
With Sheets(targetSheet)
.Range("A1").Insert Shift:=xlDown
If mySheet <> arr(UBound(arr)) Then .Rows(1).Delete xlUp
End With
Next mySheet
Application.ScreenUpdating = True
End Sub
Code so far, but i receive a code error "Application-defined or object-defined error". It does copy the rows which match into a new sheet and state the difference as 0 in column H, but it doesn't work for the ones that dont match.
Sub MatchRows()
Dim a As Variant, b As Variant, c As Variant, d As Variant
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim dic As Object, ky As String
Set dic = CreateObject("Scripting.Dictionary")
a = Sheets("Sheet1").Range("A1:I" & Sheets("Sheet1").Range("H" & Rows.Count).End(3).Row).Value
b = Sheets("Sheet2").Range("A1:I" & Sheets("Sheet2").Range("H" & Rows.Count).End(3).Row).Value
ReDim c(1 To UBound(a, 1), 1 To UBound(a, 2))
ReDim d(1 To UBound(a, 1), 1 To UBound(a, 2))
For i = 1 To UBound(b, 1)
ky = b(i, 3) & "|" & b(i, 4) & "|" & b(i, 5) & "|" & b(i, 9)
dic(ky) = i
Next
For i = 2 To UBound(a, 1)
ky = a(i, 3) & "|" & a(i, 4) & "|" & a(i, 5) & "|" & a(i, 9)
If dic.exists(ky) Then
j = dic(ky)
If a(i, 8) = b(j, 8) Then
k = k + 1
For n = 1 To UBound(a, 2)
c(k, n) = a(i, n)
Next
c(k, 8) = 0
Else
m = m + 1
For n = 1 To UBound(a, 2)
d(k, n) = a(i, n)
Next
d(k, 8) = a(i, 8) - b(j, 8)
End If
End If
Next
Sheets("Sheet3").Range("A" & Rows.Count).End(3)(2).Resize(k, UBound(a, 2)).Value = c
Sheets("Sheet4").Range("A" & Rows.Count).End(3)(2).Resize(m, UBound(a, 2)).Value = d
End Sub
Unless the performance is too slow remove the complexity of arrays by writing to the output sheets one line at a time.
Update - copy complete line
Option Explicit
Sub MatchRows2()
Dim dic As Object, key As String
Set dic = CreateObject("Scripting.Dictionary")
Dim wb As Workbook
Dim ws As Worksheet, ws3 As Worksheet, ws4 As Worksheet
Dim iLastRow As Long, s As String, diff As Long
Dim iRow3 As Long, iRow4 As Long, i As Long, t0 As Single
Dim rng As Range
t0 = Timer
s = "|"
Set wb = ThisWorkbook
' sheet 2
Set ws = wb.Sheets("Sheet2")
iLastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
For i = 2 To iLastRow
key = ws.Cells(i, "C") & s & ws.Cells(i, "D") _
& s & ws.Cells(i, "E") & s & ws.Cells(i, "I")
If dic.exists(key) Then
MsgBox "Duplicate key '" & key & "'", vbCritical, "Sheet2 Row " & i
Exit Sub
Else
dic.Add key, ws.Cells(i, "H")
End If
Next
Debug.Print dic.Count
' results
Set ws3 = wb.Sheets("Sheet3")
iRow3 = ws3.Cells(Rows.Count, "A").End(xlUp).Row
Set ws4 = wb.Sheets("Sheet4")
iRow4 = ws4.Cells(Rows.Count, "A").End(xlUp).Row
'sheet 1
Application.ScreenUpdating = False
Set ws = wb.Sheets("Sheet1")
iLastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
For i = 2 To iLastRow
key = ws.Cells(i, "C") & s & ws.Cells(i, "D") _
& s & ws.Cells(i, "E") & s & ws.Cells(i, "I")
If dic.exists(key) Then
diff = ws.Cells(i, "H") - dic(key)
If diff = 0 Then
iRow3 = iRow3 + 1
Set rng = ws3.Cells(iRow3, "A")
Else
iRow4 = iRow4 + 1
Set rng = ws4.Cells(iRow4, "A")
End If
ws.Rows(i).Copy rng
rng.Offset(0, 7).Value = diff ' col H
End If
Next
Application.ScreenUpdating = True
MsgBox "Done in " & Format(Timer - t0, "0.0 secs"), vbInformation
End Sub

Combine duplicate rows in a loop vba

I want to combine duplicate rows with the same A and C columns values and sum their cells values for the column B (by adding the value of the textbox2 from the duplicate to the original). My problem is about the condition of the "If" in the Loop. It doesn't consider those conditions when I have duplicates and just add a new row. Is there a better way to do this?
Private Sub CommandButton1_Enter()
ActiveSheet.Name = "Sheet1"
Dim lastrow As Long
With Sheets("Sheet2")
lastrow = .Cells(Rows.Count, "H").End(xlUp).Row
For x = lastrow To 3 Step -1
For y = 3 To lastrow
If .Cells(x, 1).Value = .Cells(y, 1).Value And .Cells(x, 3).Value = .Cells(y, 3).Value And x > y Then
.Cells(y, 8).Value = .Cells(y, 8).Value + TextBox2.Text
.Cells(y, 2).Value = .Cells(y, 2).Value + TextBox2.Text
.Rows(lastrow).EntireRow.Delete
Else
.Cells(lastrow + 1, 8).Value = TextBox2.Text
.Cells(lastrow + 1, 2).Value = TextBox2.Text
.Cells(lastrow + 1, 1).Value = TextBox1.Text
.Cells(lastrow + 1, 3).Value = TextBox3.Text
Exit For
End If
Next y
Next x
End With
End Sub
Here's a picture of the data
There's no blank cell in the column H (I changed the color of the font to make it invisible).
Create a primary key by joining the 2 columns with tilde ~ and use a Dictionary Object to locate duplicates.
Option Explicit
Private Sub CommandButton1_Click()
Dim wb As Workbook, ws As Worksheet
Dim iLastRow As Long, iRow As Long, iTarget As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet2")
iLastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
Dim dict As Object, sKey As String
Set dict = CreateObject("Scripting.Dictionary")
' build dictionary and
' consolidate any existing duplicates, scan up
For iRow = iLastRow To 3 Step -1
' create composite primary key
sKey = LCase(ws.Cells(iRow, 1).Value) & "~" & Format(ws.Cells(iRow, 3).Value, "yyyy-mm-dd")
If dict.exists(sKey) Then
iTarget = dict(sKey)
' summate and delete
ws.Cells(iTarget, 2) = ws.Cells(iTarget, 2) + ws.Cells(iRow, 2)
ws.Cells(iTarget, 8) = ws.Cells(iTarget, 8) + ws.Cells(iRow, 8)
ws.Rows(iRow).EntireRow.Delete
Else
dict(sKey) = iRow
End If
Next
' add new record from form using dictionary to locate any existing
iLastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
sKey = LCase(TextBox1.Text) & "~" & Format(DateValue(TextBox3.Text), "yyyy-mm-dd")
If dict.exists(sKey) Then
iTarget = dict(sKey)
ws.Cells(iTarget, 2) = ws.Cells(iTarget, 2) + TextBox2.Text
ws.Cells(iTarget, 8) = ws.Cells(iTarget, 8) + TextBox2.Text
Else
iTarget = iLastRow + 1
ws.Cells(iTarget, 1) = TextBox1.Text
ws.Cells(iTarget, 2) = TextBox2.Text
ws.Cells(iTarget, 3) = TextBox3.Text
ws.Cells(iTarget, 8) = TextBox2.Text
End If
End Sub

How to capture additional line from various worksheets

I was given an old excel file with macro that copies scores and bservations from all the worksheets in that workbook into the first worksheet. The worksheets were amended with additional comments fields and I have tried to update the macro so it also shows it on the the first worksheet but to no avail. Here is the code:
sno = 1
lastcol = Sheets(1).Range("iv8").End(xlToLeft).Column
resrow = 9
headers = Array("Registration information", "CASE DETAILS (GENERAL) SCREEN", "Sender INFORMATION", "Client SCREEN", "PRODUCT", "Price")
For i = 2 To Sheets.Count
observ = ""
observno = 1
resrow = resrow + 1
rescol = 10
lastrow = Sheets(i).Range("c65535").End(xlUp).Row
Sheets(1).Cells(resrow, 1) = sno
Sheets(1).Cells(resrow, 2) = Sheets(i).Range("d2")
Sheets(1).Cells(resrow, 4) = Sheets(i).Range("d9")
Sheets(1).Cells(resrow, 3) = Sheets(i).Range("d3")
Sheets(1).Cells(resrow, 5) = Sheets(i).Range("d4")
Sheets(1).Cells(resrow, 6) = Sheets(i).Range("d5")
Sheets(1).Cells(resrow, 7) = Sheets(i).Range("E9")
While rescol <= lastcol - 4
For j = 9 To lastrow
If Sheets(i).Cells(j, 3) <> "" Then
Sheets(1).Cells(resrow, rescol) = Sheets(i).Cells(j, 3)
If Sheets(i).Cells(j, 3) > 0 And j <> lastrow Then
observ = observ & observno & ". " & Sheets(i).Cells(j, 4) & vbCrLf
observno = observno + 1
End If
rescol = rescol + 1
End If
Next j
Wend
Sheets(1).Cells(resrow, 8) = observ
sno = sno + 1
Next i
End Sub
The new field in the worksheets is called comments and I tried to copy and amend this part
While rescol <= lastcol - 4
For j = 9 To lastrow
If Sheets(i).Cells(j, 3) <> "" Then
Sheets(1).Cells(resrow, rescol) = Sheets(i).Cells(j, 3)
If Sheets(i).Cells(j, 3) > 0 And j <> lastrow Then
commen = commen & commenno & ". " & Sheets(i).Cells(j, 4) & vbCrLf
commenno = commenno + 1
But to no avail.
Here are the columns in the main worksheet (row 9 in worksheet)
Here is the example of the other worksheets
Similar code for comments to that you have for observations.
Option Explicit
Sub Consolidate()
Dim sno As Long, lastrow As Long, lastcol As Long, resrow As Long, rescol As Long
Dim observno As Integer, commentno As Integer, i As Long, j As Long
Dim observ As String, comment As String
Dim ws As Worksheet, wsRes As Worksheet
Set wsRes = Sheets(1)
sno = 1
lastcol = wsRes.Range("iv8").End(xlToLeft).Column
Debug.Print lastcol
resrow = 9
' not sure what this is doing
'headers = Array("Registration information", "CASE DETAILS (GENERAL) SCREEN", "Sender INFORMATION", "Client SCREEN", "PRODUCT", "Price")
For i = 2 To Sheets.Count
observ = ""
comment = ""
observno = 1
commentno = 1
resrow = resrow + 1
rescol = 10
' determine last row by using Ctrl-Up from cell C1048576
lastrow = Sheets(i).Range("C" & Rows.Count).End(xlUp).Row
Set ws = Sheets(i)
' fill the current results columns1 to 6 from scorecard
With wsRes.Cells(resrow, 1)
.Offset(0, 0) = sno
.Offset(0, 1) = ws.Range("D2")
.Offset(0, 2) = ws.Range("D3")
.Offset(0, 3) = ws.Range("D9")
.Offset(0, 4) = ws.Range("D4")
.Offset(0, 5) = ws.Range("D5")
.Offset(0, 6) = ws.Range("E9")
.VerticalAlignment = xlTop
End With
' scan down scorecard sheet starting at row 9 (probably should be 11)
Dim qu As Integer
For j = 9 To lastrow
qu = rescol - 9
' skip the group heading between questions
If Trim(Sheets(i).Cells(j, 3)) <> "" Then
' transfer score to result sheet
' move to next col ready for next Qu
wsRes.Cells(resrow, rescol) = Sheets(i).Cells(j, 3)
rescol = rescol + 1
End If
' all observation regardless of score
' trim removes any hidden leading spaces
' if scan starts at 11 remove the And j > 9
If Trim(ws.Cells(j, 4)) <> "" And j > 9 Then
' start new line for 2nd, 3rd etc observation
If Len(observ) > 0 Then observ = observ & vbCrLf
observ = observ & qu & ". " & ws.Cells(j, 4)
'observno = observno + 1
End If
' all comments regardless except first row which is header
If Trim(ws.Cells(j, 5)) <> "" And j > 9 Then
If Len(comment) > 0 Then comment = comment & vbCrLf
comment = comment & qu & ". " & ws.Cells(j, 5)
'commentno = commentno + 1
End If
Next j
wsRes.Cells(resrow, 8) = observ
wsRes.Cells(resrow, 9) = comment
sno = sno + 1
Next i
MsgBox Sheets.Count - 1 & " sheets scanned", vbInformation
End Sub

How to check for equal values in cells with a for loop?

I want to check if the text value in a cell is the same as in the cell below with a for loop.
If the value in Cell(1) and Cell(2) does not match I want the value from Cell(3) written in Cell(4).
I get an error
"Overflow (Error 6)"
Dim i As Integer
For i = 1 To Rows.Count
If Cells(2 + i,21) = Cells(3 + i,21) Then
i = i + 1
Else
a = Cells(3 + i, 1)
j = j + 1
Cells(228 + j, 3) = a
End If
Next i
End Sub
I have a production output and a timeline from 6 am to 12 am and I want to create a timetable as seen below.
Screenshot:
You could use
Option Explicit
Sub test()
Dim LastRowA As Long, i As Long, j As Long, LastRowW As Long
Dim StartTime As Date, EndTime As Date, strOutPut
j = 0
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 3 To LastRowA
If i > j - 1 Then
StartTime = .Range("A" & i).Value
strOutPut = .Range("U" & i).Value
For j = i + 1 To LastRowA + 1
If strOutPut <> .Range("U" & j).Value Then
EndTime = .Range("A" & j - 1).Value
LastRow = .Cells(.Rows.Count, "W").End(xlUp).Row
.Range("W" & LastRow + 1).Value = StartTime
.Range("X" & LastRow + 1).Value = EndTime
.Range("Y" & LastRow + 1).Value = strOutPut
Exit For
End If
Next j
End If
Next i
End With
End Sub
Result
Here I'm using a dictionary which will store every time for every product comma separated, so later will split that and take the first and last occurrence:
Sub TimeTable()
'Declare an array variable to store the data
'change MySheet for your sheet name
arr = ThisWorkbook.Sheets("MySheet").UsedRange.Value 'this will store the whole worksheet, the used area.
'Declare a dictionary object
Dim Products As Object: Set Products = CreateObject("Scripting.Dictionary")
'Loop through the array
Dim i As Long
For i = 3 To UBound(arr) 'start from row 3 because of your screenshoot
If arr(i, 21) = vbNullString Then GoTo NextRow 'if column U is empty won't add anything
If Not Products.Exists(arr(i, 21)) Then '21 is the column index for column U
Products.Add arr(i, 21), arr(i, 1)
Else
Products(arr(i, 21)) = arr(i, 21) & "," & arr(i, 1)
End If
NextRow:
Next i
Erase arr
'Redim the array to fit your final data, 4 columns and as many rows as products
ReDim arr(1 To Products.Count + 1, 1 To 4)
'Insert the headers
arr(1, 1) = "Time"
arr(1, 4) = "Product / Error"
'Now loop through the dictionary
Dim Key As Variant, MySplit As Variant
i = 2
For Each Key In Products.Keys
MySplit = Split(Products(Key), ",")
arr(i, 1) = MySplit(LBound(MySplit))
arr(i, 2) = "-"
arr(i, 3) = MySplit(UBound(MySplit))
arr(i, 4) = Key
i = i + 1
Next Key
'I don't know where are you going to paste your data, so I'm making a new worksheet at the end of your workbook
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
With ws
.Range("A1").Resize(UBound(arr), UBound(arr, 2)).Value = arr
.Range("A1:C1").Merge
End With
End Sub

Copying the data above blank cell to the last blank cell before meeting the next cell contains

Can someone help me if this is possible to do?
Logic is: If ColA = 1 and ColC >=1 then it should copy the entire row and insert new row below the last blank cell before meeting the next cell that contains then 1 will become 0.
Raw:
Final output should be:
I tried to put it as text but it doesn't seem right. the code i have for now is only this, its my first project tho. my code is still incomplete as i don't know what to do next. i tried a lot of codes but not working. here's the code:
Dim asd As Integer
Dim LastRow As Long
LastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For zxc = 2 To C
If Cells(zxc, "A").Value = 1 And Cells(zxc, "C").Value >= 1 Then
asd = asd + 1
End If
Next zxc
Dim AddCountRow As Long
AddCountRow = LastRow + asd
For i = 2 To AddCountRow
Dim A As Long
A = Worksheets("Sheet1").Cells(i, "A").Value
Dim B As Long
B = Worksheets("Sheet1"). Cells(i + 1, "D"). Value
If A >= 1 And B >= 1 Then
Cells(i + 1, "A").EntireRow.Insert
i = i + 1
End If
Next i
End Sub
Thank you so much guys!
This is a different approach. Considering maybe you have data below and
lastrow could not be reliable.
Look for the <<< Customize this >>> where I set the first cell where you have the header.
This code covers the data in the sample image:
Sub CopyInsertRows()
Dim colAValue As String
Dim colBValue As String
Dim colCValue As String
Dim colDValue As String
Dim initialCell As String
Dim rowCounter As Long
' <<< Customize this >>>
initialCell = "A4"
' Loop through all cells
For rowCounter = 2 To Rows.Count
If Range(initialCell).Cells(rowCounter, 1).Value <> vbNullString Then
colAValue = Range(initialCell).Cells(rowCounter, 1).Value
colBValue = Range(initialCell).Cells(rowCounter, 2).Value
colCValue = Range(initialCell).Cells(rowCounter, 3).Value
colDValue = Range(initialCell).Cells(rowCounter, 4).Value
ElseIf Range(initialCell).Cells(rowCounter, 1).Value = vbNullString And Range(initialCell).Cells(rowCounter + 1, 1).Value <> vbNullString Then
Range(initialCell).Cells(rowCounter + 1).EntireRow.Insert
Range(initialCell).Cells(rowCounter + 1, 1).Value = "0"
Range(initialCell).Cells(rowCounter + 1, 2).Value = colBValue
Range(initialCell).Cells(rowCounter + 1, 3).Value = colCValue
Range(initialCell).Cells(rowCounter + 1, 4).Value = colDValue
rowCounter = rowCounter + 1
End If
If Range(initialCell).Cells(rowCounter, 4).Value = vbNullString Then
Range(initialCell).Cells(rowCounter, 1).Value = "0"
Range(initialCell).Cells(rowCounter, 2).Value = colBValue
Range(initialCell).Cells(rowCounter, 3).Value = colCValue
Range(initialCell).Cells(rowCounter, 4).Value = colDValue
Exit For
End If
Next rowCounter
End Sub
This code covers the data in the sample linked file:
Sub CopyInsertRows()
Dim sourceRow As Range
Dim initialCell As String
Dim dateColumnLetter As String
Dim dateColumnNumber As Integer
Dim rowCounter As Long
' <<< Customize this >>>
initialCell = "A1" ' First cell of header row
dateColumnLetter = "AA" ' Where
' Get column number
dateColumnNumber = Range(dateColumnLetter & 1).Column
' Loop through all cells
For rowCounter = 2 To Rows.Count
If Range(initialCell).Cells(rowCounter, 1).Value <> vbNullString Then
' Store row values
Set sourceRow = Range(initialCell).Range("A" & rowCounter & ":" & dateColumnLetter & rowCounter)
ElseIf Range(initialCell).Cells(rowCounter, 1).Value = vbNullString And Range(initialCell).Cells(rowCounter + 1, 1).Value <> vbNullString Then
' Insert new row
Range(initialCell).Cells(rowCounter + 1).EntireRow.Insert
' Duplicate source row
Range(initialCell).Range("A" & rowCounter + 1 & ":" & dateColumnLetter & rowCounter + 1).Value = sourceRow.Value
' Replace first cell
Range(initialCell).Range("A" & rowCounter + 1).Value = "0"
rowCounter = rowCounter + 1
End If
If Range(initialCell).Cells(rowCounter, dateColumnNumber).Value = vbNullString Then
' Duplicate source row
Range(initialCell).Range("A" & rowCounter & ":Y" & rowCounter).Value = sourceRow.Value
' Replace first cell
Range(initialCell).Range("A" & rowCounter + 1).Value = "0"
Exit For
End If
Next rowCounter
End Sub
You will be inserting rows so work from the bottom up.
Sub addLines()
Dim i As Long, lr As Long, n As Long
With Worksheets("sheet5")
'collect last data row
lr = .Cells(.Rows.Count, "D").End(xlUp).Row + 1
'loop through the rows backwards, inserting rows and transferring values
For i = lr To 3 Step -1
If i = lr Or .Cells(i, "A") <> vbNullString Then
n = Application.Match(1E+99, .Range("A:A").Resize(i - 1, 1))
.Cells(i, "A").Resize(1, 4).Insert Shift:=xlDown
.Cells(i, "A").Resize(1, 4) = .Cells(n, "A").Resize(1, 4).Value
.Cells(i, "A") = 0
End If
Next i
End With
End Sub

Resources