Mini-Travis
New Member
- Joined
- Nov 30, 2010
- Messages
- 5
Good Afternoon,
I am having a weird issue with a snipit of my code.
My goal is to be able to copy a worksheet from one workbook to another. This is simple enough but I am running into an issue with it. My activeworkbook has a sheet in it called CR-####. I am opening a workbook in a database and trying to copy the activesheet into the database workbook. The database workbook also has a sheet named CR-####, so I am renaming the one that I add.
Once again this is simple stuff, but the weird part is the code crashes if I don't have the VB editor up. In other words if I open excel and run the macro it crashes, but if I open excel and hit alt F11 and still run the macro as normal it works perfectly fine.
I have isolated the part of the code that fails, but I can't understand why it fails if I do not have VBA up.
Any takers? Also my code may not be the most effective way of doing this, but I have scrached my head at this for hours now.
I am having a weird issue with a snipit of my code.
My goal is to be able to copy a worksheet from one workbook to another. This is simple enough but I am running into an issue with it. My activeworkbook has a sheet in it called CR-####. I am opening a workbook in a database and trying to copy the activesheet into the database workbook. The database workbook also has a sheet named CR-####, so I am renaming the one that I add.
Once again this is simple stuff, but the weird part is the code crashes if I don't have the VB editor up. In other words if I open excel and run the macro it crashes, but if I open excel and hit alt F11 and still run the macro as normal it works perfectly fine.
I have isolated the part of the code that fails, but I can't understand why it fails if I do not have VBA up.
Code:
''This is where I start running into problems
Workbooks(CurrentBook).Activate
Worksheets(CRNumber).Select
Application.DisplayAlerts = False
Filenameit = "Path" & CRWorksheet
Workbooks.Open Filename:=Filenameit, Notify:=False
DataBaseWorkbook = CRWorksheet
DataBaseWorkbook = Left(DataBaseWorkbook, Len(DataBaseWorkbook) - 5)
Count = ActiveWorkbook.Worksheets.Count
ReNameMe = CRNumber & " Update #" & Count
CountNew = Count + 1
OldName = CRNumber & " (2)"
Workbooks(DataBaseWorkbook).Worksheets(CRNumber).Select
Worksheets(CRNumber).Copy Before:=Workbooks(DataBaseWorkbook).Sheets(1)
Workbooks(DataBaseWorkbook).Activate
Workbooks(DataBaseWorkbook).Worksheets(CRNumber).Select
Workbooks(DataBaseWorkbook).Worksheets(OldName).Select
Workbooks(DataBaseWorkbook).Worksheets(OldName).Name = ReNameMe
Workbooks(DataBaseWorkbook).Worksheets(ReNameMe).Select
Cells.Select
Selection.Locked = True
Workbooks(DataBaseWorkbook).Worksheets(ReNameMe).Protect ("Password"), AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True
Workbooks(DataBaseWorkbook).Close (True)
Any takers? Also my code may not be the most effective way of doing this, but I have scrached my head at this for hours now.