How to resolve error 2042 for vlookup function - excel

I am trying to do a vlookup function with VBA. However, it keeps ending up as 0 due to Error 2042. Did I do anything wrong with the coding? It keeps having error 2042.
Dim a As Integer
Dim x As Integer
Dim y As Integer
y = 2
For x = 17 To 21
a = TextBox1.Value
z = Application.vlookup(a, Sheet7.Range("A:F"), y, False)
If Not IsError(z) Then
Sheet1.Cells(42, x).Value = z
Else
Sheet1.Cells(42, x).Value = 0
End If
y = y + 1
Next x

Related

Copying data from one page to another depending on cell value

This code copies the entire row to another when the word 'ordered' is in a certain column.
However, I need to adapt this code to not copy the entire row for another function but requires only copying columns A:J over into the next sheet but I'm having difficulty achieving this.
Sub MovingOrderedItems()
Dim xRg As Range
Dim xCell As Range
Dim X As Long
Dim Y As Long
Dim Z As Long
X = Worksheets("Engineer-Items to be ordered").UsedRange.Rows.Count
Y = Worksheets("Admin").UsedRange.Rows.Count
If Y = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Admin").UsedRange) = 0 Then Y = 0
End If
Set xRg = Worksheets("Engineer-Items to be ordered").Range("N3:N" & X)
On Error Resume Next
Application.ScreenUpdating = False
For Z = 1 To xRg.Count
If CStr(xRg(Z).Value) = "ordered" Then
xRg(Z).EntireRow.Copy Destination:=Worksheets("Admin").Range("A" & Y + 1)
xRg(Z).EntireRow.Delete
If CStr(xRg(Z).Value) = "ordered" Then
Z = Z - 1
End If
Y = Y + 1
End If
Next
Application.ScreenUpdating = True
End Sub
There's probably a more elegant way to do this, but you can replace
xRg(Z).EntireRow.Copy Destination:=Worksheets("Admin").Range("A" & Y + 1)
With
Range(xRg(Z).EntireRow.Cells(1, 1), xRg(Z).EntireRow.Cells(1, 10)).Copy Destination:=Worksheets("Admin").Range("A" & Y + 1)

end if without block if, though if statements are closed

I'm getting a mysterious 'End If without block If' error in my vba 2013 code. Any insight on what's going wrong?
I get the error with both 'End If's. If I comment them out, it throws a 424 error at me on the second line of either of the 'If' blocks.
Code (in ThisWorkbook):
Sub TryAgain()
Dim ran As Range
Dim strt As Range
Dim X As Long
Application.ScreenUpdating = False
Set strt = Sheet3.Range("A2")
Set ran = Sheet3.Range(strt, strt.End(xlDown))
n = 1
For Each blah In ran:
cause = blah.Offset(0, 11)
X = 0
c = 0
Do Until X = n:
citem = Sheet4.Cells(1, 1).Offset(X, 0)
If cause.Value = citem.Value Then:
citem.Offset(0, 1).Value = citem.Offset(0, 1).Value + 1
c = c + 1
End If
X = X + 1
Loop
If c = 0 Then:
bernerner = Sheet4.Cells(1, 1).Offset(X, 0)
bernerner.Offset(1, 0).Value = cause.Value
bernerner.Offset(1, 1).Value = bernerner.Offset(1, 1).Value + 1
n = n + 1
End If
Next blah
Application.ScreenUpdating = True
End Sub
I don't think you need : in VBA.
For , While and If none of these need a : at the end.

Solving equation using excel-vba, If condition seems to work differently

