activating dynamic named excel file - vba

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.

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.

Put a formula inside a cell using an automatic path

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).

How to Move files with VBA when I have full filepath

I'm very new to VBA.
I have a list of file names and file paths. For example, I have a file name of "More tutorials.xlsx" and a file path of "C:\Users\Matt\Documents\Coding\VBA\More tutorials.xlsx" which is where the aforementioned file is saved.
I want to move all the files in this list to another directory. Using either one or both of the above pieces of information that I have, could someone please show me how to do this? I realise it's very simple.
Thanks in advance,
Matt
You can use the Name command. Example:
name "C:\temp\test.txt" as "c:\testdir\test.txt"
This just moved the file test.txt from my temp folder to my testdir folder on C drive.
Now you can use that to loop through the rows using something like
For each cell in range("A1:A" & range("A" & rows.count).end(xlup).row)
'Do your moving of files using cell.text to get the dir
Next

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.