Excel 2013 VBA Workbooks.Open opens with wrong filename - vba

When I attempt to open a certain target .xls file using the Workbooks.Open command in an Excel VBA macro, the target Excel file opens with a "1" appended to the end of the file name. This altered filename shows up in the file window (). With each subsequent attempt (manually closing the target Excel file & re-running the script) the appended number at the end of the target filename increments by one.
The target Excel file itself does not appear to be corrupted, as I am able to open the file manually in windows explorer without any issue. Also, when I do open it this way, the filename shows up correctly (without the '1' appended to the end). Looking at the 'Type' column in the Explorer window, it shows that the target file is 'Microsoft Excel 97-2003 Worksheet', so the file extension does match with the actual file format.
Below is the code that I am using:
Public Sub Import_TD_TVs()
Dim ActiveWB As Workbook
Dim srcTDWB As Workbook
Dim wkbkStr As String
Set ActiveWB = ActiveWorkbook
wkbkStr = "X:\sites\DTS_Data_Files.xls"
Set srcTDWB = Workbooks.Open(wkbkStr, , False)
End Sub
Any assistance on this issue would be greatly appreciated!

This can happen if the target workbook was originally saved as a template (.xlt) file and then the extension manually changed (to .xls) in Windows Explorer. You can open the target workbook and do a 'save as...' to .xls file format.

Related

How to open an excel file from a Sharpoint

I'm trying to open an excel file from a sharepoint site using VBA. I used the code from this page, but all it did was pull up my file explorer. I have also tried this code which I usually use for opening a closed workbook, not on a sharpoint.
dim wbk as workbook
set wbk = Workbooks.Open("Filepath")
Using this method returns an error telling me my file path is bad or non-existent, which my file path is correct. Any help would be appreciated. Thanks!
Dim wbk as Workbook
Set wbk = Workbooks.Open(Filename:="\\filepath\file.xlsm", UpdateLinks:=False, ReadOnly:=True)
You need to include the syntax "filename". This iteration also opens the file as a read-only copy. To open as a file you can edit, remove the "ReadOnly:=True" portion of the code.

Calling a function from another Excel workbook

I've got a pile of data in one worksheet that I am trying to save to individual workbooks based on values in several columns. The approach I am taking (for better or worse!) is to copy the relevant worksheet (and macros) to a new workbook, save it with an appropriate name (let's say temp.xlsx), and then to cleanse the data in that new workbook by deleting irrelevant rows (function called deleteInfo). This all has to be done without altering the original workbook, as per company policy.
I can copy the stuff over no problem, but I'm having serious issues calling macros in the new workbook then.
I have tried:
Application.Run "'temp.xlsx'!deleteInfo"
ActiveWorkbook.Application.Run deleteInfo
Application.Run ("'C:\user\.....\temp.xlsx'!deleteInfo")
But none have worked.
For the task like this you should consider creating an Excel add-in (file extension .xla) containing VBA macros while keeping the regular Workbooks with data macro-free (extension .xls or .xlsx). More details in Microsoft online article: https://support.office.com/en-ca/article/Add-or-remove-add-ins-0af570c4-5cf3-4fa9-9b88-403625a0b460
Hope this may help.
Solved this issue by exporting the module in which the macro was saved, copying the original workbook and importing it into the new workbook. pathName was defined in previous module to this as the path to the original file's folder (pathName = ActiveWorkbook.Path)
Sub exportMacro(ByVal pathName As String)
'Export the macro to save as .bas file
On Error Resume Next
Kill pathName & "\Module6.bas" 'Delete previously exported file
On Error GoTo 0
ActiveWorkbook.VBProject.VBComponents("Module6").Export pathName & "\Module6.bas"
End Sub
Sub importMacro(ByVal pathName As String)
'import the macro to a new workbook
ActiveWorkbook.VBProject.VBComponents.Import pathName & "\Module6.bas"
End Sub

How to save a read-only file as a different file extension in an excel macro?

