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
Related
I am trying to use ChDir in Excel VBA to change the default directory to the one the current workbook is located, so that I can have the macro open an open file dialog box with that same folder as the default.
I can't get it to work properly, my code below gives a runtime error saying the path not found.
Dim myPath As String
myPath = ActiveWorkbook.Path
ChDrive ""
ChDir myPath 'this line gives the error
OpenFiles = Application.GetOpenFilename(Title:="select file(s) to import", MultiSelect:=True)
I'm working in a Onedrive folder, which seems to be the problem (it works ok if I move the workbook to my desktop.)
How can I get around this problem? I want to be able to use this macro regardless of where the workbook is located.
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
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 building a utility to grab data from specific files and transfer that data to another file. The source files all have the same naming convention which includes an identical string that can be used to differentiate them from other files in a directory. The intent is to use that string with wildcards to open the file and pull the required data.
I thought I had Workbook.Open working with wildcards yesterday, after consulting this StackOverflow post and this entry in the Office Dev Center, but today I try running my test code and VBA claims not to find the file (though the text suggests otherwise).
There is not much to go wrong in my test code:
Dim strFilePath As String
strFilePath = "C:\...\KMMacros\DeepLinkTransferFromSabaForm\"
Dim strFileName As String
strFileName = "*SabaEntryForm.xlsx"
Workbooks.Open FileName:=Dir$(strFilePath & strFileName), ReadOnly:=True
The error reads
'TEST_KM6.7-3_BRSTA_SabaEntryForm.xlsx' could not be found. Check the
spelling of the file name, and verify that the file location is
correct.
and debug highlights
Workbooks.Open FileName:=Dir$(strFilePath & strFileName), ReadOnly:=True
Because the error specifically mentions the file I expected to open, and because my code does not give the specific file name, it seems like VBA has found the file, even though the error states otherwise.
How can VBA be finding the name of the file and then claim that it cannot find it? What am I doing wrong?
Dir() only returns the filename, not the whole path, so if your current directory is different from the folder you pass to Dir then you will have this problem.
Dim strFilePath As String
strFilePath = "C:\...\KMMacros\DeepLinkTransferFromSabaForm\"
Dim strFileName As String
strFileName = "*SabaEntryForm.xlsx"
Workbooks.Open FileName:=strFilePath & Dir$(strFilePath & strFileName), _
ReadOnly:=True