Trying to solve an equation using excel-vba,
Equation:
xy = 5(x+y)
I use the below vba code to determine the values of x and y and print it in cell A1
Sub equation()
Dim x As Long, y As Long
For x = 1 To 9
For y = 1 To 9
If ((x * y) = (5 * (x + y))) Then
Range("A1") = x & "," & y
End If
Next y
Next x
End Sub
However this code is not working. I guess I am missing something in the IF condition.
I tried the below and this works perfectly,
Sub equation()
Dim x As Long, y As Long
For x = 1 To 9
For y = 1 To 9
If 5 * (x + y) = 45 Then
Range("A1") = x & "," & y
End If
Next y
Next x
End Sub
I even tried with 2 temp variables, but doesn't works,
Sub equation()
Dim x As Long, y As Long, temp1 As Long, temp2 As Long
For x = 1 To 9
For y = 1 To 9
temp1 = x * y
temp2 = 5 * (x + y)
If temp1 = temp2 Then
Range("A1") = x & "," & y
End If
Next y
Next x
End Sub
Can someone tell me what I am doing wrong with the IF condition.
Consider:
xy = 5(x+y)
xy = 5x+5y
xy-5y = 5x
y(x-5) = 5x
y = 5x/(x-5)
Then just plug in values for x and solve for y
Sub qwerty()
Dim x As Long, y As Long
For x = 6 To 13
y = 5 * x / (x - 5)
Cells(x, 1) = x
Cells(x, 2) = y
Next x
End Sub
Pick any values for x, just avoid x = 5.Declare y as Double if you require the fractional part:

Excel 2013 VBA - Runtime error 9: Subscript out of range

I'm new to VBA and did run into a problem I can't seem to find a solution to on my own -> so Your help is much appreciated :).
I'm getting "Run-time error 9: Subscript out of range" when attempting to run following code:
Dim i as Long, l as Long, x as Long, y as Long, z as Long, lastrow as Long
'..some unrelevant code
x = 2
For y = 1 To lastrow Step 2: For i = y To lastrow Step 2: For z = 0 To 1
'..do stuff
If z = 0 Then
Dim datarange As Variant
Dim myvar As Double
Dim comp As Double
Dim lrow As Long
Dim lcol As Long
l = 0
lrow = x
datarange = Range("k" & x & ":" & "ab" & x + 1).Value
For lcol = 11 To 28
myvar = datarange(lrow, lcol)
comp = datarange(lrow + 1, lcol)
If comp > 1 Then
If myvar > 1 Then
l = l + 1
End If
Else: l = l + 1
End If
Next
End If
x = x + 1
Next: Next: Next
'..etc
I got to this point after implementing the code-part with grey background in effort to include following knowledge into my project: http://blogs.office.com/2008/10/03/what-is-the-fastest-way-to-scan-a-large-range-in-excel/ (third point "Use a variant type variable").
Thanks in advance!
If you'll pardon the expression, you've two-stepped out of the bounds of the array:)
The index numbers of the dataRange array are different then their row/column numbers. If you go to VB Editor, View, and the Locals window as you are stepping through the code (F8), you will see that the array is 1 to 2 by 1 to 18.

VBA Runtime error 1004 range of object_global failed

If the cell has a value not equal to 0 then it will add up, X = X +1 so then I can see how many pages I need to print. I'm not sure if this is the best way to do it, but here is what I have so far.
All help is needed! This is just a side project for work, so time is not an issue. I am creating this in excel also.
Sub Find_Pages_to_Print()
Sheets("Invoice").Select
Dim X As Integer
If Not Range("D9").Value = 0 Then
X = X + 1
If Not Range("D41").Value = 0 Then
X = X + 1
If Not Range("D70").Value = 0 Then
X = X + 1
If Not Range("D100").Value = 0 Then
X = X + 1
Else:
X = X + 0
If Not Range("D131").Value = 0 Then
X = X + 1
If Not Range("D161").Value = 0 Then
X = X + 1
If Not Range("191").Value = 0 Then
X = X + 1
Else:
X = X + 0
If Not Range("D221").Value = 0 Then
X = X + 1
End If
End If
End If
End If
End If
End If
End If
End If
MsgBox "Sets to Print: " & X
End Sub
The problem is at line:
If Not Range("191").Value = 0 Then
you probably want
If Not Range("D191").Value = 0 Then

Resources