Close one of 2 workbooks open at the end of a macro

JOSTERBAUER

Board Regular
Joined
Jan 17, 2005
Messages
101
Here is the code I have wrote. The only thing that I want it to finish with, is that the macro close the worksheet under the "invoice.xls" I just opened up. If you have any other suggestions please let me know.


Sub invoice()

Dim x As String
Dim cell As String
Dim counter As Integer
Dim Invoicenum As String
Dim Name As String
Dim amount As String

Invoicenum = Range("D5")
Name = Range("A10")
amount = Range("d35")


If Name <> "" Then

Workbooks.Open Filename:= _
"C:\PDF FILES\INVOICE TRACKER\Invoice tracker.Xls"
tracker = ActiveWorkbook.Name
Workbooks(tracker).Activate

Range("A2").Activate

For counter = 1 To 200
If "" = ActiveCell.Value Then
ActiveCell.Value = Invoicenum
cell = "A" & ActiveCell.Row
Range(cell).Offset(0, 1).Value = Name
Range(cell).Offset(0, 2).Value = Date
Range(cell).Offset(0, 7).Value = amount
GoTo escape1
Else: cell = "A" & ActiveCell.Row + 1
Range(cell).Activate
End If
Next
escape1:
End If

ActiveWorkbook.Close True

ChDir "C:\PDF FILES\INVOICE TRACKER\INVOICES"

ActiveWorkbook.SaveAs Range("D5")
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

Workbooks.Open Filename:= _
"C:\PDF FILES\INVOICE TRACKER\Invoice.Xls"

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Do I understand you correctly, all you want is to close Invoice.xls? If so just add this line:
Windows("Invoice.xls").Close

JPM
 
Upvote 0
I see, there are many ways to accomplish that, one of them is to add this line between the last two lines (printing the sheets and open Invoice.xls):

TheFile = ActiveWorkbook.Name

and then add at the end:
Windows(TheFile).Close

JPM
 
Upvote 0
Yes, That is what I am trying to accomplish, but when I add that in the spot you are telling me to, it comes up with a compile error. Am I doing something wrong?
 
Upvote 0
I just tried out the macro myself and it worked just fine. The only thing I can think off would be if the directory address (C:\PDF FILES\INVOICE TRACKER\INVOICES) is not correct.

JPM
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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