Cell value from offset range - vba

yellow_cell = ActiveCell.Address
MsgBox (Range(yellow_cell).Value)
implant = yellow_cell.Offset(6, -2).Address
MsgBox (Range(implant).Value)
The first MsgBox works, but the second one doesn't (Run-time error 424, object required).
I've also tried this:
implant = ActiveCell.Offset(6, -2).Address
MsgBox (Range(implant).Value)
And I get a run-time error 1004, Method 'Offset' of object 'Range' failed.
Anyone know what I'm doing wrong? I've unmerged all cells btw.

Here's the proper way to achieve the desired results:
Dim implant As Range, yellow_cell As Range
Set yellow_cell = ActiveCell
MsgBox yellow_cell.Value
Set implant = yellow_cell.Offset(6, -2)
MsgBox implant.Value
Notice: If the active cell is less than two columns away from column A, then this code will result in run-time error 1004, due to the second parameter of the Offset function.

Another way to achieve this is to test whether the column is in fact further right than 2 columns, something like below, this shouldn't cause any errors:
Sub foo()
Dim yellow_cell As String, implant As String
Dim col As Long
yellow_cell = ActiveCell.Address
MsgBox (Range(yellow_cell).Value)
col = ActiveCell.Column
If col > 2 Then
implant = ActiveCell.Offset(6, -2).Address
MsgBox (Range(implant).Value)
Else
MsgBox "Out of Range!", vbInformation, "Error"
End If
End Sub

Related

VLOOKUP generates Run-Time Error '1004'

I want to setup a template that finds data based on pasted data in another worksheet.
Private Sub GoNoGo()
Dim i As Integer
Dim OffInt As Integer
Dim Neg As Integer
Neg = -30
Dim Ret As String
Dim I3 As Cell
Dim FindValue As String
Worksheets("BF59520").Activate
Range("AE3").Activate
i = 3
OffInt = 0
Do Until ActiveCell.Offset(0, Neg).Value = ""
If ActiveCell.Offset(0, -1).Interior.Color = RGB(255, 235, 160) Then
ActiveCell.Offset(1, 0).Activate
i = i + 1
Else
ActiveCell.Value = Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -18), Worksheets("Go No Go").Range("B2:O180"), 4, False)
ActiveCell.Offset(1, 0).Activate
i = i + 1
End If
OffInt = OffInt + 1
Loop
End Sub
When the loop gets to the VLOOKUP Line the code returns an error of Run-Time error '1004':
Unable to get the VLOOKUP property of the worksheetFunction class.
Generally, when you get that error on a Worksheet Function it means the function itself has returned an error. Make sure you're passing it the right values. If you can't guarantee that you'll get a correct value from the function then you can try using On Error like so
On Error Resume Next
Application.WorksheetFunction.VLookup(ActiveCell.Offset(0, -18), Worksheets("Go No Go").Range("B2:O180"), 4, False)
On Error GoTo 0
or you can capture the error in an evaluate statement like so
ActiveCell.Value = Evaluate("=IFERROR(VLOOKUP(" & ActiveCell.Offset(0,-18) & ", 'Go No Go'!B2:O180, 4, FALSE),0)")
The first will result in a no change in the ActiveCell when the vlookup fails, the second allows you to set a default value as the second argument of the 'IFERROR' function.
Hope this helps!

Application VLOOKUP no value

I am inputting data based on a VLOOKUP code but keep getting an error.
For Each Cell In Rng
Cell.Offset(0, 2).Value = Application.WorksheetFunction.VLookup(Cell, Table2, 1, False)
Next
I want Column C to either post the VLOOKUP value or return a message 'Returned Item Not Scanned'. I was using a error handler to do this however I keep getting an error when this runs.
'MyErrorHandler:
' If Err.Number = 1004 Then
' Cell.Offset(0, 2).Value = "Returned Item Not Scanned"
' ElseIf Err.Number = 13 Then
' MsgBox "Incorrect Exceptions Data."
' Else
'
' End If
Error is stating 'Unable to get VLookup Property of the WorksheetFunction Class.
Can anyone help?
Try the piece of code below, I am using Application.VLookup in order to trap the errors.
(When trapping the VLookup errors in this method, you are not getting 1004 for Err.Number)
Option Explicit
Sub VLookup_with_ErrHandling()
Dim Cell As Range
Dim Rng As Range
Dim Table2 As Range
' modify "Table2" range to your needs
Set Table2 = Sheets("Sheet1").Range("A1:C20")
' modify "Rng" range to your needs
Set Rng = Sheets("Sheet2").Range("A1:A10")
For Each Cell In Rng
If Not IsError(Application.VLookup(Cell, Table2, 1, False)) Then
Cell.Offset(0, 2).Value = Application.VLookup(Cell, Table2, 1, False)
Else
Cell.Offset(0, 2).Value = "Returned Item Not Scanned"
End If
Next
End Sub

Look up values in sheet(x) column(x), match to values in sheet(y) column(y), if they match paste row

