I have a pretty basic knowledge base of VBA. I've been using it the past year to write programs that copy the data from excel to websites.
Right now I have a local excel sheet full of data (which is updated daily), that I am trying to have automatically copied into the Excel Web App via VBA.
I'm hoping someone can tell me how to reference the cells in the Web App from my local VBA (assuming it's possible). I can't seem to find the answer anywhere else.
Any help would be greatly appreciated, thank you!
The Excel Web App opens files from OneDrive, therefor, saving the file in the OneDrive folder on your local machine will do what you ask.
All three of the following examples presume the purpose is to save a macro-enabled workbook as a macro-enabled workbook of the same name in the default OneDrive folder location "C:\Users\user\OneDrive"
The first two examples save a copy of the active workbook to OneDrive; in contrast, the third example moves the file without creating a copy and cannot be used if the file is open.
Example 1: save a copy of the active workbook to OneDrive. The original workbook remains the active workbook. Subsequent changes remain with the original and are not applied to the copy in OneDrive.
Sub SaveCopyToOneDrive()
Dim destinationFolder As String
destinationFolder = Environ("USERPROFILE") & "\OneDrive\"
ActiveWorkbook.SaveCopyAs destinationFolder & ActiveWorkbook.Name
End Sub
Example 2: save the active workbook in OneDrive. The file in OneDrive becomes the active workbook and the original workbook is closed without saving.
Sub SaveToOneDrive()
Dim destinationFolder As String
destinationFolder = Environ("USERPROFILE") & "\OneDrive\"
ActiveWorkbook.SaveAs destinationFolder & ActiveWorkbook.Name, FileFormat:=xlOpenXMLWorkbookMacroEnabled
End Sub
Example 3: move the file from an explicitly defined location to OneDrive. The target file cannot be the VBA host for this macro.
Sub MoveToOneDrive()
Dim shortFileName As String
Dim fullFileName As String
Dim destinationFolder As String
fullFileName = "C:\MyDataFiles\File.xlsm"
Name fullFileName As destinationFolder & shortFileName
destinationFolder = Environ("USERPROFILE") & "\OneDrive\"
shortFileName = Mid(fullFileName, InStrRev(fullFileName, Chr(92)) + 1, Len(fullFileName))
End Sub
Related
I'm trying to run a Macro across multiple files. I have used this script before and it worked, but maybe I have changed something inadvertently? The problem I'm having is that I get an error message that "PERSONAL.XLSB cannot be found, is it possible it was moved..." However, I'm using the full path of the .xlsb file and I am running the macro from that file.
This is my script:
'Sub SHELLforMacros()
Dim wbMatrix As Workbook
Dim strFileName As String
Dim strPath As String
Dim strExt As String
Dim objWorkbook As Workbook
Dim ws As Worksheet
'This is the folder of files it needs to run through:
strPath = "C:\Users\myname\Desktop\All_mricgcm3_files\45\Fall45\test\"
strExt = "csv"
strFileName = Dir(strPath & "*." & strExt)
While strFileName <> ""
Set wbMatrix = Workbooks.Open(strPath & strFileName)
'Set objWorkbook = ActiveWorkbook
Application.Run "C:\Users\myname\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB'!Graph_NEW"
wbMatrix.ActiveWorkbook.Save
wbMatrix.Close SaveChanges:=True
strFileName = Dir
Wend
End Sub
I have also tried putting the PERSONAL.XLSB file into the same folder as the files it needs to run (and changing the path to reflect that). And I tried to run, or get it started, from one of the files in the folder rather than from the PERSONAL.XLSB without using a full path. I don't understand why it can't be found. Thanks.
Running a macro from another workbook can be simple done in a way I will try describing in a following step.
But running "a Macro across multiple files" does not mean any modification in terms of calling the macro. You did not show the macro code, but if it references ThisWorkbook like the one to be processed, it must reference the needed one (ActiveWorkbook, or wb, if previously it has been Set).
If "I am running the macro from that file" means running the macro from "PERSONAL.XLSB", it should simple be called as:
Graph_NEW
as it has already been suggested. If you insist to use Application.Run, even if it is not necessary, you can try:
Application.Run "Graph_NEW"
Now, if you run the code in a different workbook and need to run the macro from "PERSONAL.XLSB", the simplest way is using:
Application.Run "PERSONAL.XLSB!Graph_NEW"
Of course, "PERSONAL.XLSB" should be open.
If you insist to use the "PERSONAL.XLSB" full path, it will also work and you can use something similar with what you are trying. The advantage is that, if the workbook is closed, it will be open to run the macro:
Application.Run
Application.Run "'C:\Users\myname\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB'!Graph_NEW"
Your code misses the prefix "'". It will work without it if no any spaces exist in the workbook full path, but you should delete the one before "!"... In your case, if "myname" does not contain any space, it may be missing.
You can also add a reference to "Personal.xlxb" VBAProject and call the macro as you do in the working workbook. To do that, previously change the initial/existing standard name in something like "PersVBProject" (right click on the project from VBE 'Project Explorer`, choose 'VBAProject Properties...', change the existing name and press 'OK').
So, simple call it as:
Graph_NEW
I have many excel file(.xls) in a folder and each of them have a vba macro/module in it and I need them to be remove. Can I write an external program which loop through the folder, inspect each excel file and remove vba module? Please tell me what programming language could does it most easily. Thanks!
With a manual process, I have to 1) open the worksheet, 2) click "Enable Content", 3) Go to Developer tab, 4)Click "Visual Basic" button, 5) Right click the vba module and delete. As I mentioned, I have of these excel files and it is a routine job and I want to remove the human part. Thanks.
What I would do, I would create a macro which would open these files and save them as xlsx instead of xlsm. As xlsx can not contain the modules they will vanish.
Than you just have to delete the original ones.
You can even use macro recording to do this.
If we are talking about a lot of files,( I suppose we do)
You can generate the name of the files, if there is logic behind them ( like dates)
or you can place them into folder, and than loop through the items in the folder, open them, save as, delete original.
You can find a lot of options how to check each file in a folder (FSO)
Happy new year!
Change Path and try:
Option Explicit
Sub LoopThroughFiles()
Dim StrFile As String
Dim Element As Object
Dim WB As Workbook
StrFile = Dir("C:\Users\XXXXX\Desktop\Test\*")
Do While Len(StrFile) > 0
If Right(StrFile, 4) = ".xls" Then
Set WB = Workbooks.Open("C:\Users\marios.p\Desktop\Test\" & StrFile)
For Each Element In ActiveWorkbook.VBProject.VBComponents
If Element.Type <> 100 Then
ActiveWorkbook.VBProject.VBComponents.Remove Element
End If
Next
WB.Save
WB.Close
End If
StrFile = Dir
Loop
End Sub
This is one of my first times using VBA. I have a command button that is supposed to be saving a file as, to my sharepoint online documents page. I can use the "excel services" and save to that documents page from excel manually, but when i try the same in VBA it is saying I do not have permission. Below is the code I am using, any advice would be much appreciated!
Private Sub CommandButton1_Click()
Dim path as string
dim filename1 as string
path = "https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents"
filename1 = Range("B3").text
activeworkbook.SaveAs FileName:=path & filename1 & ".xlsx", _
FileFormat:=xlopenxmlworkbook
End Sub
If your current file is in the same folder as the destination you would like to save in, try this change for the definition of path:
path = "https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents/"
The missing final / in your code is causing the FileName argument to be invalid, because path & filename1 & ".xlsx" evaluates to
https://xxxxxx.sharepoint.com/sites/xxxxx/Shared Documents[filename1].xlsx
Which means if permissions weren't restricted on the /xxxxx folder you would have written a badly named Excel workbook in that location.
ALTERNATIVE SOLUTION
Potentially another solution for your problem. Save an empty workbook in the location you wish to save your new Excel files. Open the empty Excel file, and run your macro there. Change the path line to:
path = ActiveWorkbook.Path & "\"
Try this to see if it works instead. This is how I got around Sharepoint permissions problems.
I am trying to create a macro that will allow me to import data from all files ending in ".xlsx" in a specified folder into a single spreadsheet. I've mostly figured it out, but there's a recurring issue that requires a manual work-around until I figure out a solution.
The source .xlsx files contain a standard template for inputting data and are populated and submitted to me by other users. I download these files into a specified folder on my computer. However, the Dir function does not recognize any of the .xlsx files in the specified folder unless I open the file in Excel and click "Save". Once I do that, the Dir function works perfectly and all the .xlsx files are listed in the Immediate window. I'd appreciate any insight anyone has regarding why this is happening and if there's any way to fix the code so it recognizes the files without opening and saving.
Sub Test()
Dim strPath As String
Dim strFile As String
strPath = ActiveWorkbook.Path & Application.PathSeparator & "ReferenceFolderName" & Application.PathSeparator
strFile = Dir(strPath, MacID("XLSX"))
Do While Len(strFile) > 0
strFile = Dir
Debug.Print strFile
Loop
End Sub
I have an inventory xls file, that includes a diagram of the office. When the xls is opened, the VBA automatically makes a backup of the file before any changes are made. That has worked for the past year. Today it has broken.
Yesterday, the file was working fine.
1) I added some info to the xls sheet - nothing unusual; the same sort of info I've added over the past year. I did NOT edit the VBA.
2) I added some objects (shapes-circle, square, etc) to the tab with the office diagram.
Today, the file is not working.
1) The VBA debugger gives an error on open: "Compile Error: Cannot find file or library."
2) Numerous text boxes/shapes have vanished off the tab with the diagram. I did not remove them.
UPDATE 1: I moved a shape on the diagram, and all the text boxes reappeared.
Any suggestions to fix this are appreciated.
'Saves an exact copy of the file upon opening it to the \Back_Tracker location and added today's date to the filename.
Private Sub Workbook_Open()
Dim WBPath As String, WBName As String, TimeStampStr As String, PassW As String
WBPath = ThisWorkbook.Path
WBName = ThisWorkbook.Name
'PassW = "something"
Const cstrBACKUP As String = "Backup_Tracker"
If InStr(1, WBPath, cstrBACKUP) = 0 Then 'prevent backups from making backups of themselves.
TimeStampStr = Format(Now(), "YYYY-MM-DD_hh-mm_")
'Application.StatusBar = "Saving backup..."
ActiveWorkbook.SaveCopyAs Filename:=WBPath & Application.PathSeparator & cstrBACKUP & Application.PathSeparator & TimeStampStr & WBName
'Application.DisplayStatusBar = False
'Application.DisplayStatusBar = True
End If
End Sub
My experience with Excel VBA is that the code can sometimes corrupt when opening. It used to be the case with Access as well, but I'm not sure if that was eventually fixed.
My solution was to either make backup copies after each edit, or use source control for the Excel file. Its the only way to be sure.
Found the problem.
Unchecked this, and it's working now.
Reference
Open the database or application.
Open a module in Design view or press ALT+F11 to switch to the Visual Basic Editor.
On the Tools menu, click References.
Clear the check box for the type library or object library marked as "Missing:"