EXCEL VBA: opening XLSM excel using vba - vba

i cant figure out my error in my codes
If cboUnit.Text = "MARINER" Then
Application.ScreenUpdating = False
Application.Workbooks.Open Filename:=ThisWorkbook.Path & "\UNIT" & "\MARINER"
ThisWorkbook.Activate
Workbooks("TOUKEI DATA REPORT GRAPHING").Close
End If
i just want to open xlsm file which is on the folder unit
but i always got an error:
runtime error '1004'
the file this getting is 'xlsx' extension

If you use something like this, it should at least make debugging simpler. Then substitute the code I've provided with this line:
Application.Workbooks.Open Filename:=ThisWorkbook.Path & "\UNIT" & "\MARINER"
Declare the variables with your other variables. I'm understanding your question to be that "Mariner" is the file name. If it's not, you will need to change the fileNPath.
Dim path As String, fileNPath As String
'add these two values above your code
path = ActiveWorkbook.path
fileNPath = path & "\UNIT\MARINER.xlsm"
Application.Workbooks.Open fileName:=fileNPath 'Change this line
Once you've done this, you can see the values for the file path in debug mode where the version you have won't have a value until the line that isn't working anyway. So this way, you can SEE what is going on before you try to USE it.
note: To have a macro enabled workbook, it must be .xlsm, not .xlsx

Related

VBA Error on opening workbook - Cannot find workbook that is there

I'm attempting to open a workbook, but Excel gives me an error message saying that it cannot find the workbook.
Dim pricing As Workbook
dim fname As String
fname = Dir(ActiveWorkbook.Path & "\Budget Pricing Detail*.csv")
Set pricing = Workbooks.Open(Filename:=fname, UpdateLinks:=False)
As you can see I'm using a wildcard to pull the file name as the last bit of the filename will be different for each instance. What's weird to me as that when Excel gives me the error box it actually names the WHOLE file (even though I never did in my code) I want it to find and says it can't find it. Also what's weird is that this was working already and all of a sudden now it isn't.
I think DIR only returns the filename, and not the entire path. Try:
fname = ActiveWorkbook.Path & "\" & Dir(ActiveWorkbook.Path & "\Budget Pricing Detail*.csv")

How to open and activate another workbook in VBA?

I'm creating a macro where I will need to run it in 1 file (called "Masterfile"), it will open and execute the macro on another file ("SurveyReport") and then give me a message box saying "done!".
The code I have to execute on the SurveyReport file works fine when I open that file manually and execute it. The code I need to open SurveyReport from MasterFile is also working it seems, I ran the below with no issues:
Sub PivotTable()
'
' PivotTable Macro
Dim MasterFile As String
MasterFile = ActiveWorkbook.Name
Dim SurveyReport As String
SurveyReport = Application.GetOpenFilename("Excel files (*.xlsx), *xlsx", 1, "Please select the Survey Create Report file", , False)
Workbooks.Open (SurveyReport)
End Sub
But, when I try to activate the SurveyReport file so I can begin executing the macro in it, I get a "Subscript out of range" error. I've tried using the following code after the above block and before the code to execute in the SurveyReport file:
Windows(SurveyReport).Activate
This didn't work, not did:
ThisWorkbook.Activate
...which only had the effect of activating the MasterFile.
SurveyReport file is a .xlsx file. I tried saving it as a .xls file and amending the code, but no joy.
I also tried passing it the file name directly (i.e. Windows("filename.xlsx").Activate), same issue.
ActiveWorkbook is as it says on the tin - whichever workbook happens to be active when the code runs.
ThisWorkbook is always the workbook that the code is sitting in.
You can SET references to specific workbooks rather than just using their names each time. A name can change, or reference the wrong object.... imagine you have a friend called Darren. Each time you mention him you mention him by name. Someone that doesn't know Darren hasn't a clue which Darren out of all the ones available in the world you're talking about. Now imagine you have a little replica of Darren in your pocket... nah, that's a terrible anology - it wouldn't be a replica, it would be a reference to the real Darren... anyway, I digress.
This code sets a reference to the workbook, you can then use that reference any time you want to refer to the correct workbook:
Sub PivotTable()
Dim MasterFile As Workbook
Dim SurveyRptName As String
Dim SurveyReport As Workbook
Set MasterFile = ThisWorkbook '
SurveyRptName = Application.GetOpenFilename("Excel files (*.xlsx), *xlsx", 1, _
"Please select the Survey Create Report file", , False)
If SurveyRptName <> "False" Then
Set SurveyReport = Workbooks.Open(SurveyRptName)
End If
SurveyReport.Activate 'You don't need this line. It doesn't matter if
'the workbook is active, the code knows which one
'you're talking about in the next line.
MsgBox "This is " & SurveyReport.Name & _
" containing " & SurveyReport.Worksheets.Count & " sheets." & vbCr & _
"The value in cell A1 of the first sheet is " & _
SurveyReport.Worksheets(1).Range("A1")
End Sub
Edit: Of course, if you press Cancel when selecting a file then the lines following the IF...THEN code won't have a reference to work on and you'll get a Object Variable or With block variable not set - best not to run the bottom bit of code if you haven't successfully opened the Survey Report file.
The part of the answer that is missing - is that he tried to call a method of an object when his variable was STRING - the open command and subsequent commands will give you an OBJECT - which has properties and methods like .Activate. The STRING type has no such method or property (to be sure - it may have others).
the solution provided by Darren solves this by declaring his SurveyReport as type Workbook - an object of Excel.

