Using multiple if/then statements without getting a runtime error - vba

New to vba and trying to write a simple if/then statement in sequential order to return a value from different sheets.
If Cells(4, 4).Value = "water stress" Then
Cells(15, 4).Value = Application.WorksheetFunction.VLookup(region, ws2.Range("a3:b12"), 2)
End If
If Cells(4, 4).Value = "fire" Then
Cells(15, 4).Value = Application.WorksheetFunction.VLookup(region, ws3.Range("a3:b12"), 2)
End If
When i run the code with only the first if statement, it runs fine. When I add in the second if statement, I get a run-time error '1004' and am unable to get the vlookup property of the worksheetfunction class. I am new, and not sure how to trouble shoot.

Related

Unable to run the copy code in Excel VBA

I am not able to run the below copy code, getting error Application -defined or Object-Defined error. This code was running earlier but is now giving the error. Please help me as I am not able to understand what went wrong.
For i = Start To last
'MsgBox ActiveSheet.Cells(i, 17).Value, vbInformation
If (ActiveSheet.Cells(i, 17).Value >= ActiveSheet.Cells(1, 34).Value) Then
'MsgBox ActiveSheet.Cells(i, 17).Value, vbInformation
Worksheets("Sample Data").Range(Cells(i, 1), Cells(i, 2)).Copy
Worksheets("Output").Cells(j, 2).PasteSpecial xlValues
j = j + 1
End If
Next i
I am able to run it line by line but getting the error when pressingf5.
The code runs fine now on restart. This can be ignored as the code is correct.

WorksheetFunction error sub or function not defined

I was wondering if someone could assist me please. I have created a very simple userform to log information. My issue is however when one of the fields is blank I am receiving an error message :
Sub or Function not defined.
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Data")
'find first empty row in database
iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
'check for a Name number
If Trim(QueryType.Value) = "" Then
QueryType.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If
'copy the data to the database
ws.Cells(iRow, 1).Value = Format(Now, "DD/MM/YYYY")
ws.Cells(iRow, 2).Value = Format(Now, "HH:MM")
ws.Cells(iRow, 3).Value = Application.UserName
ws.Cells(iRow, 4).Value = CType.Value
ws.Cells(iRow, 5).Value = IName.Value
ws.Cells(iRow, 6).Value = QType.Value
ws.Cells(iRow, 7).Value = 1
ws.Cells(iRow, 8).Value = Format(Date, "MMM-YY")
'ws.Cells(iRow, 9).Value = Application.WorksheetFunction.VLookup(InternalName.Value, Sheet2.Range("C1:D23"), 2, 0)
'ws.Cells(iRow, 9).Value = Application.WorksheetFunction.IfError(VLookup(InternalName.Value, Sheet2.Range("C1:D23"), 2, 0), "")
ws.Cells(iRow, 10).Value = Application.WorksheetFunction.VLookup(InternalName.Value, Sheet2.Range("C1:E23"), 3, 0)
ws.Cells(iRow, 11).Value = "IB"
Unload Me
MsgBox "Data added", vbOKOnly + vbInformation, "Data Added"
End Sub
The issue is in either one of the commented out lines where I receive the error. I only receive if I leave the dropdown box empty. If populated then no error occurrs. I could easily fit an extra menu option for "Not Applicable" however would rather it was just blank. Does anyone have any suggestions at all please?
Three things.
1- VLookup is not a VBA function on its own, it's a member method of either the Application object or the WorksheetFunction object. So you should qualify VLookup by either, even if you had it inside another method, such as WorksheetFunction.IfError(...).
ws.Cells(iRow, 9).Value =
WorksheetFunction.IfError(WorksheetFunction.VLookup(InternalName.Value, Sheet2.Range("C1:D23"), 2, 0), "")
' ^^^^^^^^^^^^^^^^^^
2- The same methods in WorksheetFunction and in Application objects work differently. In the former, an error is raised in VBA if the result is an error (such as: no match for the searched value). In the latter, no error is raised, but the returned value is an Error Variant. This latter form is usually safer for VBA, because you dont need to have some On Error Resume Next or so, but you can just check the result with If(IsError(result)).
3- When your search criteria is empty or unmatched, you had the error raised due to the use of WorksheetFunction.VLookup (According to 2). If your intention is to just set the resulting value and proceed, you can use Application.VLookup instead:
ws.Cells(iRow, 9).Value = Application.IfError(Application.VLookup(InternalName.Value, Sheet2.Range("C1:D23"), 2, 0), "")
p.s. I personally prefer Application. most of the time. Some prefer WorksheetFunction mostly because it offers Intellisense, but I find that pretty useless, because the arguments of the methods in Intellisense are unnamed and untyped, i.e. VLookup(arg1, arg2, arg3, [arg4]).. (non-sense to me).

