Make this macro open a workbook?

bpflyr

Board Regular
Joined
Nov 7, 2005
Messages
116
I have this macro (was given to me by you wizards!):

Sub GetData()
Application.ScreenUpdating = False
Windows("BDataEntry.xls").Activate
Sheets("Data Entry").Select
Range("A3:AF20").Copy
Windows("BLogbook.xls").Activate
Sheets("Log Entry").Select
Cells(Rows.Count, 1).End(xlUp)(2, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=True _
, Transpose:=False
With Application
.ScreenUpdating = True
.CutCopyMode = False
End With
End Sub


Is there a way that it can open BDataEntry automatically. Right now it appears that if BDataEntry is not already open and I try to run the macro, I get a "subscript out of range" error message. If BDataEntry is open all works great. It would be cool though if the macro would open it for me. Is there a way?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi

You could build your own function to see if the workbook is open and action accordingly

Code:
Function IsOpen(wb)
  IsOpen = False
  For Each ce In Workbooks
    If ce.Name = wb Then
     IsOpen = True
    End If
 Next ce
End Function

Then change

Windows("BDataEntry.xls").Activate

to

if isopen("BDataEntry.xls") then
Windows("BDataEntry.xls").Activate
else
workbooks.open("c:\temp\BDataEntry.xls")
end if

Change the path name as required.


HTH

Tony
 
Upvote 0

Forum statistics

Threads
1,214,363
Messages
6,119,086
Members
448,868
Latest member
Elcassell

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