How to reference the text in a cell in vba

What i am about to ask may have already been asked before but i couldn't find the relevant thread, most likely because i am not phrasing the search correctly.
What i want to do in vba is open a workbook in the same folder as the excel-workbook with the macro.
Currently i have referenced the exact location of the file:
wkbSource = Workbooks.Open("C:\Users\Desktop\Scorecard\E2E.xlsx")
The name of the excel file may not be the same in the future.
So what i want to do is have the user type in the name of the file in Sheet1!A1 cell and then use the text typed in the cell.
I tried using the following but that didnt work. Its throwing a Run-time error '76' path not found.
wkbSource = Workbooks.Open(ThisWorkbook.Path\Range("A1"))
Regards
The concatenation operator in VBA is &. Use it to connect strings together:
wkbSource = Workbooks.Open(ThisWorkbook.Path & "\" & Range("A1"))
path = "C:\Users\Desktop\Scorecard" & "\"
Name = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
wkbSource = path & Name & ".xlsx"

Excel workbook trying to save to network directory when it shouldn't when running macro

I'm trying to get a macro to work where it saves a file a certain format and reopens it to avoid compatibility issues, however, when the macro runs it tries to save to a network directory that is no way specified in the code.
Dim wkbook As String
wkbook = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".xlsm"
ActiveWorkbook.SaveAs wkbook, FileFormat:=52
ActiveWorkbook.Close
Workbooks.Open Filename:=wkbook
Use ChDir to set the directory.
ChDir("C:\mydirectory")

Saving an Excel sheet in a current directory with VBA

I have created a sheet in vba Excel. I would like to save it the current directory, but not in absolute path, then, when this is executed somewhere else, there won't be problem.
Can somebody help ?
I am not clear exactly what your situation requires but the following may get you started. The key here is using ThisWorkbook.Path to get a relative file path:
Sub SaveToRelativePath()
Dim relativePath As String
relativePath = ThisWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:=relativePath
End Sub
VBA has a CurDir keyword that will return the "current directory" as stored in Excel. I'm not sure all the things that affect the current directory, but definitely opening or saving a workbook will change it.
MyWorkbook.SaveAs CurDir & Application.PathSeparator & "MySavedWorkbook.xls"
This assumes that the sheet you want to save has never been saved and you want to define the file name in code.
If the Path is omitted the file will be saved automaticaly in the current directory.
Try something like this:
ActiveWorkbook.SaveAs "Filename.xslx"
Taking this one step further, to save a file to a relative directory, you can use the replace function. Say you have your workbook saved in: c:\property\california\sacramento\workbook.xlsx, use this to move the property to berkley:
workBookPath = Replace(ActiveWorkBook.path, "sacramento", "berkley")
myWorkbook.SaveAs(workBookPath & "\" & "newFileName.xlsx"
Only works if your file structure contains one instance of the text used to replace. YMMV.