Put a formula inside a cell using an automatic path - vba

With VBA I'm trying to put automatic paths to my resources inside Excel cells.
I've been trying to put a path from the directory where I launch my macro using:
ThisWorkbook.Path
This function works to delete a file as follows :
Set fs = CreateObject("Scripting.FileSystemObject")
fs.DeleteFile ThisWorkbook.Path & "\File.xlsx", force
But I'm experiencing some trouble when trying to use it inside the following line:
Range("E2").Formula = "='" & ThisWorkbook.Path & "\[DJNDA.xls]Feuil1'!$I2"
Where I'm just wishing to obtain the value of a cell from a file in the same directory.Right now the formula won't change if I move my file in another directory, that's why I want to use this kind of functions. I still use this kind of functions directly inside my cells:
='C:\path\to\file\[File.xls]Feuil1'!$I2
Thanks for your help

Try the formula
= CELL("filename")
in your cell (after you have saved the workbook of course, so that a valid pathname exists for it).

Related

Visual Basic: Save in same folder as Excel Sheet

I'm very new to VB, so this is probably a very easy one. I'm creating a Word document from an Excel spreadsheet, and would like the Word doc to save in the same folder location as the spreadsheet.
I'm using the code:
.SaveAs Filename:=ThisWorkbook.Path & Range("C8").Text & ".docx"
Which I though would work, but it saves it in teh directory up from the location.
I.e. the spreadsheet is in C:/User/Documents/MySpreadsheet. But the Word doc would be saved in C:/User/Documents.
I also made a message popup to display ThisWorkbook.Path which comes up with the Spreadsheet path, so I know that's right!
I also don't think I've done the naming right, as I would like it to be named the text in cell C8. But it's actually the 'Documents' folder name with the text in C8 added on.
Thanks in advance.
Activate Immediate Window (Ctrl+G) and and add this line to your code:
debug.print ThisWorkbook.Path & Range("C8").Text & ".docx"
You will see if your path is correct. In particular, if you have "\" between folder path and filename.

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"

activating dynamic named excel file

Apologies if this is too easy to ask;
I have an Excel file which has a dynamic Name under this code:
Code for saving file with dynamic name.
Dateiname = Ord & mNummerGanz & "_" & Name & ".xlsm"
ThisWorkbook.SaveAs Filename:=Dateiname
After saving this file with a dynamic Name, i want to open another Excel file, copy a range from this file and then paste it on my dynamic named file.
Windows("Dateiname.xlsm").Activate gives an error.If i put Windows(Dateiname).Activate it still gives an error. Dateiname variable is in different module then Windows.Activate, how can i call this variable then ?
Does anyone have solution for this Problem ?
You probably don't want to open a file named Dateiname.xlsm, but a file with named stored in Dateiname variable. Then, use Windows(Dateiname).Activate.

EXCEL VBA: opening XLSM excel using 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

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.