Please check my VBA CODE; I have to be sure I'm doing it correct. Thanks for helping!

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi Everyone!
My code is working perfect i guess for now but I have to make sure things are placed accordingly like error handling etc....as i dont want problem to occur latter.

Thanks again!
Pedie;)


Code:
Sub try12()
Application.ScreenUpdating = False
Dim wb As Workbook, MyFile As String
Dim sh As String
sh = Sheet1.Range("B1").Value
    MyFile = "D:\Book2.xlsm"
    On Error Resume Next
        Set wb = Workbooks.Open(Filename:=MyFile, Password:="trytry")
              On Error GoTo 0
    If wb Is Nothing Then MsgBox "Error.....": Exit Sub
    Windows("Book2.xlsm").Activate
    [COLOR=blue]On Error GoTo ErrHandl[/COLOR]
    Worksheets(sh).Visible = xlSheetVisible
    ActiveSheet.Activate
    Sheet1.Range("A51:K51").Copy
    Windows("Book2.xlsm").Activate
    Range("A1").Select
    ActiveSheet.Paste
    Worksheets(sh).Visible = xlSheetVeryHidden
    wb.Close savechanges:=True
    Application.CutCopyMode = False
    ActiveSheet.Range("A3").Select
    MsgBox "Done"
[COLOR=blue]    Exit Sub[/COLOR]
[COLOR=blue]ErrHandl:[/COLOR]
[COLOR=blue]    MsgBox "Neee", vbInformation[/COLOR]
    [COLOR=blue]wb.Close savechanges:=False[/COLOR]
Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Untested but perhaps

Code:
Sub try12()
Dim wb As Workbook, MyFile As String
Dim sh As String
Application.ScreenUpdating = False
sh = Sheet1.Range("B1").Value
MyFile = "D:\Book2.xlsm"
On Error Resume Next
Set wb = Workbooks.Open(Filename:=MyFile, Password:="trytry")
On Error GoTo 0
If wb Is Nothing Then MsgBox "Error.....": Exit Sub
On Error GoTo ErrHandl
Worksheets(sh).Visible = xlSheetVisible
Worksheets(sh).Range("A51:K51").Copy Destination:=Workbooks("Book2.xlsm").Sheets("Sheet1").Range("A1")
Worksheets(sh).Visible = xlSheetVeryHidden
wb.Close savechanges:=True
Application.CutCopyMode = False
MsgBox "Done"
Exit Sub
Application.ScreenUpdating = True
ErrHandl:
MsgBox "Pedie was here", vbInformation
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hello Peter

Shouldnt that be.

Code:
MsgBox "Done"
Application.ScreenUpdating = True
Exit Sub
ErrHandl:

Or the sub could end, with ScreenUpdating set to False.
 
Upvote 0
Hello Peter

Shouldnt that be.

Code:
MsgBox "Done"
Application.ScreenUpdating = True
Exit Sub
ErrHandl:[

/CODE]Or the sub could end, with ScreenUpdating set to False.[/QUOTE]Yes, you are correct.
 
Upvote 0
Hi Vog, didnt know your name was Peter:), Brian, thanks for letting me know that...

Anyways thanks alot for helping. Yesterday there was blackout here and hence no NET....;). Thanks again .... have a great day ahead!

Pedie
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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