Code to copy worksheet
Thank you all for your help.
I also found it strange to be advised to not use ActiveWorkbook, Select etc...
Perhaps I haven't understood and explained the situation properly - here is
the previous post and reply:
______________
Problem previously posted:
I have Buttons on worksheets that are assigned to macros in module 1 within
the current open workbook.
The macro copies the active worksheet, formats it (copy/paste values to
remove formulas, delete buttons not required), and moves it out into a
separate workbook.
The 1st time I click on the button it works great.
The 2nd time I click - the buttons have been re-assigned to Book1!"mba"
which doesn't have any macros
_____________________
Joel responded with: (although he was confused whether I was opening a
workbook - it's already open)
Excel doesn't delete the VBA project when a workbook is closed so the
project is still active when you run the macro a 2nd time. You always want to
run the macro from the first workbook that was opened. You should always
refere to the main workbook using THISWORKBOOK.
Try to Avoid using Activeworkbook. When you open a workbook use
Set bk = workbooks.open(filename:="c:\temp\book1.xls")
Then use bk to reference the new workbook. for some reason if you open a
text file as a workbook the statement above doesn't work. So I immediately
after opening the workbook use a set statement to make a variable equal to
the active workbook
Set bk = activeworkbook
A common problem with VBA is that the focus shifts from one object to
another without you knowing it is happening. for example when you open a
workbook the focus changes to the workbook that you opened to one of the
sheet of the workbook (which ever sheet was the active sheet when the
workbook was closed).
Without seeing your code I can't tell how to fix your problem. but is is
good practive to avoid usiong the following methods:
1) ActiveSheet
2) ActiveCell
3) select
4) Selection
With VBA it is inconsistenet and some commands only work with the above
methods so that is why I say Avoid.
_________________
Here is the original code:
Sub copyplan()
ActiveSheet.Copy After:=Sheets(Sheets.Count)
Application.Goto Reference:="PlanCPTrange"
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.Goto Reference:="GRPpot"
Selection.Copy
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
ActiveSheet.Move
ActiveSheet.Buttons.Select
Selection.Delete
Columns("C").Select
Cells.Find(What:=".", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False) _
.Activate
ActiveCell.Replace What:=".", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
End Sub
Your help with fixing my problem would be greatly appreciated.
--
Thank for your help
BeSmart
|