My Assistant
![]() ![]() |
|
|
Apr 9 2012, 12:50 PM
Post
#1
|
|
|
UtterAccess Enthusiast Posts: 61 |
I have the following codes which I run directly in Excel to delete links, delete sheets and save a new email version of the workbook. I would like to run this code from Access as part of my regular refresh batch, instead of Excel, so I can keep all my Excel files macro-free. How do I need to modify the code to do this? Muchos Gracias!
CODE Sub DeleteLinks() Dim Ws As Worksheet, FirstSheet As Worksheet Set FirstSheet = ActiveSheet Application.ScreenUpdating = False For Each Ws In Worksheets Ws.Activate 'Remove formulas & external links With Cells .Select .Copy Selection.PasteSpecial Paste:=xlValues Application.CutCopyMode = False End With 'Remove Hyperlinks Cells.Hyperlinks.Delete [A1].Select Next FirstSheet.Activate End Sub CODE Sub DeleteSheet() Application.DisplayAlerts = False Sheets("SheetName1").Delete Sheets("SheetName2").Delete Sheets("SheetName3").Delete Application.DisplayAlerts = True End Sub CODE Sub MailVersion()
Dim FileExtStr As String Dim FileFormatNum As Long Dim TempFilePath As String Dim TempFileName As String Dim NewFileName As String With Application .ScreenUpdating = False .EnableEvents = False End With FileExtStr = ".xlsx": FileFormatNum = 51 TempFilePath = "C:\autoemailfiles\" TempFileName = Range("dashboard").Value NewFileName = TempFileName & " " & Format$(Date, "mm-dd-yyyy") With ActiveWorkbook .SaveAs TempFilePath & NewFileName, FileFormat:=FileFormatNum .Close SaveChanges:=False End With With Application .ScreenUpdating = True .EnableEvents = True End With End Sub |
|
|
|
Apr 9 2012, 01:22 PM
Post
#2
|
|
|
UtterAccess Enthusiast Posts: 67 From: chained to desk |
You'll need to set a reference to the Excel Object library I believe. Then you can create an Excel object in Access VBA. Here is some "air code":
Dim xlsObj As Object Dim xlsRange As Excel.Range Dim xlsSheet As Excel.Sheet Set xlsObj = CreateObject("Excel.Application") xlsObj.Workbooks.Open "[path to your workbook]" With xlsObj Set xlsSheet = .Activesheet etc... This post has been edited by thegeek: Apr 9 2012, 01:22 PM |
|
|
|
Apr 9 2012, 01:24 PM
Post
#3
|
|
|
UtterAccess VIP Posts: 8,140 From: CT |
here is an example of how to do your deletesheet.
CODE Set oApp = CreateObject("Excel.Application")
oApp.Visible = False oApp.Workbooks.Open FileName:=strfilename oApp.UserControl = True oApp.Application.DisplayAlerts = False oApp.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select oApp.ActiveWindow.SelectedSheets.Delete |
|
|
|
Apr 9 2012, 01:31 PM
Post
#4
|
|
|
UtterAccess Enthusiast Posts: 61 |
Thank you. I love examples!
|
|
|
|
Apr 9 2012, 03:23 PM
Post
#5
|
|
|
UtterAccess Enthusiast Posts: 61 |
Any "example" for how to do the deletelinks() sub? (IMG:style_emoticons/default/iconfused.gif) I was able to get some working code to copy/paste in the first worksheet. But I can't get it to loop through all the worksheets in the workbook.
CODE Sub DeleteLinks()
Dim Ws As Worksheet, FirstSheet As Worksheet Set FirstSheet = ActiveSheet Application.ScreenUpdating = False For Each Ws In Worksheets Ws.Activate 'Remove formulas & external links With Cells .Select .Copy Selection.PasteSpecial Paste:=xlValues Application.CutCopyMode = False End With 'Remove Hyperlinks Cells.Hyperlinks.Delete [A1].Select Next FirstSheet.Activate End Sub |
|
|
|
Apr 10 2012, 06:29 AM
Post
#6
|
|
|
UtterAccess VIP Posts: 8,140 From: CT |
you may not need to loop thru all the worksheets. In your case, you are working on all the cells in all the worksheets. You should be able to select them all at once and perform what you need.
try this extra piece of code ****untested**** CODE Set oApp = CreateObject("Excel.Application")
oApp.Visible = False oApp.Workbooks.Open FileName:=strfilename oApp.UserControl = True oApp.Application.DisplayAlerts = False oApp.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select oApp.ActiveWindow.SelectedSheets.Delete oApp.ActiveWorkbook.Sheets.Select oApp.cells.hyperlinks.delete |
|
|
|
Apr 10 2012, 10:15 AM
Post
#7
|
|
|
UtterAccess Enthusiast Posts: 61 |
Thank you Bob. I can see how that would work for something like a hyperlink or a connection where the code can identify, select and delete. How would this look if I wanted to go through each sheet and replace formulas with values?
|
|
|
|
Apr 10 2012, 10:18 AM
Post
#8
|
|
|
UtterAccess VIP Posts: 8,140 From: CT |
off the top of my head and only for conversation purposes.
is what you are describing basically selecting the entire worksheet, the selecting copy, then paste special ? |
|
|
|
Apr 10 2012, 10:53 AM
Post
#9
|
|
|
UtterAccess Enthusiast Posts: 61 |
Basically, yes.
|
|
|
|
Apr 10 2012, 12:49 PM
Post
#10
|
|
|
UtterAccess VIP Posts: 8,140 From: CT |
then it would work the same basic way. Selecting all the worksheets and then all the cells and copy and paste formulas.
you can record a macro that does the steps and see what the excel vba would look like |
|
|
|
Apr 10 2012, 12:55 PM
Post
#11
|
|
|
UtterAccess Guru Posts: 594 |
Can I ask what the overall process is (As in what/why you are exporting from Access. What is in the Excel file etc) as I think there are other ways of tackling this that would remove the need to delete sheets and links etc and could all be done Access side.
|
|
|
|
Apr 10 2012, 12:57 PM
Post
#12
|
|
|
UtterAccess VIP Posts: 8,140 From: CT |
@jon,
The way I read this was that the OP wanted to move the "work" from Excel to Access. Bob |
|
|
|
Apr 10 2012, 01:04 PM
Post
#13
|
|
|
UtterAccess Guru Posts: 594 |
Hi Bob, I think I am reading it the other way, Access work to Excel. But the reason I am after a little bit more info is because I think this is the same thing that NineIron is asking about in another topic right now and I think some of the solutions proposed there (Pushing the data to a pre-defined spreadsheet avoiding links and MS-Query so as to allow emailing) might be better options then what is happening here. Need a little more detail to be sure though, I could be barking up the wrong tree.
|
|
|
|
Apr 10 2012, 01:17 PM
Post
#14
|
|
|
UtterAccess VIP Posts: 8,140 From: CT |
will have to wait to see which way it really is.
|
|
|
|
Apr 10 2012, 03:21 PM
Post
#15
|
|
|
UtterAccess Enthusiast Posts: 61 |
Firstly, thanks for all your replies. I have something that is 98% there now, but let me try and better explain what I am doing.
1) I have a lot of databases and queries built in Access than run every day through a master batch file. These are pulling in fresh data from a variety of sources. 2) I have linked data tables (from Access tables) in Excel files to create Dashboards with graphs, statistics and other relevant information that is emailed to a distribution. These Excel files contain a lot of pivot tables, vlookups, array formulas, etc., to provide the "graphs, statistics and other relevant information" I need. 3) Rather than emailing out a 50mb Excel file, I wanted convert all formulas to values, delete the sheets with linked tables that I don't need anymore, and create a slimmed down email version of the Excel file. 4) I already had the Excel macros to do this, but I wanted to run it from my Access master batch file directly--hence, the crux of my biscuit (problem, as it were) This was my original Excel code which worked great: CODE Sub DeleteLinks() Dim Ws As Worksheet, FirstSheet As Worksheet Set FirstSheet = ActiveSheet Application.ScreenUpdating = False For Each Ws In Worksheets Ws.Activate 'Remove formulas & external links With Cells .Select .Copy Selection.PasteSpecial Paste:=xlValues Application.CutCopyMode = False End With [A1].Select Next FirstSheet.Activate End Sub This is where I am at with the Access version. The only problem now, is that it is not fully releasing the Excel file as I cannot open it until I quit Access. What am I still doing wrong? CODE Function DeleteLinks() Dim xlApp As Excel.Application Dim Ws As Worksheet, FirstSheet As Worksheet Set xlApp = CreateObject("Excel.Application") Set FirstSheet = ActiveSheet With xlApp.Application .Visible = True .ScreenUpdating = False .Workbooks.Open "C:\Queries\Coating_Editing\test4.xlsx" For Each Ws In Worksheets Ws.Activate 'Remove formulas & external links With Ws.Cells .Select .Copy .PasteSpecial Paste:=xlPasteValues xlApp.Application.CutCopyMode = False End With Ws.[A1].Select Next .Sheets("Summary").Activate 'Save .ActiveWorkbook.Save .DisplayAlerts = False .ActiveWorkbook.Close SaveChanges:=True .Application.Quit End With Set xlApp = Nothing End Function |
|
|
|
Apr 10 2012, 05:41 PM
Post
#16
|
|
|
UtterAccess VIP Posts: 8,140 From: CT |
this should be what you need.
xlApp.Close set xlApp = Nothing |
|
|
|
Apr 10 2012, 06:01 PM
Post
#17
|
|
|
UtterAccess Enthusiast Posts: 61 |
Thanks Bob!
|
|
|
|
Apr 11 2012, 07:37 AM
Post
#18
|
|
|
UtterAccess Enthusiast Posts: 61 |
Hi Bob, I think I am reading it the other way, Access work to Excel. But the reason I am after a little bit more info is because I think this is the same thing that NineIron is asking about in another topic right now and I think some of the solutions proposed there (Pushing the data to a pre-defined spreadsheet avoiding links and MS-Query so as to allow emailing) might be better options then what is happening here. Need a little more detail to be sure though, I could be barking up the wrong tree. While the code is working now, Jon's comment above got me thinking that there is probably a more efficient way to do what I am doing. Instead of creating a linked data table in Excel which pulls from my Access table and refreshing each time, have Access push the data into Excel eliminating the links. If I have an Excel file called "C:\Dashboard.xlsx" with a worksheet called "Data", what is the best way to transfer all records from my Access table "FinalDataTable" into my Excel worksheet "Data"? I would need the code to overwrite existing records in "Data" without any prompts. Can you point me in the right direction? Thanks. |
|
|
|
Apr 11 2012, 07:53 AM
Post
#19
|
|
|
UtterAccess VIP Posts: 8,140 From: CT |
i think if you create a new thread for this new scenario you will get more people looking to give a good answer. You can also search UA
|
|
|
|
Apr 11 2012, 02:59 PM
Post
#20
|
|
|
UtterAccess Guru Posts: 594 |
Hi Classicxman,
See attached my example of pushing data to an existing Excel file. I choose to use an Excel template file when doing this as it opens a new instance of the file and forces the user to save a new copy somewhere keeping the original template always intact. You need to set a Reference to the 'Microsoft Office Excel Object Library' in the Access file otherwise it won't run. I used a dynamic named range in Excel as the source data for my Pivot Tables and Charts, this means that now matter how much data you add in it will always include all of that in them and I have written some quick code on each one so they refresh each time you select them. Apart from that it should be self-explanatory from the files. Good luck if you choose to go this way and don't hesitate to ask for any more help. JonSmith
Attached File(s)
|
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 24th May 2013 - 12:14 PM |