I'm new to VBA so please bear with me here.
I'm having some trouble with updating a master file (the KAM Account Plan) using files from a defined folder (we'll call it the 'upload folder').
What I'm trying to do is:
- open the files contained in the upload folder one by one
- match the account code in them (cell E5) against those that appear in the master file (column A)
- update the relevant cells in the same row if a match appears
- insert a whole new row with the information at the bottom if it doesn't
- then use cells to make a folder path, send the checked file to it, and delete it from the upload folder
- repeat for all files in the folder
What I have at the moment only seems to use ONLY the first file to update the master list, then everything else gets filed away and cleaned up without updating.
It's close to working, but I can't quite get that final step.
Here's what I have so far:
Sub KAMtemplateupload()
Application.ScreenUpdating = False
Dim wb As Workbook
Dim y As Workbook
Dim actrow As Integer
Dim folderPath As String
Dim filename As String
folderPath = "C:\test\test\"
filename = Dir(folderPath & "*.xlsx")
Do Until filename = ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)
Set y = Workbooks.Open("C:\test\KAM - Account Plan spreadsheet template -sample.xlsx")
actrow = y.Sheets("Sheet1").Cells(y.Sheets("Sheet1").Rows.Count, "A").End(xlUp).Offset(1).Row
Dim r As Variant
Dim Lastrow As Long
Dim rng As Range
With y.Sheets("Sheet1")
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng = .Range("A:A")
r = Application.Match(wb.Sheets("Sheet1").Range("E5"), rng, False)
If Not IsError(r) Then
'update
.Cells(CLng(r), 3).Value = wb.Sheets("Sheet1").Range("C6")
.Cells(CLng(r), 4).Value = wb.Sheets("Sheet1").Range("E6")
Else
'new
'name
wb.Sheets("Sheet1").Range("C5").Copy
y.Sheets("Sheet1").Range("B" & actrow).PasteSpecial xlPasteValues
'acctcode
wb.Sheets("Sheet1").Range("E5").Copy
y.Sheets("Sheet1").Range("A" & actrow).PasteSpecial xlPasteValues
'policytotal
wb.Sheets("Sheet1").Range("C6").Copy
y.Sheets("Sheet1").Range("C" & actrow).PasteSpecial xlPasteValues
'membertotal
wb.Sheets("Sheet1").Range("E6").Copy
y.Sheets("Sheet1").Range("D" & actrow).PasteSpecial xlPasteValues
End If
End With
Const MYPATH As String = "C:\test\test\"
Dim part3 As String
part3 = wb.Sheets("Sheet1").Range("E4").Value
If Len(Dir(MYPATH & part3, vbDirectory)) = 0 Then
MkDir MYPATH & part3
End If
wb.SaveCopyAs "C:\test\test\" & part3 & "\" & "ER group renewal notes - " & wb.Sheets("Sheet1").Range("C5") & ".xlsx"
wb.ChangeFileAccess xlReadOnly
Kill wb.FullName
filename = Dir(folderPath & "*.xlsx")
Loop
Application.ScreenUpdating = True
End Sub
When I run it, I also get a prompt reading "Do you want to save the changes before changing file status?"
I gather that's some sort of complication with setting the ReadOnly status, but if I don't set it to ReadOnly then it won't let me kill the file.
Any help would be hugely appreciated.
Thanks!