Application defined or object defined If I am workiing with CSV file as active s
ID: 3560657 • Letter: A
Question
Application defined or object defined
If I am workiing with CSV file as active sheet , the line
LastRow1 = dst.Cells(Cells.Rows.Count, "A").End(xlUp).Row
shows Application defined or object defined error message is coming. But that dst file is excel file.
Please help
Dim wBk As Workbook
'On Error Resume Next
Set wBk = Workbooks("filtercode.xls")
If wBk Is Nothing Then
MsgBox "Please open the worksheet to remove unwanted Account code "
Set wBk = Nothing
Exit Sub
End If
Set dst = Workbooks(["filtercode.xls"]).Sheets("Sheet1")
Set src = ActiveSheet
'last row of sheet1
LastRow = src.Cells(Cells.Rows.Count, "A").End(xlUp).Row
' last row of sheet2
LastRow1 = dst.Cells(Cells.Rows.Count, "A").End(xlUp).Row
'set range
Set MyRange = src.Range("A1:A" & LastRow)
For Each c In MyRange
If WorksheetFunction.countif(dst.Range("A1:A" & LastRow1), c.Value) > 0 Then
c.EntireRow.Hidden = True
End If
Next
End Sub
Some time the active sheet (src) might be CSV file. On such case following line shows error message
Explanation / Answer
*(*(*I'd try qualifying each range -- including cells in the cells.rows.count.
Option Explicit
Sub testme()
Dim wBk As Workbook
Dim dst As Worksheet
Dim src As Worksheet
Dim myRange As Range
Dim myCell As Range
Dim LastRowSrc As Long
Dim LastRowDst As Long
Set wBk = Workbooks("filtercode.xls")
If wBk Is Nothing Then
MsgBox "Please open the worksheet to remove unwanted Account code"
Set wBk = Nothing
Exit Sub
End If
Set dst = Workbooks(["filtercode.xls"]).Sheets("Sheet1")
Set src = ActiveSheet
'last row of sheet1
With src
LastRowSrc = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
' last row of sheet2
With dst
LastRowDst = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'set range
Set myRange = src.Range("A1:A" & LastRowSrc)
For Each myCell In myRange.Cells
If WorksheetFunction.CountIf(dst.Range("A1:A" & LastRowDst), _
myCell.Value) > 0 Then
myCell.EntireRow.Hidden = True
End If
Next myCell
End Sub
Compiled, but untested.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.