To copy the worksheet to a new workbook and rename it based on a specified cell value and save

Kenor

Board Regular
Joined
Dec 8, 2020
Messages
116
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

My previous post is about copying the worksheet and renaming it based on a specified cell value by using the click button.
But still in the same workbook.
See below the VBA code:-

Sub RectangleBeveled1_Click()
Dim ShtName As String

ShtName = Range("L1").Value & " " & Range("L2").Value & " " & Range("L5").Value
If ShtName = "" Then
MsgBox "no name entered"
Exit Sub
ElseIf Evaluate("isref('" & ShtName & "'!A1)") Then
MsgBox "Sheet name " & ShtName & " is already used"
Exit Sub
End If
ActiveSheet.Copy , Sheets(Sheets.Count)
ActiveSheet.Name = ShtName
End Sub



And then, I created another click button to clear the content on the existing worksheet.
I use the below VBA code:-

Sub RectangleBeveled2_Click()

Range("K1:M1").ClearContents
Range("B5:D5").ClearContents
Range("L5:M5").ClearContents
Range("B9:M16").ClearContents
Range("B20:M27").ClearContents

End Sub



So now, I need help to combine both VBA codes and make it copy the worksheet to a new workbook without opening it.

The purpose is to use the current worksheet only to enter daily data.
But can make one copy of the data to a new workbook every day with the press of a click button.

The image below is a list of daily data files in one folder.

1655714159350.png
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I tried the VBA code below but can only copy it in the same workbook as well.

There are some errors. I have no idea.



Sub Rectangle3_Click()

Dim path As String
path = "C:\Bent Hole Data\New Data\"

Dim ShtName As String

ShtName = Range("K1").Value & " " & Range("L5").Value
If ShtName = "" Then
MsgBox "no name entered"
Exit Sub
ElseIf Evaluate("isref('" & ShtName & "'!A1)") Then
MsgBox "Sheet name " & ShtName & " is already used"
Exit Sub
End If

Application.DisplayAlerts = False

ActiveSheet.Copy , Sheets(Sheets.Count)
ActiveSheet.Name = ShtName

With ActiveWorkbook
.SaveAs Filename:=path & ShtName, FileFormat:=51
.Close

End With

Application.DisplayAlerts = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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