Error using the instruction on error goto

jongova

New Member
Joined
Dec 6, 2012
Messages
9
I have the following code that is causing me the error "Object Variable or With block variable not set"


The code roughly what it does is: it opens a file, search for a word, when find cut and paste a range of cells in another file.


When he returns to find and not find the word I get an error, for which we add the instruction "On Error GoTo"


But it does not work, if someone could help me thank you.


regards
jongova

Sub AbrirBuscar()
'
Dim sPath As String, sName As String
Dim bk As Workbook
On Error GoTo CerrarArchivo
sPath = "D:\Documents\SQ\Licitaciones\2013 MegaLicitacion\TEST\"
sName = Dir(sPath & "*.xls")
Do While sName <> ""
Set bk = Workbooks.Open(sPath & sName)

Cells.Find(what:="BACAL-515 (DES)", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(-3, 0).Range(Cells(1, 1), Cells(62, 11)).Select
Selection.Cut
Windows("Book1").Activate
ActiveSheet.Paste
ActiveCell.Offset(62, 0).Activate
Workbooks(2).Activate


Cells.Find(what:="BACAL-515 (DES)", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(-3, 0).Range(Cells(1, 1), Cells(62, 11)).Select
Selection.Cut
Windows("Book1").Activate
ActiveSheet.Paste
ActiveCell.Offset(62, 0).Activate

CerrarArchivo:
bk.Close Savechanges:=False
sName = Dir()


Loop


End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
It would probably be better not to rely on On Error Goto.

Instead try dealing with any errors in the code.

Perhaps with something like this.
Code:
Option Explicit

Sub AbrirBuscar()
Dim sPath As String, sName As String
Dim bk As Workbook
Dim rngFnd As Range
Dim rngSrc As Range
Dim rngDst As Range

    Set rngDst = ActiveCell

    sPath = "D:\Documents\SQ\Licitaciones\2013 MegaLicitacion\TEST\"
    sName = Dir(sPath & "*.xls")

    Do While sName <> ""

        Set bk = Workbooks.Open(sPath & sName)

        Set rngFnd = bk.ActiveSheet.Cells.Find(What:="BACAL-515 (DES)", After:=ActiveCell, LookIn:= _
                                               xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
                                               xlNext, MatchCase:=False, SearchFormat:=False)
        If Not rngFnd Is Nothing Then
            Set rngSrc = rngFnd.Offset(-3).Resize(62, 11)

            rngSrc.Copy rngDst

            Set rngDst = rngDst.Offset(62, 0)

            Set rngFnd = bk.ActiveSheet.Cells.Find(What:="BACAL-515 (DES)", After:=ActiveCell, LookIn:= _
                                                   xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
                                                   xlNext, MatchCase:=False, SearchFormat:=False)
            If Not rngFnd Is Nothing Then
                Set rngSrc = rngFnd.Offset(-3).Resize(62, 11)

                rngSrc.Copy rngDst

                Set rngDst = rngDst.Offset(62, 0)
            End If
        End If

        bk.Close Savechanges:=False
        sName = Dir()

    Loop

End Sub
 
Upvote 0
Norie,

Thanks for your reply.

I was using the cut function because when the code looks for the second time, he takes the same range that already he copy.


When I wanted to change the copy function by cut function in your code I get an error.
 
Upvote 0
Andrew, sorry for my ignorance, but where exactly should put the instruction "on error goto"

thanks
 
Upvote 0
jongova

Did you try the code I posted without changing anything?

If it doesn't work try clearing the cells before the next Find.
Code:
 If Not rngFnd Is Nothing Then
            Set rngSrc = rngFnd.Offset(-3).Resize(62, 11)

            rngSrc.Copy rngDst

            rngSrc.Clear

            Set rngDst = rngDst.Offset(62, 0)

            Set rngFnd = bk.ActiveSheet.Cells.Find(What:="BACAL-515 (DES)", After:=ActiveCell, LookIn:= _
                                                   xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
                                                   xlNext, MatchCase:=False, SearchFormat:=False)
            If Not rngFnd Is Nothing Then
                Set rngSrc = rngFnd.Offset(-3).Resize(62, 11)

                rngSrc.Copy rngDst

                Set rngDst = rngDst.Offset(62, 0)
            End If
        End If
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top