Object not found error. How it can be solved? - excel

This code is producing an "Object not found" error.
Sub Button86_Click()
Dim Y As Integer
Dim i As Integer
Dim LastRow As Long
Y = 2
Worksheets("Abnormal").Activate
With ActiveSheet
LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
End With
Sheets("Abnormal").Rows(1).Copy Destination:=Sheets("Ab_IT").Rows(1)
For i = 2 To LastRow
If Abnormal.Cells(i, 11).Value = "IT" Then
Sheets("Abnormal").Rows(i).Copy Destination:=Sheets("Ab_IT").Rows(Y)
Y = Y + 1
End If
Next i
Worksheets("Ab_IT").Activate
With ActiveSheet.UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
Worksheets("Ab_IT").Columns("A:J").AutoFit
End Sub
** Error line - If Abnormal.Cells(i, 11).Value = "IT" Then
Details - There are two sheets. Abnormal and Ab_IT.
In Abnormal sheet, there is one column(11),which sometimes contain "IT"
I am trying to copy all the rows, which contain IT to another sheet Ab_IT.
But getting an error object not defined.

I don't see the variable 'Abnormal' defined prior to this line:
If Abnormal.Cells(i, 11).Value = "IT" Then
Maybe you meant:
If Sheets("Abnormal").Cells(i, 11).Value = "IT" Then

Related

Why do I get the "type mismatch" error when running the macro?

