Use VBA to write VBA in new worksheet

BODYCOTE

New Member
Joined
May 18, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I currently have a master sheet that costs drinks, when it is ready I hit 'Save Duplicate' button and it copies the worksheet to the left of the current sheet and renames it based on cell 'B2'

VBA Code:
Public Sub CopySheetAndRenameByCell()
        ActiveSheet.Select
        ActiveSheet.Unprotect Password:="lavish123"
Range("h17").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(B2,Table60,4,FALSE)*1000"
Range("D4:D14").Sort _
Key1:=Range("D4"), Header:=xNo
        ActiveSheet.Select
    ActiveSheet.Protect Password:="lavish123", DrawingObjects:=True, Contents:=True, Scenarios:=True
    Dim wks As Worksheet
    Set wks = ActiveSheet
    ActiveSheet.Copy After:=Worksheets("****TAIL COSTER")
    If wks.Range("B2").Value <> "" Then
        On Error Resume Next
        ActiveSheet.Name = wks.Range("B2").Value
    End If
 
    wks.Activate
   
    Sheets("****TAIL COSTER").Select
    Range("B2:m2").Select
    ActiveCell.FormulaR1C1 = "*****TAIL NAME HERE*"
    Range("B4:d14").Select
    Selection.ClearContents
    Range("C19:H19").Select
    Selection.ClearContents
    Range("B22").Select
    Selection.ClearContents
    Range("B24").Select
    Selection.ClearContents
    Range("B26").Select
    Selection.ClearContents
    Range("f17").Select
    Selection.ClearContents
    Range("h17").Select
    Selection.ClearContents
    Range("d19:i19").Select
    Selection.ClearContents
    Range("h17").Select
    Selection.ClearContents

End Sub


1621343756951.png


I also want the 'save duplicate' vba to use the following in the sheet to allow 'B2' to control the value of the worksheet name:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("b2")) Is Nothing Then
        ActiveSheet.Name = ActiveSheet.Range("b2")
    End If
End Sub

is it possible for my button to edit vba sheets?
 
Last edited by a moderator:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
If the source sheet contains the VBA event procedure so the new sheet has it also …​
 
Upvote 0
If the source sheet contains the VBA event procedure so the new sheet has it also …​
I don't want the source sheet to contain the vba as the sheet name must always remain '****TAIL COSTER'
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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