I have a macro setup to automatically open/save a file that I am opening from the web. The web format is a #csv.gz format. I have code that currently just saves the file in the default location (which I have changed to c:\files). I want to write a macro that will keep the filename of the file, but change the extention to just file.xlsm. Is there a way to do this with VBA/excel? The reason while I need to change the extension is because it currently does not work with my formulas. The default save code I have just saves the file as a #csv.txt.
Is this possible?
Integrate this code into your own:
Sub SaveIt()
Dim wkb As Workbook
Set wkb = ActiveWorkbook 'change to your workbook reference
wkb.SaveAs Replace(wkb.Name, ".txt", ""), 52 'change ".txt" to ".csv" if need be
End Sub
See Excel File Type Enum for more information the 52.

How to open a file in an active workbook? VB.NET 2008

I have a program that filters data and then outputs it to a tabg delimited file. I then use Excel Interop to open this file in Excel because directly outputting to a worksheet takes too long. So currently I am using this code:
AD.DF.WriteToFile(vbTab)
Dim WB As Workbook = ExcelApp.Workbooks.Open(AD.DF.DatafileInfo.WriteToFileLocation)
ExcelApp.Visible = True
The first line takes the filtered data and outputs to a tab delimited file. The second opens that same file in a new workbook in Excel. And obviously the third makes Excel visible. Here is my problem though: right now when this code is run there are two open workbooks. I already have an active workbook and I would just like to open this file to that workbook.
If this is possible, how do I do it?
Thank you.
Look at the GetObject function.
Dim MyXL As Object
MyXL = GetObject(, "Excel.Application")
should get you a reference to the currently running instance of Excel.
In the code I created an object that is an Excel Workbook. I then set the created workbook as the ExcelApp.ActiveWorkbook. Then I was able to open the file without another workbook being created.

VBA - Copy as Path

I need help with a coding requirement that I've not previously experienced. I just browsed a similar issue raised here a couple of years ago - VBA to Copy files using complete path and file names listed in Excel Object.
My issue is similar but somewhat simpler than the OP.
I have a number of folders that each contain about 100 small .csv files; for each folder I need to copy the path for each file to an open worksheet. Each folder of .csv files has its own associated workbook.
As one example, the open workbook is F:\SM\M400AD.xlsm and the active worksheet is CSV_List. The folder containing the .csv files is F:\SM\M400AD.
Doing it manually, my sequence is then:
Open folder F:\SM\M400AD
Select all
Copy path
Paste to Range("B11") of worksheet CSV_List
When I do it manually, as described above, I get a list that looks like:
"F:\SM\M400AD\AC1.csv"
"F:\SM\M400AD\AC2.csv"
"F:\SM\M400AD\AE.csv"
"F:\SM\M400AD\AF.csv"
"F:\SM\M400AD\AG.csv"
"F:\SM\M400AD\AH1.csv"
"F:\SM\M400AD\AH2.csv"
"F:\SM\M400AD\AJ.csv"
and on down the page until I have a list of 100 paths. This single column list is then pasted into worksheet CSV_List, starting at Range("B11").
I need to automate this and would be grateful if a VBA guru could kindly code this for me.
Such of question has been asked before, for example:
Loop through files in a folder using VBA?
List files in folder and subfolder with path to .txt file
The difference is you want to "automate" it, which means you want to execute code on workbook Open event.
How to achieve that?
Open F:\SM\M400AD.xlsm file.
Go to Code pane (ALT+F11)
Insert new module and copy below code
Option Explicit
Sub EnumCsVFilesInCurrentFolder()
Dim sPath As String, sFileName As String
Dim i As Integer
sPath = ThisWorkbook.Path & "\"
i = 11
Do
If Len(sFileName) = 0 Then GoTo SkipNext
If LCase(Right(sFileName, 4)) = ".csv" Then
'replcae 1 with proper sheet name!
ThisWorkbook.Worksheets(1).Range("B" & i) = sPath & sFileName
i = i + 1
End If
SkipNext:
sFileName = Dir(sPath)
Loop While sFileName <> ""
End Sub
Now, go to ThisWorkbook module and insert below procedure:
Private Sub Workbook_Open()
EnumCsVFilesInCurrentFolder
End Sub
Save and close workbook
The workbook is ready to use. Whenever you open it, EnumCsVFilesInCurrentFolder macro will be executed.
Note: you have to change above code to restrict the number of records.