I would like to compare two cells and it should display "OK" or "NOT OK".
When I used an excel formula, it worked properly.
However when I tried it using VBA, I am not getting the correct results.
Below is a simpler example of my data:
Fee1
Fee2
0.009
0.009
To note that in my worksheet, Fee1 is generated via a VBA vlookup function and Fee2 is generated via a case statement VBA code.
My code is as per below:
If Range("L2") <> Range("M2") Then
Range("N2").Value = "NOT OK"
Else
Range("N2").Value = "OK"
End If
Debug.Print Range("N2")
Debug.Print Range("L2")
Debug.Print Range("m2")
I used debug print to check and got the results below:
NOT OK
0.009
0.009
I am perplexed at what i am doing wrong.
You might want to compare the values with a limited number of decimals like:
If Round(Range("L2").Value, 3) <> Round(Range("M2").Value, 3) Then
Related
I am using vlookup to get data from a source but I want the result to be blank instead of 0 or N/A when there is no vlookup value. So far it gives me an error. My code:
ws1.Range("H2:H" & lastrow3).Formula = "=IF(ISNA(VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:H,3,FALSE))+(VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:H,3,FALSE)=""),"",VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:H,3,FALSE))"
Edit (Still it does not run)
ws1.Range("H2:H" & lastrow3).Formula = "=IFERROR(VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:H,3,FALSE),"")"
I could emulate your example with a sample code, because you didn't provide a data to testing purposes.
The formula that worked:
=IFERROR(LOOKUP(B11;A1:A9;B1:B9);"")
So I have a template that I am inputting data from a csv into an excel file. The data doesn't always "file the template" so to speak or there are some fields that have '0' in them. What I am trying to do is add a for loop or something of the sort to basically 'throw out' the 0's but still average the cells say BX2-BX50. I would like excel to ignore the 0's when averaging the numbers within those fields.
Any help would be greatly appreciated!
No need to over-think this. The AVERAGEIFS function should be more than sufficient.
=averageifs(BX:BX, BX:BX, "<>0")
all,
so you mean the line with "0" is not the data, is only the error line when you convert it from CSV to Excel? If so, it is better to delete those lines as you know if the 0 shows as the correct data, it should reduce the overall average. you can eliminate the error data but you cannot ignore the real to lead to wrong result.
and for delete error zero line, you can use loop:
Sub removeZero()
Dim a As Integer
a = ActiveSheet.UsedRange.Rows.Count
For i = 1 To a
If Range("B" & i) = 0 Then
Range("B" & i) = ""
End If
Next i
Columns("B:B").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
I have a macro that inserts a VLOOKUP into a column. The macro has to take a number stored as text and convert it to a number, before looking up that number in another sheet.
The macro always produces the same results, such as reaching row 43 before starting to produce erroneous results however when using F8 to step through the code, these incorrect results are not produced.
The erroneous results are that the value placed into col 13 is not equal to the number stored as text. Mostly it seems as though values from rows above and below, sometimes 2 rows below are being inserted to col 13. Almost seems to me as if 2 different threads are running at 2 different speeds or something?
If anyone could have a look at the loop causing the errors I would be grateful, thanks.
For counter = 2 To NumRowsList
checker = CInt(Sheets("Sheet2").Cells(counter, 3)
Sheets("Sheet2").Cells(counter, 13).Value = checker
'Call WaitFor(0.5)
If checker < 4000 Then
Sheets("Sheet2").Cells(counter, 14) = "=VLOOKUP(M" & counter & ",Sheet4!E2:F126,2,FALSE)"
Else
Sheets("Sheet2").Cells(counter, 14) = "=VLOOKUP(M" & counter & ",Sheet5!B2:C200,2,FALSE)"
End If
Next counter
I have tried a few similar variations of this code, such as using the value stored in col 13 directly rather than using the cell reference in the VLOOKUP, always producing the same results.
I even used the waitfor function to try and create a delay hoping it may synchronise the operations, but it did not help and using a delay of more than 0.5 would cause the run time of the macro to be too big.
UPDATE:
I did not find a perfect solution, only a long hand work around. I simply combined the Vlookups onto a single sheet, and converted the numbers stored as text to numbers outside of the vba routine. This took the error away from the number calculation (just col C * 1), and then the vlookups were looking up the correct values. Thank you for the help, regardless.
you can avoid looping, checker and all those If-Then-Else, like follows
edited to account for VlookUp range depending on VlookUp value
With Worksheets("Sheet2")
.Range("N2", .Cells(NumRowsList, 14)).FormulaR1C1 = "=VLOOKUP(Value(RC3),IF(Value(RC3)<4000,Sheet4!R2C5:R126C6,Sheet4!R2C2:R200C3),2,FALSE)"
End With
The following works for me with my test data, but you'll need to see if it works for you... (also are you turning off calculation or events? I don't know if this might have an issue?)
I find it preferable to set a reference to the sheet you want to use rather than access it directly, and this may help?
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet2")
Dim VLURange As String, checker As Long
For counter = 2 To 200 ' NumRowsList
checker = CLng(ws.Cells(counter, 3).Value)
ws.Cells(counter, 13) = checker
VLURange = IIf(checker < 4000, "Sheet4!E2:F126", "Sheet5!B2:C200")
ws.Cells(counter, 14) = "=VLOOKUP(M" & counter & ", " & VLURange & ", 2, FALSE)"
Next counter
Thanks for taking a look at my question. Know that I am new to stackoverflow and VBA I have a long macro I've been working on. At one point in the macro I have to convert "Start Depth" (L) and "End Depth" (M) from "m" to "ft". Then I subtract the two to find the "Footage" (N). However, some of the values in the columns are originally left blank. So, after making my conversions and subtractions, I'm left with "#VALUE!" which is giving me errors later on in the macro. Originally I had changed all of the blanks to 0's before the conversions and subtractions. But, After "finishing" the macro I realize the zeros are messing with presenting the data. So, I'd like to just do the conversions and subtractions and then, change all the "#VALUES!" back to blanks. I found some stuff on this but nothing that I could (that i know of) use or specific to me:
http://www.ozgrid.com/forum/showthread.php?t=60740 and https://superuser.com/questions/715744/excel-2010-formula-how-do-i-write-this-formula-vba
Here is what i was using to change blanks into 0's
Worksheet1.Select
lastrow = Range("A666666").End(xlUp).Row
For Each cell In Range("A1:Q" & lastrow)
If Len(cell.Value) = 0 Then
cell.Value = 0
End If
Next
Here is the code resulting in errors. Note: The data starts with blanks and after using these formulas I am given errors because some of the original cells begin as null or blanks. Also, These lines aren't in this order but, they are the lines leaving errors.
ActiveCell.FormulaR1C1 = "=CONVERT(RC[2],""m"",""ft"")"
Selection.AutoFill Destination:=Range("N2:O2"), Type:=xlFillDefault
Selection.AutoFill Destination:=Range("N2:O" & lastrow)
Range("N1") = "Footage"
Range("N2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
Selection.AutoFill Destination:=Range("N2:N" & lastrow)
Any help is appreciated. Thanks guys!
It depends if the #VALUE! is being generated from a formula or from VBA code.
Formula
If it's being generated from a formula, wrap the formula in the IFERROR function. This was added in Excel 2007 I believe. So for example if your formula was:
=A1-B1
Then you could put
=IFERROR(A1-B1,"")
Which is saying, if A1-B1 is an error, return "", otherwise return the result of A1-B1.
VBA
If the value is being generated by VBA you could write a helper function that works like IFERROR
Public Function MyIfError(value As Variant, value_if_error As Variant)
If IsError(value) Then
MyIfError = value_if_error
Else
MyIfError = value
End If
End Function
And then pass your value through that.
This does not directly answer your question, but it does fix the presentation of Zero's on your worksheet. You can use the following setting to show Zeros as Blanks through VBA: ActiveWindow.DisplayZeros = False It might be a consideration instead of looping through everything looking for 0's and switching them to blanks manually.
Worksheet1.Select
lastrow = Range("A666666").End(xlUp).Row
For Each cell In Range("A1:Q" & lastrow)
If Len(cell.Value) = 0 Then
cell.Value = 0
End If
Next
For Each cell In Range("A1:Q" & lastrow)
cell.value = WorksheetFunction.Iferror(cell.value,"")
End If
Next
I'm sorry I don't have opprtunity to test it right but give it a try.
Bottom line is that I simply included a second loop that runs IFERROR function on your data.
I am having difficulties with this code. I am trying to make it so that based on the value of cell D25 the value of F25 will change. I made the code in VBA (don’t know if this is actually the best way) and it is giving me a 424 runtime error: Object Required. Could someone please point me in the right direction?
Sub Storage_vesel_Controle()
Sheets("NSR Form").Select
If Range("D25") = "1" Then Range("F25").Select.Value = "0"
If Range("D25") = "2" Then Range("F25").Select.Value = ".95"
If Range("D25") = "3" Then Range("F25").Select.Paste = ".98"
End Sub
Also, what do I need to add to make the code "always running"... in loop I think?
Further to my comment above, I wasn't planning on posting an answer but saw few things which I think I will bring to your notice and comments wouldn't accommodate so much of text.
I would recommend not using .Select to select the sheet but work with the sheet directly (See Below code) You might also want to see this link
Sub Storage_vesel_Controle()
With Sheets("NSR Form")
Select Case .Range("D25").Value
Case 1: .Range("F25").Value = 0
Case 2: .Range("F25").Value = 0.95
Case 3: .Range("F25").Value = 0.98
End Select
End With
End Sub
EDIT:
Regarding your edit. You can use the Worksheet_Change Event to ensure that the values of F25 gets automatically updated or if you want a non VBA solution then simply use a Formula in F25
If you are interested in Worksheet_Change then see this. Else a simple formula like this in F25 will suffice your needs :)
=IF(D25=1,0,IF(D25=2,0.95,IF(D25=3, 0.98,"")))
Clean it up a little bit. Using a Select Case statement will be easier to work with than mutliple If/Then statements.
Sub Storage_vesel_Controle()
With Sheets("NSR Form")
.Activate '<this line is not actually necessary unless you want to be on the sheet.
Select Case .Range("D25").Value
Case 1
.Range("F25").Value = 0
Case 2
.Range("F25").Value = 0.95
Case 3
.Range("F25").Paste = 0.98
Case Else
'do nothing, or, modify as needed
End Select
End With
End Sub