It's my first time doing VBA Macro and I'm having a hard time understanding the problem.
I'm trying to filter and color cells with specific values but when I try running the code it says 'Type mismatch'.
Dim count, i As Long
Dim ws As Worksheet
Dim count, i As Long
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
count = ws.Cells(Rows.count, "E").End(xlUp).Row
i = 2
Do While i <= count
If Cells(i, 5).Value = "#N/A" _
Or Cells(i, 5).Value = "#Ref" _
Or Cells(i, 5).Value = "Null" _
Or Cells(i, 5).Value = "" _
Or Cells(i, 5).Value = "#DIV/0!" _
Or Cells(i, 5).Value = "" _
Or Cells(i, 5).Value Like "*-*" Then
Cells(i, 5).Interior.Color = RGB(38, 201, 218)
End If
i = i + 1
Loop
ws.Range("E1").AutoFilter Field:=5, Criteria1:=RGB(38, 201, 218), Operator:=xlFilterCellColor
And when I click the debug it highlights the If statements. Is there a way to solve this or is there a better way to filter these values while highlighting them in VBA?
Not really an answer, more of a expanded comment.
If IsError(Cells(i, 5)) Then
Cells(i, 5).Interior.Color = RGB(0, 0, 255)
ElseIf Cells(i, 5).Value = "" Then
Cells(i, 5).Interior.Color = RGB(0, 0, 255)
Else
Cells(i, 5).Interior.Color = xlNone
End If
Also, this to sift the errors https://learn.microsoft.com/en-us/office/vba/excel/concepts/cells-and-ranges/cell-error-values
First problem: If your cell contain an error, it doesn't contain the string "#N/A" or "#Ref", it contains a special value. What you see is only a visual representation of that error. If you want to check for an error within Excel, you should use the function IsError. That would lead to (wait, don't use that!):
If isError(Cells(i, 5).Value)
Or Cells(i, 5).Value = "Null" _
Or Cells(i, 5).Value = "" _
Or Cells(i, 5).Value Like "*-*" Then
Second problem: In VBA, there is no optimization for a conditional statement, VBA will always evaluate all parts. Your If-statement contains several conditions, combined with Or. While other programming languages quit evaluating when one condition makes the whole expression true, VBA will continue to evaluate all conditions.
Now if you have an error in a cell and you would use the code above, you will still get a type mismatch error: You cannot compare an error with a string. The condition isError(Cells(i, 5).Value) will get True, but VBA will continue to compare the cell content with strings and that gives you the mismatch. You need a way to split your If-statement.
Some more remarks: You are assigning the worksheet you want to work with to variable ws, but you are not using it. You will need to qualify every single usage of Cells (write ws.Cells(i, 5), else VBA will assume you are working with the Active Sheet, and that may or may not be Sheet1. Usually, this is done with a With-statement (note all the leading dots).
Your declaration statement is flawed (a common mistake in VBA), you will need to specify the type for every variable. In your case, Count will be of type Variant, not Long. No problem here, but in other cases it is, so make it a habit to declare all variables correctly.
You should use a For-Loop rather than a Do While.
Dim count As Long, i As Long
With ws
count = .Cells(.Rows.count, "E").End(xlUp).Row
For i = 2 to count
Dim markCell as boolean
If isError(.Cells(i, 5).Value) Then
markCell = True
ElseIf .Cells(i, 5) = "Null" _
Or .Cells(i, 5).Value = "" _
Or .Cells(i, 5).Value Like "*-*" Then
markCell = True
Else
markCell = False
End If
If markCell Then
.Cells(i, 5).Interior.Color = RGB(38, 201, 218)
End If
Next i
End With
If you want to check for specific errors you first need to check if there are errors with IsError. You cannot check for an error and a value in one condition:
Do While i <= count
Dim Condition As Boolean
Condition = False ' initialize when in a loop!
If IsError(Cells(i, 5).Value) Then
If Cells(i, 5).Value = CVErr(xlErrNA) _
Or Cells(i, 5).Value = CVErr(xlErrRef) _
Or Cells(i, 5).Value = CVErr(xlErrNull) _
Or Cells(i, 5).Value = CVErr(xlErrDiv0) Then
Condition = True
End If
ElseIf Cells(i, 5).Value = "" Or Cells(i, 5).Value Like "*-*" Then
Condition = True
End If
If Condition = True Then
Cells(i, 5).Interior.Color = RGB(38, 201, 218)
End If
Loop
Filter By Color
Sub FilterByColor()
Const wsName As String = "Sheet1"
Const Col As String = "E"
Dim FilterColor As Long: FilterColor = RGB(38, 201, 218)
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
If ws.FilterMode Then ws.ShowAllData ' remove any filters
Dim lRow As Long: lRow = ws.Cells(ws.Rows.count, Col).End(xlUp).Row
Dim rgData As Range ' data range; no header
Set rgData = ws.Range(ws.Cells(2, Col), ws.Cells(lRow, Col))
rgData.Interior.Color = xlNone ' remove all colors
Dim rgColor As Range ' the combined range to be colored
Dim DataCell As Range ' each cell of the data range
Dim cString As String
Dim DoColor As Boolean
For Each DataCell In rgData.Cells
If IsError(DataCell) Then ' error value
DoColor = True
Else
cString = CStr(DataCell.Value)
If Len(cString) = 0 Then ' blank
DoColor = True
Else
If InStr(1, cString, "-") > 0 Then ' contains a minus ('-')
DoColor = True
End If
End If
End If
If DoColor Then
If rgColor Is Nothing Then ' combine cells into a range
Set rgColor = DataCell
Else
Set rgColor = Union(rgColor, DataCell)
End If
DoColor = False ' don't forget to reset
End If
Next DataCell
If rgColor Is Nothing Then Exit Sub
rgColor.Interior.Color = FilterColor ' apply color in one go
Dim rgTable As Range ' table range; header included
Set rgTable = ws.Range(ws.Cells(1, Col), ws.Cells(lRow, Col))
rgTable.AutoFilter 1, FilterColor, xlFilterCellColor
' To delete the rows, you could continue with e.g.:
' rgData.SpecialCells(xlCellTypeVisible).EntireRow.Delete
' ws.AutoFilterMode = False ' remove 'AutoFilter'
End Sub

Iterate if statement in vba

I am new to VB and facing some issues to iterate through. below is my code and i want to iterate the if statement more than once.
Sub Refresh_Data()
On Error Resume Next
A = Worksheets("DATA").Cells(Rows.Count, 4).End(xlUp).Row
Dim x As String
x = 9550
For i = 1 To A
If Worksheets("DATA").Cells(i, 1).Value = x Then
Worksheets("DATA").Rows(i).Copy
Worksheets(x).Activate
B = Worksheets(x).Cells(Rows.Count, 4).End(xlUp).Row
Worksheets(x).Cells(B + 1, 1).Select
ActiveSheet.Paste
Worksheets("DATA").Activate
x = x + 50
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("DATA").Cells(1, 1).Select
End Sub
You are clearly making some rookie mistakes in your code, let me make some first corrections, and from there please tell us if you still have problems and in case yes, which ones:
Sub Refresh_Data()
' On Error Resume Next (do not use this, it is masking errors instead of solving them)
Dim A As Long
Dim B As Long ' not only A, also B
Dim x As Long ' x is not a string, but a number
Dim i As Long ' you forgot about i
A = Worksheets("DATA").Cells(Rows.Count, 4).End(xlUp).Row
x = 9550
For i = 1 To A
If Worksheets("DATA").Cells(i, 1).Value = x Then
Worksheets("DATA").Rows(i).Copy
Worksheets(x).Activate
B = Worksheets(x).Cells(Rows.Count, 4).End(xlUp).Row
Worksheets(x).Cells(B + 1, 1).Paste ' you can paste here directly, no reason to select first.
Worksheets("DATA").Activate
x = x + 50
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("DATA").Cells(1, 1).Select
End Sub

Run Time Error: 13 showing while looking through #N/A string

I'm trying to run the below code to find whether the A column has keyword "SUP ID" or not. Any instance of SUP ID would be updated with number 1 on adjacent column and rest all would go as 0. However while finding the #N/A word, getting error Run-Time Error '13': Type Mismatch
Sub m()
For i = 2 To 10
If Cells(i, 1).Value = "SUP ID" Then
Cells(i, 2).Value = 1
Else
Cells(i, 2).Value = 0
End If
Next i
End Sub
You will need to check for the error before the existing if:
Sub m()
With ActiveSheet 'Better to use actual sheet: WorkSheets("Sheet1")
'load with all `0`
.range("B2:B10").Value = 0
For i = 2 To 10
If Not IsError(.Cells(i, 1)) Then
If .Cells(i, 1).Value = "SUP ID" Then
.Cells(i, 2).Value = 1
End If
End If
Next i
End with
End Sub
With only 9 cells to check the above will run quickly, but as the range increases the number of times that vba references the worksheet will slow it down.
You can use Arrays to speed it up.
Sub m()
With ActiveSheet 'Better to use actual sheet: WorkSheets("Sheet1")
Dim inputArr() As Variant
inputArr = .Range("A2:A10").Value
'Default of Integer is `0`
Dim outputArr() As Integer
ReDim outputArr(1 To UBound(inputArr, 1), 1 To 1) As Integer
For i = LBound(inputArr, 1) To UBound(inputArr, 1)
If Not IsError(inputArr(i, 1)) Then
If inputArr(i, 1) = "SUP ID" Then
outputArr(i, 1) = 1
End If
End If
Next i
.Range("B2").Resize(UBound(inputArr, 1)).Value = outputArr
End With
End Sub

How to assign variable to value from loop VBA

my newbie question:
I would need to define variable from values gathered by loop.
I have column of datas, and I need to filter those data and copy to another new sheet named with variable.
Problem is, I cannot get variable from loop.
Is it possible?
Example: variable is "hu"
i = 2
Do Until IsEmpty(Cells(i, 9))
**hu** = Cells(i, 9).Value
i = i + 1
Loop
ActiveWorkbook.Worksheets.Add
ActiveSheet.Name = **hu**
Worksheets("Sheet1").Range("A1:I1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$I$1").AutoFilter Field:=9, Criteria1:=**hu**
With ActiveSheet.AutoFilter.Range
On Error Resume Next
Set rng2 = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
If rng2 Is Nothing Then
MsgBox "No data to copy"
Else
Set rng = ActiveSheet.AutoFilter.Range
rng.Offset(1, 0).Resize(rng.Rows.Count - 1).Copy _
Destination:=Worksheets("Comparison2").Range("A2")
End If
ActiveSheet.ShowAllData
Thanks!
You need to include a subroutine call within your loop to use the variable.
Something like this ..
Option Explicit
Sub do_it()
Dim hu As String
Dim i As Integer
i = 2
Cells(i, 9).Select
Do Until IsEmpty(Cells(i, 9))
hu = Cells(i, 9).Value
get_worksheet (hu)
i = i + 1
Loop
End Sub
Sub get_worksheet(name)
ActiveWorkbook.Worksheets.Add
..etc
end sub
With data like:
In column I, this is a way to get the last item before the empty:
Sub marine()
i = 2
Do Until Cells(i, 9).Value = ""
hu = Cells(i, 9).Value
i = i + 1
Loop
MsgBox hu
End Sub
Ok I googled and found out the problem, error message was due to "This error happens also when a Sub is called the same as variable (i.e. in one Sub you have for loop with iterator "a", whilst another Sub is called "a")."
I changed the name of variable and code works.
Thanks to everyone

getting "application-defined or object defined" error

i wrote the below code to check two cells in a row if both have 0 in them then it should hide that row
For Each rr In Range("I17:I28") 'rr is defined as range
If rr = 0 Then
If ActiveCell.Offset(0, -3).Range(rr).Value = 0 Then ' getting error in this line
ActiveCell.EntireRow.Select
Selection.EntireRow.Hidden = True
Range("B16").Select
Else
ActiveCell.EntireRow.Select
Selection.EntireRow.Hidden = False
Range("B16").Select
End If
End If
Next
but i am getting "application-defined or object defined" error
i tried replacing range(rr) to simply rr but still it is giving "Object dosen't support this property or method" error
please help on this....
Just check the below code whether it works fine for you... It hides first row if there are two consecutive 0's. if there are three consecutive zeros it hide first two and so on...
Sub CheckZeros()
Dim rng As Range
Set rng = ActiveSheet.Range("I17:I28")
For i = 1 To rng.Count
If rng.Cells(i, 1) = rng.Cells(i + 1, 1) And rng.Cells(i, 1) = 0 Then
rng.Cells(i, "I").EntireRow.Hidden = True
End If
Next
End Sub

Resources