If command malfunction "Name SRC_Path & fname As DEST_Path & fname" - vba

Unsure what has happened over the weekend but my code no longer works and I am receiving an error message for the below code. "Name SRC_Path & fname As DEST_Path & fname" any help is greatly appreciated kind regards
Private Sub CommandButton1_Click()
Dim SRC_Path As String
SRC_Path = "C:\Users\oliver\OneDrive\Client Files\Job Sheets\"
If ComboBox1.Value = Sheet1.Range("A115") Then
Dim DEST_Path As Variant
DEST_Path = "C:\Users\oliver\OneDrive\Client Files\Wasdell Packaging Cleanrooms\Job Sheets\"
Dim fname As Variant
fname = Dir(SRC_Path & "*Wasdell Packaging Cleanrooms*")
If Len(fname) > 0 Then
Name SRC_Path & fname As DEST_Path & fname
End If
End If

Related

Trying to search for a file using a variant, however its searching for just the variant "\Client" see below many thanks

Dim SRC_Path As String
SRC_Path = "C:\Users\Oliver\OneDrive\Client Files\Job Sheets\"
Dim Client As Variant
Client = ComboBox1.Value
Dim DEST_Path As Variant
DEST_Path = "C:\Users\Oliver\OneDrive\Client Files\" & Client & "\Job Sheets\"
Dim fname As Variant
fname = Dir(SRC_Path & "*Client*")
If Len(fname) > 0 Then
Name SRC_Path & fname As DEST_Path & fname
End If
End Sub

Search for file using partial name then copy file to another location

Hi I have been having a go at trying to make this work but its been a while since I have used VBA,
can anybody shine any light on the below? Any help is greatly appreciated
the getfolder line doesnt seem to be working for me.
Private Sub CommandButton1_Click()
Private Sub CommandButton1_Click()
'use Const for fixed values
Const SRC_PATH As String = "C:\Users\oliver\Servicing - Job Sheets"
Const DEST_PATH As String = "C:\Users\oliver\Servicing Client Files\Wasdell\Job Sheets"
Dim fname As Variant
fname = Dir(SRC_PATH & "Wasdell Europe*")
If Len(fname) > 0 Then
FileCopy SRC_PATH & fname, DEST_PATH & fname
End If
End Sub
Should work:
Private Sub CommandButton1_Click()
'use Const for fixed values
Const SRC_PATH As String = "C:\Users\oliver\Servicing - Job Sheets\"
Const DEST_PATH As String = "C:\Users\oliver\Servicing Client Files\Wasdell\Job Sheets\"
Dim fname As Variant
fname = Dir(SRC_PATH & "*Wasdell Europe*") '<< anywhere in file name
If Len(fname) > 0 Then
FileCopy SRC_PATH & fname, DEST_PATH & fname
End If
End Sub

Rename files in folder vba

I have code to find a filename from column A and rename files as in column B in a source folder and then copy to a new folder.
The code is as below.
Sub Rename_Files()
Dim SourcePath, DestPath, Fname, NewFName
SourcePath = "C:\Invoices\"
DestPath = "C:\Invoices\Renamed\"
For i = 1 To 100
Fname = Range("A" & i).Value
NewFName = Range("B" & i).Value
If Not Dir(SourcePath & Fname, vbDirectory) = vbNullString Then
FileCopy SourcePath & Fname, DestPath & NewFName
Else
MsgBox (Fname & " Not Exists in Folder")
End If
Next i
End Sub
The problem is that The filenames in the source directory are long like 'INVOICEDUMP_OFND_4294819_ABC Corp.pdf' and hundreds of like this.
I want to find the file containing 4294819 (from column A) in the name and then replace the name with only 'INV 4294819.pdf' (as mentioned in column B).
Thanks
Unless my DOS skills are extremely rusty, you should be able to use
Sub Rename_Files()
Dim SourcePath As String, DestPath As String, Fname As String, NewFName As String
Dim i As Long
SourcePath = "C:\Invoices\"
DestPath = "C:\Invoices\Renamed\"
For i = 1 To 100
If Not IsEmpty(Range("A" & i).Value) Then
NewFName = Range("B" & i).Value
'Search for the first file containing the string in column A
Fname = Dir(SourcePath & "*" & Range("A" & i).Value & "*")
If Fname <> vbNullString Then
FileCopy SourcePath & Fname, DestPath & NewFName
Else
MsgBox Range("A" & i).Value & " Not Exists in Folder"
End If
End If
Next i
End Sub
This assumes that column A has entries such as 4294819 and that the corresponding entry in column B is something like INV 4294819.pdf.

IsNA Does Not Recognize #N/A in Closed Workbook Reference

