VBA doesn't close

jas1980

New Member
Joined
Oct 14, 2016
Messages
39
Office Version
  1. 2019
Platform
  1. Windows
In the following code (this is at the end of the macro) the opened csv doesn't close when the code above Else runs, but when the code below Else runs it does, the csv is saved because when I close there is no request to save and all data remains.

Is there a way to change this so it saves and closes without me having to after this code finishes?


filetoopen = ThisWorkbook.Worksheets("CSV COLLECT").Range("V3")
Workbooks.Open fileName:=filetoopen
If Not IsEmpty(Range("A1")) = True Then
ThisWorkbook.Worksheets("COLLECT HIS").Range("C9").Copy
Range("A1").End(xlDown).Offset(1).PasteSpecial xlValues
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CalculateFullRebuild
For Each wsheet In ThisWorkbook.Worksheets
wsheet.PROTECT Password:="TRADE"
Next wsheet
ThisWorkbook.Worksheets("COLLECT HIS").Activate
Application.ScreenUpdating = True
Application.DisplayStatusBar = True

Else
End If


filetoopen = ThisWorkbook.Worksheets("CSV COLLECT").Range("V3")
ThisWorkbook.Worksheets("COLLECT HIS").Range("C8:C9").Copy
Workbooks.Open fileName:=filetoopen
If IsEmpty(Range("A1")) = True Then
Range("A1").PasteSpecial xlValues
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CalculateFullRebuild
For Each wsheet In ThisWorkbook.Worksheets
wsheet.PROTECT Password:="TRADE"
Next wsheet
Worksheets("COLLECT HIS").Activate
Application.ScreenUpdating = True
Application.DisplayStatusBar = True





End If
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
If A1 is empty then the first code will jump to the Else then End If, you need to close the workbook in the Else to cover that
VBA Code:
filetoopen = ThisWorkbook.Worksheets("CSV COLLECT").Range("V3")
Workbooks.Open Filename:=filetoopen
If Not IsEmpty(Range("A1")) = True Then
ThisWorkbook.Worksheets("COLLECT HIS").Range("C9").Copy
Range("A1").End(xlDown).Offset(1).PasteSpecial xlValues
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CalculateFullRebuild
For Each wsheet In ThisWorkbook.Worksheets
wsheet.Protect Password:="TRADE"
Next wsheet
ThisWorkbook.Worksheets("COLLECT HIS").Activate
Application.ScreenUpdating = True
Application.DisplayStatusBar = True

Else
ActiveWorkbook.Close

End If
 
Upvote 0
If A1 is empty then the first code will jump to the Else then End If, you need to close the workbook in the Else to cover that
VBA Code:
filetoopen = ThisWorkbook.Worksheets("CSV COLLECT").Range("V3")
Workbooks.Open Filename:=filetoopen
If Not IsEmpty(Range("A1")) = True Then
ThisWorkbook.Worksheets("COLLECT HIS").Range("C9").Copy
Range("A1").End(xlDown).Offset(1).PasteSpecial xlValues
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CalculateFullRebuild
For Each wsheet In ThisWorkbook.Worksheets
wsheet.Protect Password:="TRADE"
Next wsheet
ThisWorkbook.Worksheets("COLLECT HIS").Activate
Application.ScreenUpdating = True
Application.DisplayStatusBar = True

Else
ActiveWorkbook.Close

End If

[/CODE
[/QUOTE]
Thanks for the reply, the current code below else closes the workbook but the code above doesn't, if A1 is empty it needs to copy and paste C8 & C9 into A1 & A2 but, if A1 already contains data it needs to copy C9 into the last empty cell in column A
 
Upvote 0
Thanks for the reply, the current code below else closes the workbook but the code above doesn't, if A1 is empty it needs to copy and paste C8 & C9 into A1 & A2 but, if A1 already contains data it needs to copy C9 into the last empty cell in column A.
 
Last edited by a moderator:
Upvote 0
Please do not multi quote posts, it just causes a mess. Simply type your reply into the Reply box at the bottom of the thread.

Rather than running the code separately just use
VBA Code:
filetoopen = ThisWorkbook.Worksheets("CSV COLLECT").Range("V3")
Workbooks.Open Filename:=filetoopen
If IsEmpty(Range("A1")) = True Then
   ThisWorkbook.Worksheets("COLLECT HIS").Range("C8:C9").Copy
   Range("A1").PasteSpecial xlValues
Else
   ThisWorkbook.Worksheets("COLLECT HIS").Range("C9").Copy
   Range("A1").End(xlDown).Offset(1).PasteSpecial xlValues
End If
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CalculateFullRebuild
For Each wsheet In ThisWorkbook.Worksheets
   wsheet.Protect Password:="TRADE"
Next wsheet
Worksheets("COLLECT HIS").Activate
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
 
Upvote 0
Solution
Thank you for this, I will add it now. This way has defiantly condensed the code a lot.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Have used this now and it works brilliantly, I have also removed lines from the start of the code and it runs quicker. Thank you
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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