Dealing with an issue that seems simple enough, but for some reason I cannot get this to work.
I have a data input sheet I am trying to match values across to another sheet, the values are both in column E, and all the values in column E are unique.
The values will always be stored in rows 8 though to 2500.
My code is as below, however is throwing the ever useful 1004 error (Application-Defined or object-defined error), on line
If Sheets("Target Inputs").Range("E" & CStr(LSearchRow)).Value = searchTerm Then
any help would be greatly appreciated:
Sub LOAD_BUID_Lookup()
Dim i As Integer
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim searchTerm As String
On Error GoTo Err_Execute
For i = 8 To 2500
searchTerm = Range("E" & i).Text
If Sheets("Target Inputs").Range("E" & CStr(LSearchRow)).Value = searchTerm Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Sheet2 in next row
Sheets("LOAD").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Target Inputs").Select
End If
Next i
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
LSearchRow is not being set to any value, which means it is 0. This in turn throws the exception, since the row number cannot be 0. And there is no reason whatsoever to convert to string with CStr, since the concatenation casts the entire range parameter to a string anyway.
Usually when comparing two different columns in two different sheet you would see a double loop the first to loop through sheet1 and the second to take every value of sheet1 and loop through sheet2 to find a match. In reading your description I think this is what you want.
Double loops can be time intensive. There is another way, Worksheetfunction.match!!
I also noticed your code selecting/activating sheets multiple times. Typically selecting/activating sheets is not required if you declare and instantiate the variables you need.
Below is an example code I tried to make it as plug and play as possible, but I wasn't sure of the name of the sheet you are looping through. I've tested the code on dummy data and it seems to work, but again I'm not quite positive on the application. I've commented the code to explain as much of the process as possible. Hopefully it helps. Cheers!
Option Explicit 'keeps simple errors from happening
Sub LOAD_BUID_Lookup()
'Declare variables
Dim wb As Workbook
Dim wsInputs As Worksheet
Dim wsTarget As Worksheet
Dim wsLoad As Worksheet
Dim searchTerm As String
Dim matchRng As Range
Dim res
Dim i As Integer
'instantiate variables
Set wb = Application.ThisWorkbook
Set wsInputs = wb.Worksheets("Inputs") 'unsure of the name of this sheet
Set wsTarget = wb.Worksheets("Target Inputs")
Set wsLoad = wb.Worksheets("LOAD")
Set matchRng = wsTarget.Range("E:E")
On Error GoTo Err_Execute
For i = 8 To 2500
searchTerm = wsInputs.Range("E" & i).Text 'can use sheet variable to refer exactly to the sheet you want without selecting
'get match if one exists
On Error Resume Next
res = Application.WorksheetFunction.Match(searchTerm, matchRng, 0) 'will return a row number if there is a match
If Err.Number > 0 Then 'the above command will throw an error if there is no match
'MsgBox "No Match!", vbCritical
Err.Clear ' we clear the error for next time around
On Error GoTo 0 'return to previous error handeling
Else
On Error GoTo 0 'return to previous error handeling
wsInputs.Range("A" & i).EntireRow.Copy Destination:=wsLoad.Range("A" & wsLoad.Range("E50000").End(xlUp).Row + 1) 'gets last row and comes up to last used row ... offset goes one down from that to the next empty row
End If
Next i
'Application.CutCopyMode = False -- there is no need for this when we use "Destination"
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub

Getting Right Cell Name of current Cell VBA excel

I have current cell name like B7 i want to get cell name of the cell right to current cell for example in this case the result will be C7. How can I achieve this
This is what I have tired but its not working
CellName = "B7"
ValueCellName = Right(Range(CellName)).name
Try using offset function:
valuecellname = Range(cellname).Offset(0, 1).Address
Is this what you are trying?
Sub Sample()
Debug.Print GetrightCell("B7")
Debug.Print GetrightCell("XFD7")
Debug.Print GetrightCell("ADIL1234")
End Sub
'~~> Function returns the right cell if there is one!
Function GetrightCell(CellName As String) As String
On Error GoTo Whoa
If Range(CellName).Column <> Columns.Count Then
GetrightCell = Range(CellName).Offset(0, 1).Address
Else
GetrightCell = "There are no more cells to the right of this cell"
End If
Exit Function
Whoa:
GetrightCell = "Invalid Cell Name"
End Function

Using count, counta, countblank functions

I've tried count, counta, and countblank functions for this code, but it doesn't work. My code is:
Sheet1.Activate
If WorksheetFunction.CountBlank(Range(Cells(3, 3), Cells(50, 3))) > 0 Then
MsgBox "First Enter Data!"
Else
...
I want excel to do some calculations if all of the cells in range C3 to C50 are containing a number, and return the msgbox if they aren't.
All the other codes are true. I've checked them several times.
The problem is that even when all of those cells have numbers, the msgbox appears. I've tried many ways, but it keeps going wrong.
Please help me. Thanks a lot.
Edit:
1) if your numbers stored as text, use following code (it change cells format to "number format"):
Dim rng As Range
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("C3:C50")
With rng
.NumberFormat = "0.00"
.Value = .Value
If WorksheetFunction.Count(.Cells) <> .Cells.Count Then
MsgBox "First Enter Data!"
Else
MsgBox "Everything is ok. All cells in range C3:C50 contains numbers"
End If
End With
2) You can also use this one:
Dim c As Range
Dim isAllNumbers As Boolean
isAllNumbers = True
For Each c In ThisWorkbook.Worksheets("Sheet1").Range("C3:C50")
If Not IsNumeric(c) Or c = "" Then
isAllNumbers = False
Exit For
End If
Next
If Not isAllNumbers Then
MsgBox "First Enter Data!"
Else
MsgBox "Everything is ok. All cells in range C3:C50 contains numbers"
End If
You may also want to read this: How to avoid using Select/Active statements