VB Error - Object doesnt support this property or method

I am at a very basic part of VB and I am stuck with this relentless issue. I am trying to copy data from a form to a bunch of cells in excel and I am facing this error.
Object doesn't support this property or method: Runtime Error - 438
Although the first row gets inserted. The issue is with the second row. I don't see the problem. Kindly help
Sub sbInsertingRows()
Range("A3").EntireRow.Insert
End Sub
Sub EditAdd()
sbInsertingRows
Cells(3, 19).Value = Order_Details_Form.Controls(TextBox_orderid).Value
Cells(3, 20).Value = Order_Details_Form.Controls(TextBox_name).Value
Cells(3, 24).Value = Order_Details_Form.Controls(TextBox2).Value
ActiveWorkbook.Save
End Sub
EditAdd gets called on click
I think TextBox_orderid is a name of your textbox control not a variable so you should enclose it with " like:
Cells(3, 19).Value = Order_Details_Form.Controls("TextBox_orderid").Value
Or you can simply use:
Cells(3, 19).Value = Order_Details_Form.TextBox_orderid.Value
Apply this at your other two lines.

VBA breaking while referencing a section in a formula within a loop

I am currently using a loop containing the following condition within a loop.
For Each Cell In Range("A3:A1000").Cells
If Cell.Value = "Existing" And IsEmpty(Cell.Offset(0, 7).Value) Then
Cell.Offset(0, 7).Value = "X"
Cell.Offset(0, 5).Formula = "=VLookup(RC[-1],Customers!R2:T5000,2,FALSE)"
Cell.Offset(0, 6).Formula = "=VLookup(RC[-2],Customers!R2:T5000,3,FALSE)"
End If
Next Cell
Works well with one exception, whenever the formula is appended to a cell, the second value becomes CUSTOMERS!$2:$2:'T5000'. Does anyone have any idea why this is occurring?
You mixed .Formula and .FormulaR1C1 properties and correct syntax for each of these properties. Try with these lines:
Cell.Offset(0,5).FormulaR1C1 = "=VLookup(RC[-1],Customers!R2C18:R5000C20,2,FALSE)"
Cell.Offset(0,6).FormulaR1C1 = "=VLookup(RC[-2],Customers!R2C18:R5000C20,3,FALSE)"

Moving a Do Until function across columns in excel vba

I'm pretty new to VBA and having trouble creating a quick macro used to move blocks of numbers around.
What I am trying to create is a button that when pressed:
Moves the contents of (i,5) to E63
The cells from (i, 16) down to F67:F110
Dependant on whether Row 10 contains "Low" or "High" moves three cells from the set
N106:N109 to the cells (i12:i14) [Where i is the column reference).
The Range sections of code are what accomplish this and they are working fine, the problem I am having is with my Do.Until row and with the reference Column(i)
Does anyone know how this could work? Thanks
UPDATE
So thanks to the help of Siddharth I've been able to fix all but one bit, which is the lines where there is a string in the Range function. The reason I am not using .Formula here but Paste instead is that otherwise all of the cells A12:A14 to Z12:Z14 will equal the same thing which isn't correct. On the other parts that doesn't matter. I am getting a type 13 mismatch error on these lines.
Sub Columntest()
Dim i As Integer
i = 5
Do Until Cells(5, i).Value = ""
If Cells(10, i).Value = "Low" Then
Range("E63").Formula = Cells(5, i)
Range("F67:F110").Formula = Cells(16, i)
Range("O106:O108").Copy
Range("=" & Columns(i) & "12").PasteSpecial Paste:=xlPasteValues
End If
If Cells(10, i).Value = "High" Then
Range("E63").Formula = Cells(5, i)
Range("F67:F110").Formula = Cells(16, i)
Range("N106:N108").Copy
Range(Columns(i) & "12").PasteSpecial Paste:=xlPasteValues
End If
i = i + 1
Loop
End Sub
The type mismatch is because you are trying to concatenate a column object and a string in the range reference:
Range("=" & Columns(i) & "12").PasteSpecial Paste:=xlPasteValues
Try using this instead:
Cells(12, i).PasteSpecial Paste:=xlPasteValues