copu sheet and close workbook without saving

blueozzie

New Member
Joined
Feb 12, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I'm stuck..

I'm trying to copy one sheet into a new workbook and save this as a variable name ansd then close the original workboo kwithout saving..

Here's where I am and it's not working :(

VBA Code:
Set wb = ThisWorkbook
  
Sheet16.Copy

wb.Activate

Application.DisplayAlerts = False
    ThisWorkbook.Saved = True
wb.Close

filename = accno & ".csv"

    DTAddress = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs DTAddress & filename, FileFormat:=xlCSV
    Application.DisplayAlerts = True

Any help would be appreciated.


Thanks,
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi blueozzie and Welcome to the Board! Not real sure what "accno" is? HTH. Dave
edit forgot the close wb part. Will repost
 
Upvote 0
hiya,

Sorry, accno is a variable number which will be the filename. I would like otigibsl workbook yo be closed and new workbook to stay open..
 
Upvote 0
This seems to work. Dave
Code:
Dim accno As Double

Set wb = ThisWorkbook
Sheets("Sheet16").Copy

accno = "1234"
Filename = CStr(accno) & ".csv"

DTAddress = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
Application.DisplayAlerts = False
wb.SaveAs DTAddress & Filename, FileFormat:=xlCSV
ActiveWorkbook.Close
Application.DisplayAlerts = True
Set DTAddress = Nothing
 
Upvote 0
Hi Dave,

Thanks, this almost worked, it ended up saving the original workbook as accno and not the new workbook but I fiddled around wth it and got it to work. I have DTAddress set as a string so the set DTAddress = nothing threw up an error. I've removed this line so it works, is it important??


Cheers,
 
Upvote 0
Glad you got it sorted out. No need for that line of code if DTAddress is a string. Thanks for posting your outcome. Dave
 
Upvote 1

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,096
Latest member
Anshu121

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