So the code I have below attempts to find WIP in column H. If we find WIP: copy 3 cells and make 10 replicas of them in the next column either in the same row or the next available row.
For some reason the code only runs the loop successfully for the first "WIP" value and then stops running. Can someone see why this keeps happening?
Thank you,
Ori
Sub Step1_update()
Dim dblSKU As Double
Dim strDesc As String
Dim strType As String
Dim BrowFin As Integer
Dim Browfin1 As Integer
Dim Counter As Integer
Dim Trowfin As Integer
Counter = 0
Worksheets("Final").Activate
Trowfin = 5
BrowFin = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
'loop 1
Do While Trowfin < BrowFin
'If 1
If Range("H" & Trowfin).Value = Range("H3").Value Then
dblSKU = Range("F" & Trowfin).Value
strDesc = Range("G" & Trowfin).Value
strType = Range("H" & Trowfin).Value
Browfin1 = (ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row)
'If 2
If Browfin1 > Trowfin Then
Do While Counter < 15
Range("J" & Browfin1).Value = dblSKU
Range("K" & Browfin1).Value = strDesc
Range("L" & Browfin1).Value = strType
Counter = Counter + 1
Browfin1 = Browfin1 - 1
Trowfin = Trowfin + 1
Loop
ElseIf Browfin1 < Trowfin Then
Do While Counter < 15
Range("J" & Trowfin).Value = dblSKU
Range("K" & Trowfin).Value = strDesc
Range("L" & Trowfin).Value = strType
Counter = Counter + 1
Trowfin = Trowfin + 1
Loop
Else
Do While Counter < 15
Range("J" & Trowfin).Value = dblSKU
Range("K" & Trowfin).Value = strDesc
Range("L" & Trowfin).Value = strType
Counter = Counter + 1
Trowfin = Trowfin + 1
Loop
'If 2
End If
Else
Trowfin = Trowfin + 1
'If 1
End If
counter = 0
'loop 1
Loop
End Sub
You probably need to reset your counter back to 0 after you loop through it. Otherwise you are already at the 15 the next time you get to the loop again.
Related
I have two sheets. sheet1 is Evaluation and sheet2 is the result sheet.
From the Evaluation sheet1, I would like to count the number of "OK" in Column S and T and count the number of "Invalid" in the column R.
These counted values are to be entered In the sheet2 result , in their corresponding columns.
I was working with this code and It worked completely fine.
But now, when I run the code, number of OK in the column S and T and number of Invalid in the column R are not counted.
Here is the Code I am trying to work with
Sub result()
Dim i As Integer
Dim j As Integer
Dim cnt As Integer
Dim cntU, CntS, cntT As Integer
Dim sht As Worksheet
Dim totalrows, n As Long
Set sht = Sheets("Result")
Sheets("Evaluation").Select
totalrows = Range("A5").End(xlDown).Row
n = Worksheets("FC_SCR Evaluation").Range("A5:A" & totalrows).Cells.SpecialCells(xlCellTypeConstants).count
For i = 2 To WorksheetFunction.count(sht.Columns(1))
cntT = 0
cntU = 0
CntS = 0
' get the current week of column A of result sheet
If sht.Range("A" & i) = Val(Format(Now, "WW")) Then Exit For
Next i
' if column A of result sheet and Column x of fc sheet are same, then count the mentioned parameters
For j = 5 To WorksheetFunction.CountA(Columns(23))
If sht.Range("A" & i) = Range("X" & j) And Range("T" & j) = "OK" Then cntT = cntT + 1
If sht.Range("A" & i) = Range("X" & j) And Range("S" & j) = "OK" Then cntU = cntU + 1
If sht.Range("A" & i) = Range("X" & j) And Range("R" & j) = "Invalid" Then CntS = CntS + 1
' print the counted value in corresponding column of result sheet
If cntT <> 0 Then sht.Range("D" & i) = cntT
If cntU <> 0 Then sht.Range("E" & i) = cntU
If CntS <> 0 Then sht.Range("C" & i) = CntS
If n <> 0 Then sht.Range("B" & i) = n
Next j
If cntT + cntU <> 0 Then
sht.Range("F" & i) = CntS / n
sht.Range("g" & i) = cntT / n
sht.Range("h" & i) = cntU / n
End If
End Sub
I am working on a project that requires that I use a non sequential sequence that restarts everyday. Like this:
13/11/2017
1.1
1.2
2.1
2.2
3.1
And then starts over from 1.1 on the next day.
I'm not really sure what to do in this case. Use an array? Set an Excel column with those values? Use an event?
Can someone help me?
Here's the code so far.
Private Sub CommandButton4_Click() 'Guardar Banho Grelhas
Dim LastRow As Long, CR As Long, CRG As Long, CRP As Long, CRE As Long, ws As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet, rgr As Long, rgp As Double, rge As Long, Ar As Variant, n As String, x As Long, d As Long, PRG As Long
Set ws = Sheets("Banho Grelhas")
Set ws2 = Sheets("STOCK Grelhas")
Set ws3 = Sheets("STOCK Pyr")
Set ws4 = Sheets("STOCK Et")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Banho Grelhas
CR = ws.Range("A" & Rows.Count).End(xlUp).Row
PRG = ws2.Range("A" & Rows.Count).End(xlUp).Row - 1 'Previous Row STOCK Grelhas
CRP = ws3.Range("A" & Rows.Count).End(xlUp).Row - 1 'Current Row STOCK Pyr
CRE = ws4.Range("A" & Rows.Count).End(xlUp).Row - 1 'Current Row STOCK Et
CRG = PRG + 1 'Current Row Grelhas
rgr = TextBox6.Value 'Grelhas a remover
rgp = 17.5 * 0.8 'Pyrolidona a remover
rge = 17.5 * 0.2 'Ethylenodiamina a remover
n = "001"
x = Replace(Date, "/", " ")
d = x & n
Ar = Sheets("Banho Grelhas").Range("K1:K120").Value
ws.Range("A" & LastRow).Value = d
ws.Range("B" & LastRow).Value = TextBox26.Text 'ID Carbonação
ws.Range("C" & LastRow).Value = ws2.Range("A" & CRG) 'ID Grelhas
ws.Range("D" & LastRow).Value = TextBox6.Text 'Quantidade Grelhas / Banho
ws.Range("E" & LastRow).Value = ws3.Range("A" & CRP) 'ID Pyr
ws.Range("F" & LastRow).Value = ws4.Range("A" & CRE) 'ID ET
ws.Range("G" & LastRow).Value = "1,1"
ws.Range("H" & LastRow).Value = TextBox5.Text 'TETRA
ws.Range("I" & LastRow).Value = Format(Now(), "dd/mm/yyyy hh:mm") 'Data / Hora introdução
ws.Range("J" & LastRow).Value = Date
'Nº Banho
If InStr(ws.Range("G" & CR).Value, ",1") > 0 Then
ws.Range("G" & LastRow).Value = ws.Range("G" & CR).Value + 0.1
Else
ws.Range("G" & LastRow).Value = ws.Range("G" & CR).Value + 0.9
End If
'ID Banho
If ws.Range("J" & LastRow).Value = ws.Range("J" & CR).Value Then
ws.Range("A" & LastRow).Value = ws.Range("A" & CR).Value + 1
Else
ws.Range("A" & LastRow).Value = d
End If
Do While rgr > 0
If ws2.Range("H" & CRG).Value < rgr Then
rgr = rgr - ws2.Range("H" & CRG).Value
ws2.Range("H" & CRG).Value = 0
NRG = CRG + 1
Else
ws2.Range("H" & CRG).Value = ws2.Range("H" & CRG).Value - rgr
rgr = 0
End If
Loop
Do While rgp > 0
If ws3.Range("H" & CRP).Value < rgp Then
rgp = rgp - ws3.Range("H" & CRP).Value
ws3.Range("H" & CRP).Value = 0
CRP = CRP + 1
Else
ws3.Range("H" & CRP).Value = ws3.Range("H" & CRP).Value - rgp
rgp = 0
End If
Loop
Do While rge > 0
If ws4.Range("H" & CRE).Value < rge Then
rge = rge - ws4.Range("H" & CRE).Value
ws4.Range("H" & CRE).Value = 0
CRE = CRE + 1
Else
ws4.Range("H" & CRE).Value = ws4.Range("H" & CRE).Value - rge
rge = 0
End If
Loop
TextBox4.Value = Null
TextBox5.Value = Null
'TextBox6.Value = Null
TextBox26.Value = Null
TextBox27.Value = Null
End Sub
I don't know the contents of all your variables, but based on your code I am assuming this will work as intended:
'ws.Range("G" & LastRow).Value = "1,1"
'Nº Banho
If Format(ws.Range("I" & CR).Value, "dd/mm/yyyy") = Format(Now(), "dd/mm/yyyy") Then
If InStr(ws.Range("G" & CR).Value, ",1") > 0 Then
ws.Range("G" & LastRow).Value = ws.Range("G" & CR).Value + 0.1
Else
ws.Range("G" & LastRow).Value = ws.Range("G" & CR).Value + 0.9
End If
Else
ws.Range("G" & LastRow).Value = "1,1"
End If
It looks like you're using CR as the previous line, based on how you appear to be checking whether the previous sequence-value ends in ",1" or not.
The same logic can be extended to finding out whether this is a new day or not (and conversely, whether to reset the sequence). The logic I added is primarily this line:
If Format(ws.Range("I" & CR).Value, "dd/mm/yyyy") = Format(Now(), "dd/mm/yyyy") Then
Which is to say, if the datestamp in the previous line is the same as today's datestamp it proceeds with the code that increments the value. In reverse (the outtermost Else), this means that if the datestamps do not match - meaning the line we're adding has a datestamp that isn't the same as the datestamp in the previous line - we've reached a new day and the sequence has to be reset.
You'll also see that I commented out the line that initially sets the value of col G, as it looks like this line will at some point in time override a legitimate value.
I'm trying to run two for loops, one inside another. The inner loop should run all the way through every time the outer loop runs. I have attached my code but it's not working at the moment. There is also code to write the results to an external csv file, something which I have no idea how to do so if anyone can see any obvious mistakes then that would be much appreciated. Thank you in advance.
Public Sub Practice1()
Dim UpLim1 As Double, UpLim2 As Double, LowLim1 As Double, LowLim2 As Double
Dim outcome As String, FilePath As String, MtchUIDs As String
Dim i As Long, j As Long
Dim SRCUID As String, SNKUID As String
MtchUIDs = ""
FilePath = Application.DefaultFilePath & "\ffpstage1.csv"
Open FilePath For Output As #2
For i = 2 To 91
For j = 2 To 90
UpLim1 = Range("d" & i).Value
LowLim1 = Range("c" & j).Value
UpLim2 = Range("j" & i).Value
LowLim2 = Range("i" & j).Value
SRCUID = Range("a" & i).Value
SNKUID = Range("g" & j).Value
If UpLim2 >= LowLim1 And LowLim1 >= LowLim2 Then
MtchUIDs = SRCUID & SNKUID
ElseIf UpLim1 > LowLim2 And LowLim2 >= LowLim1 Then
MtchUIDs = SRCUID & SNKUID
Write #2, MtchUIDs
MtchUIDs = ""
Else
Next j
End If
Next i
End Sub
You need an End If within the For j loop, try this for the loops:
For i = 2 To 91
For j = 2 To 90
UpLim1 = Range("d" & i).Value
LowLim1 = Range("c" & j).Value
UpLim2 = Range("j" & i).Value
LowLim2 = Range("i" & j).Value
SRCUID = Range("a" & i).Value
SNKUID = Range("g" & j).Value
If UpLim2 >= LowLim1 And LowLim1 >= LowLim2 Then
MtchUIDs = SRCUID & SNKUID
ElseIf UpLim1 > LowLim2 And LowLim2 >= LowLim1 Then
MtchUIDs = SRCUID & SNKUID
Write #2, MtchUIDs
MtchUIDs = ""
End if
Next j
Next i
I am trying to loop through a list of data then and copy the rows that contain "WIP" into a second tab. yet the code will nor do anything when I hit execute. Can someone explain why?
Thank you.
Sub Update_LvL1_WIP()
Dim BrowFi As Integer
Dim BrowWIP1 As Integer
Dim dblSKU As Double
Dim strDescription As String
Dim strType As String
BrowFi = (ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row) + 1
Do While BrowFi > 4
If Range("G" & BrowFi).Value = "WIP" Then
strType = Range("G" & BrowFi).Value
strDescription = Range("F" & BrowFi).Value
dblSKU = Range("E" & BrowFi).Value
Worksheets("WIP 1").Activate
BrowWIP1 = (ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row) +1
Range("A" & BrowWIP1).Value = dblSKU
Range("B" & BrowWIP1).Value = strDescription
Range("B" & BrowWIP1).Value = strType
Else
BrowFi = BrowiF - 1
End If
Loop
End Sub
You have a typo in your decrement variable
BrowFi = BrowiF - 1
should be
BrowFi = BrowFi - 1
Since BrowiF was not defined it was treated as 0 and so you were setting BrowFi to -1 on the first run through the loop.
It may be worth using Option Explicit to help catch these errors.
I'm trying to compare two worksheets in excel to find new/updated records using vba.
(assume worksheet 1 is old, and worksheet 2 has the potential new/updated entries)
These sheets have very similar information stored in each, just in a different order.
For example:
Worksheet 1 has Street Address in Column E whereas Worksheet 2 has the street Address in Column H. There are many other columns like this.
I'm not really sure where to start. I tried to rearrange the columns in the second sheet by cutting and inserting to match those of the first, but that got out of hand very quickly.
Also, if its a new record, it needs be appended to the end of the data.
**Updated to allow defining the 'key' column. Just change the line 'iKeyCol = 2' to the desired column.
Here is some code to try. I was too lazy to rework all the code I was using, so some of this may be extra for you. Make sure your workbook
1. Has at least three sheets (names 'Sheet1, Sheet2, NewSheet')
2. Has column headers for Sheet1 & Sheet2
3. Col1 must match in both sheets
4. Column count must match in both sheets.
Other that col1, other columns can be in any order.
Paste the code into a new module and the execute.
Let me know if you have a problem.
Option Explicit
' This module will compare differences between two worksheets.
Sub Compare106thWorksheets()
Dim iKeyCol As Integer
'>>>> CHANGE THE FOLLOWING LINE TO IDENTIFY THE KEY COLUMN
iKeyCol = 2
Dim i, i2, i3 As Integer
Dim iRow As Long
Dim iR1, iR2 As Long
Dim iC1, iC2 As Integer
Dim iColMap(30) As Integer
Dim iCol1, iCol2 As Integer
Dim LastRow1 As Long, LastRow2 As Long
Dim LastCol1 As Integer, LastCol2 As Integer
Dim MaxRow1 As Long
Dim MaxCol1 As Integer
Dim sFld1 As String, sFld2 As String
Dim sFN1, sFN2 As String
Dim rptWB As Workbook
Dim DiffCount As Long
Dim iLastRow, iLastColumn As Integer
Dim strDeleted, strInserted As String
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim wsChg As Worksheet
Dim iCHGRows As Long
Dim iCHGCols As Long
Application.ScreenUpdating = False
Application.StatusBar = "Creating the report..."
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Set ws2 = ThisWorkbook.Worksheets("Sheet2")
Set wsChg = ThisWorkbook.Worksheets("NewSheet")
With ws1.UsedRange ' Get used range of Sheet1
LastRow1 = .Rows.Count
LastCol1 = .Columns.Count
End With
With ws2.UsedRange ' Get used range of Sheet1
LastRow2 = .Rows.Count
LastCol2 = .Columns.Count
End With
With wsChg.UsedRange ' Get used range of Sheet1
iCHGRows = .Rows.Count
iCHGCols = LastCol1
End With
MaxRow1 = LastRow1
MaxCol1 = LastCol1
Debug.Print ws1.Name & " has " & LastRow1 & " rows and " & LastCol1 & " columns."
Debug.Print ws2.Name & " has " & LastRow2 & " rows and " & LastCol2 & " columns."
If MaxRow1 < LastRow2 Then MaxRow1 = LastRow2
If MaxCol1 < LastCol2 Then MaxCol1 = LastCol2
' Build a column map. Require both sheets to have the same names - but different order.
For i = 1 To 30
iColMap(i) = 0
Next i
For iC1 = 1 To MaxCol1
For i = 1 To LastCol2
If ws1.Cells(1, iC1) = ws2.Cells(1, i) Then
iColMap(iC1) = i
Exit For
End If
Next i
Next iC1
' Check if any column headers failed to match.
For i = 1 To MaxCol1
If iColMap(i) = 0 Then
MsgBox "Column named '" & ws1.Cells(1, i) & " not found in Sheet2. Please correct and start again."
GoTo Exit_Code
End If
Next i
strDeleted = "": strInserted = ""
iR2 = 1
DiffCount = 0
For iR1 = 1 To MaxRow1
If ws1.Cells(iR1, iKeyCol) <> ws2.Cells(iR2, iKeyCol) Then ' Cell is different - is it an ADD or Delete?
Debug.Print "Row: " & iR1 & vbTab & ws1.Cells(iR1, iKeyCol) & vbTab & "versus: " & ws2.Cells(iR2, iKeyCol)
sFld1 = Trim(ws1.Cells(iR1, iKeyCol).FormulaLocal)
sFld2 = Trim(ws2.Cells(iR2, iKeyCol).FormulaLocal)
If sFld1 < sFld2 Then
Debug.Print "Deleted Row " & ws1.Cells(iR1, iKeyCol)
DiffCount = DiffCount + 1
wsChg.Cells(DiffCount, iKeyCol) = "Deleted:"
wsChg.Cells(DiffCount, 2) = ws1.Cells(iR1, iKeyCol)
strDeleted = strDeleted & ws1.Cells(iR1, iKeyCol) & vbCrLf
iCHGRows = iCHGRows + 1
wsChg.Cells(iCHGRows, 1) = Now()
For i = 1 To LastCol1
wsChg.Cells(iCHGRows, i + 1) = ws1.Cells(iR1, i)
Next i
ws1.Rows(iR1).EntireRow.Delete
iR1 = iR1 - 1
GoTo Its_OK
ElseIf sFld1 > sFld2 Then
Debug.Print "Inserted Row " & ws2.Cells(iR1, iKeyCol)
Debug.Print "R1: " & iR1 & " R2: " & iR2 & vbTab & ws1.Cells(iR1, iKeyCol) & vbTab & "versus: " & ws2.Cells(iR2, iKeyCol)
DiffCount = DiffCount + 1
strInserted = strInserted & ws2.Cells(iR2, iKeyCol) & vbCrLf
ws1.Rows(iR1).EntireRow.Insert
For i = 1 To LastCol1
ws1.Cells(iR1, i) = ws2.Cells(iR2, iColMap(i))
Next i
iR2 = iR2 + 1
GoTo Its_OK
Else
iR2 = iR2 + 1
End If
Else ' Values are the same
iR2 = iR2 + 1
End If
Its_OK:
Next iR1
Debug.Print "Deleted:"
Debug.Print strDeleted
Debug.Print "------------------------------------------------------------------"
Debug.Print "Inserted:"
Debug.Print strInserted
Debug.Print "------------------------------------------------------------------"
For iRow = 2 To LastRow2
Application.StatusBar = "Comparing cells " & Format(iCol1 / MaxCol1, "0 %") & "..."
For iCol1 = 1 To LastCol1
iCol2 = iColMap(iCol1)
sFld1 = ""
sFld2 = ""
On Error Resume Next
sFld1 = ws1.Cells(iRow, iCol1).FormulaLocal
sFld2 = ws2.Cells(iRow, iCol2).FormulaLocal
On Error GoTo 0
If sFld1 <> sFld2 Then
Debug.Print "Row: " & iRow & vbTab & ws1.Cells(iRow, iCol1) & vbTab & "versus: " & ws2.Cells(iRow, iCol2)
DiffCount = DiffCount + 1
wsChg.Cells(DiffCount, 1) = ws1.Cells(iRow, iKeyCol)
wsChg.Cells(DiffCount, 2) = ws1.Cells(1, iCol1)
wsChg.Cells(DiffCount, 3) = sFld1
wsChg.Cells(DiffCount, 4) = sFld2
ws1.Cells(iRow, iCol1).FormulaLocal = ws2.Cells(iRow, iCol2).FormulaLocal
End If
Next iCol1
Next iRow
wsChg.Activate
Application.StatusBar = "Formatting the report..."
With Range(Cells(1, 1), Cells(MaxRow1, MaxCol1))
.Interior.ColorIndex = 19
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error Resume Next
With .Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
With .Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlHairline
End With
On Error GoTo 0
End With
Columns("A:IV").ColumnWidth = 20
MsgBox DiffCount & " cells contain different formulas!", vbInformation, _
"Compare " & ws1.Name & " with " & ws2.Name
Exit_Code:
Application.StatusBar = False
Application.ScreenUpdating = True
End Sub