How to Move files with VBA when I have full filepath - vba

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

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.

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.

VBA open files, edit, save

I am trying to find a code which solves the following problem:
open a couple of csv files in one folder
run a formatting
save it as a xlsx file
The code for the formatting I have already in place. I just do not know how to open all files automatically, do the formatting and save them.
Looking forward to your answers.
Best
To open a file, you can modify this code(location & file name with extension):
patH = "C:\General\"
filE = "Example.csv"
Workbooks.Open Filename:=patH & filE
To save the formatted file, you can modify this code below(will overwrite if a file with same name exists):
path2 = "C:\General\"
file2 = "Example2"
dirFile = path2 & file2
ThisWorkBook.SaveAs dirFile, AccessMode:=xlExclusive, _
ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges

Loop through specific folder of csv, merging the 1st row of each into one workbook

I've looked through several posts and can't seem to find a suitable answer - similar, but not quite what I'm looking for.
I would like some code that loops through a series of csv-files (~ 200 at a time) within a specific folder. It needs to import/copy the values of the first row of each of these csv-files to one (main) workbook. These csv-file-names are numerical (eg 20140213075458) any files that does not meet this criteria (14 characters, numbers only AND FileFormat = csv) should not be considered.
Any guidance will be much appreciated!
Add the Microsoft Scripting Runtime library to your project and use Use FileSystemObject class to loop through all files. You can use the Like operator to check if file name matches your criteria.
i think this add-in might help you for sure.
RDBMerge used to Merge Data from Multiple Excel Workbooks,
csv and xml files into a Summary Workbook.
I am just going to outline the operations you need to do. You can work from that.
Get the filename of each file in each folder: fname = Dir(Your_Folder_Path)
Initialize file read counter: i=1
Loop through all files in folder:
Do While fname <> "" 'Loop all files in folder
Check your conditions:
Extension = Split(fname, ".")(Ubound(Split(fname, "."))) 'Extract extension of file
Name = Split(fname, ".")(Ubound(Split(fname, "."))-1) 'Extract name of file without extension
If Extension = "csv" And Len(Name) = 14 And IsNumeric(Name) Then 'Check csv, length of name 14 and numeric name
Open csv, read first line and close it:
Open Your_Folder_Path & fname For Input As #f1
Line Input #f1, csvFirstLine
Close #f1
Put data in workbook:
ActiveSheet.Range("A" & i).Value = csvFirstLine 'Put line in corresponding row
ActiveSheet.Range("A" & i).TextToColumns Comma:=True 'Separate data by comma
i=i+1 'File counter +1
Exit ifs and close loops: End If Loop

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.