So I made a Excel macro and when i try to run it, it keeps crashing. Here it is:
result = "fail"
j = 4
Do While result = "fail" Or Cells(2, j) <> " "
If Cells(2, j).Value >= 15 Then
result = "pass"
Else
j = j + 1
End If
Loop
Your Do While loop Do While result = "fail" Or Cells(2, j) <> " " will run if either result = "fail" or Cells(2, j) <> " ".
I think you meant to exit the loop once you reach an empty cell, or you get result = "pass". So, you need to change your Or to And:
Do While result = "fail" And Cells(2, j) <> " "
If you want to exit in case there are only empty space in the cell, add also Trim.
Do While result = "fail" And Trim(Cells(2, j)) <> ""
changed or to and.
the loop needs to stop somewhere, currently it is going into an endless loop.
pls try below code and let me know if this is what you wanted or I have understood your requirement incorrectly.
Sub tester()
result = "fail"
j = 4
Do While result = "fail" And Cells(2, j) <> ""
If Cells(2, j).Value >= 15 Then
result = "pass"
Else
j = j + 1
End If
Loop
End Sub
Related
I am having a sheet having seven cloumns. First six columns having either true or false and in last column I have to mention the heading of false cases in one statement. Below is the excel.
Excel sheet
I have tried if else statement but there are too many possibilities. Since I am new to VBA i don't know any shortcut to that.Any suggestions?.... Thanks
Try this simple vba code,
Sub TEXTJOIN()
Dim i As Long, str As String, k As Long, j As Long
str = ""
j = 0
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
If Application.WorksheetFunction.CountIf(Range("A" & i & ":F" & i), True) = 6 Then
Cells(i, 7) = "No Discrepancy Found"
Else
For k = 1 To 6
If Cells(i, k) = False Then
str = str & Cells(1, k) & ","
j = j + 1
End If
Next k
str = Left(str, Len(str) - 1) & " mismatch found"
Cells(i, 7) = Application.WorksheetFunction.Substitute(str, ",", " and ", j - 1)
str = ""
j = 0
End If
Next i
End Sub
Here's simple code which you should try:
Sub FindDiscrepancies()
Dim lastRow, i, j As Long
Dim discrepancies As String: discrepancies = ""
'find number of last row
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
For j = 1 To 6
If LCase(Cells(i, j).Value) = "false" Then
discrepancies = discrepancies & Cells(1, j).Value & ", "
End If
Next j
If discrepancies = "" Then
Cells(i, 7).Value = "No discrepancies found"
Else
Cells(i, 7).Value = "Mismatch found in " & discrepancies
End If
discrepancies = ""
Next i
End Sub
I need to compare a cell to the one below it in a loop. i know for most languages you can say something like "if cells(i,1).value = cells(i+1,1).value then..."
is there a way to do this in vba for some reason it isn't working for me. thanks
For i = 7 To ltrw
If (Cells(i, 1).Value = 0 And Cells(i + 1, 1).Value = 0 Then
Cells(i, 1).EntireRow.Hidden = True
End If
Next i
like this ?
For i = 7 To ltrw
If Cells(i).Value = Cells(i + 1).Value Then ' you can skip the ", 1" as its optional
Cells(i).EntireRow.Hidden = True
End If
Next i
For active_row = 9 To last_row
ws1_func_loc = ThisWorkbook.Sheets(ws1).Cells(active_row, "C").Value
ws1_mat_id = ThisWorkbook.Sheets(ws1).Cells(active_row, "D").Value
ws1_mat_qty = ThisWorkbook.Sheets(ws1).Cells(active_row, "I").Value
ws1_reason2 = ""
zc_sum = WorksheetFunction.SumIfs(ThisWorkbook.Sheets(ws2).Range("F:F"), ThisWorkbook.Sheets(ws2).Range("K:K"), ws1_func_loc, ThisWorkbook.Sheets(ws2).Range("N:N"), ws1_mat_id, ThisWorkbook.Sheets(ws2).Range("S:S"), "ZC")
zk_sum = WorksheetFunction.SumIfs(ThisWorkbook.Sheets(ws2).Range("F:F"), ThisWorkbook.Sheets(ws2).Range("K:K"), ws1_func_loc, ThisWorkbook.Sheets(ws2).Range("N:N"), ws1_mat_id, ThisWorkbook.Sheets(ws2).Range("S:S"), "ZK")
'some other If conditions...
ElseIf zc_sum = 0 And zk_sum > 0 Then
row_match_count = WorksheetFunction.CountIf(ThisWorkbook.Sheets(ws2).Range("K:K"), ws1_func_loc)
Set found = ThisWorkbook.Sheets(ws2).Range("K:K").Find(What:=ws1_func_loc) 'find() found nothing on 2nd iteration
For i = 1 To row_match_count
If ThisWorkbook.Sheets(ws2).Cells(found.Row, "N").Value = ws1_mat_id And ThisWorkbook.Sheets(ws2).Cells(found.Row, "S") = "ZK" And Not found Is Nothing Then
ws1_reason2 = ws1_reason2 & Chr(10) & ThisWorkbook.Sheets(ws2).Cells(found.Row, "R").Value & ", " & "qty " & ThisWorkbook.Sheets(ws2).Cells(found.Row, "F").Value & ", " & ThisWorkbook.Sheets(ws2).Cells(found.Row, "U").Value & ", " & ThisWorkbook.Sheets(ws2).Cells(found.Row, "W").Value
Set found = Sheets(ws2).Range("K:K").FindNext(After:=found)
Else
Set found = Sheets(ws2).Range("K:K").FindNext(After:=found)
End If
Next i
ThisWorkbook.Sheets(ws1).Cells(active_row, "O").Value = ws1_reason2
ElseIf .......
I am trying to do a multi criteria search whereby I am finding all the rows in worksheet 2 (ws2) which matches the values in a specific row in worksheet 1 (ws2).
It works during the first For loop iteration, i.e. active_row = 9, but during the second iteration, i.e. when active_row = 10, Set found = ThisWorkbook.Sheets(ws2).Range("K:K").Find(What:=ws1_func_loc) returns Nothing.
But there is definitely at least a match because row_match_count comes up with a value greater than 0.
I finally figured out what's wrong.
There is a bunch of drop down list in ws2 which will filter the results.
If I used the drop down list to filter the rows, ThisWorkbook.Sheets(ws2).Range("K:K").Find(What:=ws1_func_loc) will only be able to see/find from the filtered results, while WorksheetFunction.CountIf(ThisWorkbook.Sheets(ws2).Range("K:K"), ws1_func_loc) is still able to see the whole worksheet.
So I have a column of notes in excel that has notes like "01/16 14:38 ATND [Notes from Dealer/Distributor] JR" and "01/16 14:14 ATND [Notes from Company] JR2" and "01/16 14:14 ATND [Notes from Company] TLO The item is back ordered"
As you can see after the bracket sign, there is a two letter or three letter codes of three different variations, JR, JR2 and TLO. I wrote a program that only distinguishes between JR and TLO but wont extract the code if its numbered for example, JR and JR2. If someone can help me with this, I would greatly appreciate it.
Sub G_ExtractCodes()
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Dim i As Long
Dim NoteCodes As Range
For i = LR To 2 Step -1
Set NoteCodes = Range("O" & i)
If InStr(NoteCodes, "JR") >= 1 Then
Cells(i, 20) = "JR"
ElseIf InStr(NoteCodes, "JR2") >= 1 Then
Cells(i, 20) = "JR2"
ElseIf InStr(NoteCodes, "TLO") >= 1 Then
Cells(i, 20) = "TLO"
End If
Next i
End Sub
Your first condition in the If statement is more restrictive than the first ElseIf clause, so any instance of "JR2" will be caught by the first test (only finds "JR", and the ElseIf are not evaluated).
Flip your logic, and I think this should fix it:
If InStr(NoteCodes, "JR2") >= 1 Then
Cells(i, 20) = "JR2"
ElseIf InStr(NoteCodes, "JR") >= 1 Then
Cells(i, 20) = "JR"
ElseIf InStr(NoteCodes, "TLO") >= 1 Then
Cells(i, 20) = "TLO"
End If
Alternatively, you can parse out the codes like:
Dim codeValue as String
Dim bracketLocation as Integer
For i = LR To 2 Step -1
Set NoteCodes = Range("O" & i)
'finds the position of the right bracket
bracketLocation = Instr(NoteCodes, "]")
'Lops off any characters up to & including the right square bracket
codeValue = Trim(Mid(NoteCodes, bracketLocation + 1))
'removes any text that appears *after* the code value, if any
If Instr(codeValue, " ") > 0 Then
codeValue = Left(codeValue, Instr(codeValue, " "))
End If
Cells(i, 20).Value = codeValue
'clear out the variable
codeValue = vbNullString
Next
Sub G_ExtractCodes()
sn=cells(1).currentregion.columns(1).offset(,15)
for j=1 to ubound(sn)
If InStr(sn(j,1), "JR") Then
Cells(j, 20) = iif(instr(sn(j,1),"JR2"),"JR2","JR")
ElseIf InStr(sn(j,1), "TL") Then
Cells(j, 20) = iif(instr(sn(j,1),"TL0"),"TL0","TL")
End If
Next
End Sub
I have a problem with my code, an error appears, and I don;t understand why. The error is:
"Compile error: Next without For"
I do not understand why it is like that. I am new to coding so any help and comments are more than welcome.
This is the code, the Next which is pointed as the one without For is provided a comment.
Sub CGT_Cost()
startrow = Worksheets("GUTS").Cells(10, 1) 'Here I put 1
endrow = Worksheets("GUTS").Cells(11, 1) 'Here I put 1000
For x = endrow To startrow Step -1
If Cells(x, "Q").Value = "Sale" Then
If Cells(x, "D").Value = "1" Then
For i = 1 To 1000
If Cells(x - i, "R").Value <> "1" Then
Next i
Else
Range("G" & x).FormulaR1C1 = "=R[-" & i & "]C/R[-" & i & "]C[-1]*RC[-1]"
End If
End If
End If
Next x
End Sub
Thank you all in advance,
with best regards,
Artur.
Every For statement with a body must have a matching Next, and every If-Then statement with a body must have a matching End If.
Example:
For i = 1 To 10 '<---- This is the header
Hello(i) = "Blah" '<---- This is the body
Next i '<---- This is the closing statement
You have part of the body of your If statement inside your For i loop, and part of it outside. It has to be either ALL inside or ALL outside. Think through the logic and see what it is you want to do.
you have overlapping loops-perhaps
Sub CGT_Cost()
startrow = Worksheets("GUTS").Cells(10, 1) 'Here I put 1
endrow = Worksheets("GUTS").Cells(11, 1) 'Here I put 1000
For x = endrow To startrow Step -1
If Cells(x, "Q").Value = "Sale" Then
If Cells(x, "D").Value = "1" Then
For i = 1 To 1000
If Cells(x - i, "R").Value <> "1" Then
'
Else
Range("G" & x).FormulaR1C1 = "=R[-" & i & "]C/R[-" & i & "]C[-1]*RC[-1]"
End If
Next i
End If
End If
Next x
End Sub