I'm trying to test if a cell in a closed external workbook is N/A. In the code below, cell "G5" in the referenced workbook is definitely N/A, but when referencing it using the IsNA function below it returns "Good to go!" when the intention is for it to return "Hay!" in the message box.
Sub TestTest()
'Declaring variables [BD]
Dim sFilePath As String
Dim sFileName As String
Dim sSourceSheet As String
Dim sSourceCell As String
sFileName = "0306-0312 Margin Master.xlsx"
sFilePath = "\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\"
sSourceSheet = "Bakery"
sSourceCell = "G5"
If Application.WorksheetFunction.IsNA("'" & sFilePath & "[" & sFileName & "]" & sSourceSheet & "'!" & _
Range("A1").Range(sSourceCell).Address(, , xlR1C1)) Then
MsgBox "Hay!"
Else
MsgBox "Good to go!"
End If
End Sub
Try using ExecuteExcel4Macro:
Sub TestTest()
'Declaring variables [BD]
Dim sFilePath As String
Dim sFileName As String
Dim sSourceSheet As String
Dim sSourceCell As String
dim externalValue As Variant
sFileName = "0306-0312 Margin Master.xlsx"
sFilePath = "\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\"
sSourceSheet = "Bakery"
sSourceCell = "G5"
externalValue = ExecuteExcel4Macro("'" & sFilePath & "[" & sFileName & "]" & sSourceSheet & "'!" & _
Range("A1").Range(sSourceCell).Address(, , xlR1C1))
If Application.IsNa(externalValue) Then
MsgBox "Hay!"
ElseIf IsError(externalValue) Then
MsgBox "May not work"
Else
MsgBox "Good to go! (value is '" & externalValue & "')"
End If
End Sub
Note: Range("A1").Range(sSourceCell).Address(, , xlR1C1) can probably be abbreviated to Range(sSourceCell).Address(, , xlR1C1) if you are just using cell references such as "G5" as the values of sSourceCell.
An alternative may be to use Evaluate() with a "regular formula", i.e.
Sub TestTest()
'Declaring variables [BD]
Dim sFilePath As String, sFileName As String, sSourceSheet As String, sSourceCell As String
sFileName = "0306-0312 Margin Master.xlsx"
sFilePath = "\\store\GroupDrives\Pricing\_Deli_\Deli Fresh Shift\Margin Master\"
sSourceSheet = "Bakery"
sSourceCell = "R5C7"
If Application.WorksheetFunction.IsError(Evaluate("=('" & sFilePath & "[" & sFileName & "]" & sSourceSheet & "'!" & sSourceCell & ")")) Then
MsgBox "Hay!"
Else
MsgBox "Good to go!"
End If
End Sub
It should work for you if the cell is truly an #N/A error. If it's just a string that's #N/A, you can just tweak that If statement to check evaluate the cell value.
Note: The Cell Reference needs to be R1C1 style, I believe.

Open a workbook from Saved location

Have a requirement to store a file in mydocuments. Save location has to be VDI or laptop independent.
I am able to successfully save the file, in the location I want. But with the same file name and location, I am unable to open it.
The path and the filename for workbooks.open is coming like this, when I am testing it thru VDI. Bit strange, I not able to open the workbook using the path I used to save it.
"\xxx.yyy.CO.NZ\PaleeS.home$\InsuranceAdvisory\Excel\DT_test delete_rectangle 1_04.May.17.xlsm"
Dim strFilename As String
Dim Client1 As Range
Dim Client2 As Range
Dim CurrentDate As String
Dim FilePrefix As String
Dim strDir As String
Dim StrDirSub As String
Dim Savedflie As String
Dim CurrentFolderpath As String
Dim CurrentFilename As String
CurrentFolderpath = Application.ActiveWorkbook.Path
CurrentFilename = Application.ActiveWorkbook.Path & "\" & Application.ActiveWorkbook.FullName
strDir = CreateObject("WScript.Shell").SpecialFolders("mydocuments") & Application.PathSeparator & "\InsuranceAdvisory"
StrDirSub = strDir & "\Excel"
If Dir(strDir, vbDirectory) = "" Then
MkDir strDir
End If
Select Case Dir(StrDirSub, vbDirectory)
Case ""
MkDir StrDirSub
End Select
FilePrefix = "\DT_"
Set Client1 = Range("Client1")
Set Client2 = Range("Client2")
CurrentDate = Format(Date, "dd.mmm.yy")
strFilename = FilePrefix & Client1.Value & "_" & Client2.Value & "_" & CurrentDate
ActiveWorkbook.Save
ActiveWorkbook.SaveCopyAs StrDirSub & strFilename & ".xlsm"
Savedflie = StrDirSub & strFilename & ".xlsm"
Application.Workbooks.Open (Savedflie)