VBA Copy filename to another excel file that serves as database

miguel_z

New Member
Joined
May 13, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello dear colleagues. Perhaps this topic was already discussed but I was unable to find similar thread so here's the problem.

I have a certain excel file. I am trying to write a code that will perform following steps.

By clicking on a button I would like to save a copy of this file with a speficific name that I am extracting from cell "E5" to a specific location and then write the name this file into a "database" file into a certain folder.
I am new to VBA and I tried to prepare the code myself but there is something wrong with it.

Here is the whole code:

VBA Code:
Sub save_filename()

Dim Path As String
Dim filename As String
Dim wbCopy As Workbook
Dim wbDest As Workbook
Dim Fname As String
Dim lDestLastRow As Long

'Message box for file save confirmation
If MsgBox("Do you want to write a copy of this file into m:\files ?", vbYesNo + vbQuestion) = vbYes Then

Path = "m:\files"

filename = Range("E5").Value & ".xlsm"

ActiveWorkbook.SaveAs Path & filename

End If

'Defining source and destination workbooks
Set wbCopy = ActiveWorkbook
Set wbDest = Workbooks.Open("m:\files\DATABASE.xlsx")

'Copying filename that is located in "E5" cell
wbCopy.Sheets("Raport A3").Range("E5").Copy
lDestLastRow = wbDest.Cells(wbDest.Rows.Count, "A").End(xlUp).Offset(1).Row
wbDest.Sheets("Sheet1").Range("A" & lDestLastRow).Paste
 
Workbooks("DATABASE.xlsx").Close SaveChanges:=True
ActiveWorkbook.Close

Application.DisplayAlerts = False
Application.DisplayAlerts = True


End Sub

I am getting a "Run-time error 438 - object doesnt support this property or method" in this part of the code:

VBA Code:
lDestLastRow = wbDest.Cells(wbDest.Rows.Count, "A").End(xlUp).Offset(1).Row

Could someone please help me to understand why this error occurs?

Many thanks...
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
A few issues I see right off the bat (without diving too far into it):

1. Do NOT use reserved names (names of existing Excel/VBA functions, properties, methods, etc) like "Path" as the name of your variables, procedure names, or function names.
Doing so can cause errors and unexpected results.

2. You do not have a slash after "m:\files" in your path (you should have "m:\files\").

3. You have a workbook, but no worksheet reference here:
Rich (BB code):
lDestLastRow = wbDest.Cells(wbDest.Rows.Count, "A").End(xlUp).Offset(1).Row
I don't think Excel likes that - using a Workbook reference but no Worksheet reference.
 
Upvote 0
Thanks for reply @Joe4
I will correct points number 1 & 2.

Could you be more specific about your third remark? How should I declare the worksheet there?

Many thanks...
 
Upvote 0
Look at here how you have both the workbook and worksheet references:
Rich (BB code):
wbCopy.Sheets("Raport A3").Range("E5").Copy

Here, you only have a workbook reference, but no worksheet reference:
Rich (BB code):
lDestLastRow = wbDest.Cells(wbDest.Rows.Count, "A").End(xlUp).Offset(1).Row

I am pretty sure Excel does not like that.
I am not sure that even with a worksheet reference, if Excel will like that you include the workbook reference in there (it didn't in my testing).

Since the destination workbook seems to be the open one at the time that line of code is hit, I think you just need the sheet reference in there, i.e.
Rich (BB code):
lDestLastRow = Sheets("sheetname").Cells(Sheets("sheetname").Rows.Count, "A").End(xlUp).Offset(1).Row
Just replace "sheetname" with the actual name of the sheet.
 
Upvote 0

Forum statistics

Threads
1,223,725
Messages
6,174,134
Members
452,546
Latest member
Rafafa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top