View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Changing the active workbook

Actually your code would cause an error since you can't use FileToOpen as an
argument to worksheets. Also, when you open a workbook, it is the active
workbook.

You should eliminate the line

Workbooks(fileToOpen).Activate


and the just opened workbook will be the active workbook. There is nothing
in your code that would "makes the original workbook active" that I can see.
The only other activate command is for the Worksheets("Custom Report")
which, since it is unqualified should refer to the just opened and active
workbook.
--
Regards,
Tom Ogilvy




"SP" wrote in message
oups.com...
I have a workbook called TicketReport.xls which hold the code, I need
to open a workbook that is sent to me and reformat it.
The code starts off ok, it opens the specified workbook but then it
makes the original workbook active. This is the code I have so far:

Sub CreateReport()
Dim fileToOpen As String

'Choose workbook to open
fileToOpen = Application.GetOpenFilename("Text Files (*.txt),*.txt,
Excel Files (*.xls), *.xls", 2)

Workbooks.Open fileToOpen
Workbooks(fileToOpen).Activate
Worksheets("Custom Report").Activate

'Added MsgBox to verify which Workbook & worksheet are active
MsgBox "The name of the active Workbook is " & ActiveWorkbook.Name
MsgBox "The name of the active Sheet is " & ActiveSheet.Name


'Delete "Category" column
myVar = WorksheetFunction.Match("Category", Worksheets("Custom
Report").Range("A1:G1"), 0)

'Insert "Manager" column between STATUS and CALLER columns
'code to format data ....
End Sub

Thanks in advance.