Add a sheet with dynamic Sheet name in Excel as DATE - excel

I use this code to create a new sheet with a dynamic name based on the real-time but it is not successful run, show
error 1004 "application-defined or object-defined error".
Private Sub Compute_Click()
With ThisWorkbook
Worksheets("Data").Range("A1").Value = Now
If IsDate(Worksheets("Data").Range("A1")) Then
Set Worksheet = .Sheets.Add(After:=.Sheets(.Sheets.Count))
Worksheet.Name = Format(Range("A1"), "MM-DD-YYYY hh-mm-ss")
End If
End With
End Sub

A couple of points:
Use something other than Worksheet as your variable.
You haven't specified which worksheet Range("A1") is on. That means there's an implicit ActiveSheet, which may not be (is not) the Data sheet.
Now is a date so the IsDate check is unnecessary.
All that said, your revised code might look something like this:
Private Sub Compute_Click()
With ThisWorkbook
Dim ws As Worksheet
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = Format(Now, "MM-DD-YYYY hh-mm-ss")
End With
End Sub

Related

Copy image from one worksheet (always named "Template") to worksheet with variable names

I need to copy an image from my invoice template worksheet to another worksheet with variable names. For example, the name of the sheet could be "03-000008" or "04-000005" or any other name. This problem would be easy to solve if the sheets had the same name, but since they are variable, I am struggling. Any help would be much appreciated! Thank you in advance!
Tim suggested that I add the code I am working with (thanks Tim!) Here is the code that almost works, but instead of pasting the image to my new, active invoice sheet, it pastes it right on the template itself.
Sub image()
With ActiveSheet
Set i = Sheets("Template")
Set e = ActiveSheet
i.Shapes.Range(Array("Picture 4")).Select
Selection.Copy
e.Range("b1:b4").Select
ActiveSheet.Paste
End With
End Sub
Try this:
Sub image()
Dim ws As Worksheet
ThisWorkbook.Worksheets("Template").Shapes("Picture 4").Copy
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "##-######" Then 'check matches pattern [2digits-6digits]
ws.Paste Destination:=ws.Range("B1")
MsgBox "Pasted to " & ws.Name
Exit For 'no need to check further
End If
Next ws
End Sub
If all you need to do is copy the image from Template to the activesheet then:
Sub CopyLogo()
ThisWorkbook.Worksheets("Template").Shapes("Picture 4").Copy
ActiveSheet.Paste Destination:=ActiveSheet.Range("B1")
End Sub

Trying to create a excel worksheet using data from a VBA form then adding it to end of workbook

Trying to create a excel worksheet using data from a VBA form then adding it to end of workbook. Please help to activate the code
Private Sub Add_Tab_Click()
Dim txtNameSur As Worksheet
Set txtNameSur = Worksheets("Me.Textbox1")
ThisWorkbook.Sheets(1).Copy after:=Sheets(Sheets.Count)
Newname = Worksheets.Add.Name = Userform1.txtNameSur.Value
ActiveSheet.Name = Newname
End Sub
The goal in your question is a bit unclear, but I guess you meant to do something like below:
Private Sub Add_Tab_Click()
'copy first worksheet to the end of the workbook
ThisWorkbook.Worksheets(1).Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
'set the copied worksheet to a variable `NewWorksheet`
Dim NewWorksheet As Worksheet
Set NewWorksheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) 'last one is the copied one
'give it a new name (use the text in Textbox1 as name)
NewWorksheet.Name = Me.Textbox1.Text
End Sub

VBA Macro got me stumped

I'm using a macro to make a copy of the active sheet, and rename it to whatever the value of cell 'C2' is. The only problem is, that when it copies the sheet, it somehow removes the form buttons from the top of my worksheet and replaces them with the code =$c$2 in cell 'AF'.
As far as i can see from the VBA code there is nothing that refers to the cell 'AF'. Can anyone tell me why it's doing this ?
Sub Copy_Rename()
Dim shtName As String
shtName = ActiveSheet.Name
ActiveSheet.Copy before:=ActiveSheet
ActiveSheet.Name = Range("C2").Value
Sheets(shtName).Activate
End Sub
Try this:
Sub Copy_Rename()
Dim sht As Worksheet
Set sht = ActiveSheet
Application.CopyObjectsWithCells = True '<< to also copy objects not just cell contents etc
sht.Copy before:=sht
'Get the just-created sheet
With Sheets(sht.Index - 1)
.Name = sht.Range("C2").Value
.Activate
End With
End Sub

VBA Add second Sheet with same Name

I have a CommandButton which opens a UserForm and create a copied Sheet with the name of the ComboBox Value.
This is My Code:
Private Sub CommandButton1_Click()
[UserForm1].Show ' Open UserForm
End Sub
Private Sub CommandButton2_Click()
Dim ws As Worksheet
ActiveWorkbook.Sheets("Sheet1").Visible = True ' Unhide Sheet
Sheets("Sheet1").Copy _
Before:=ActiveWorkbook.Sheets("Sheet1") ' Copy Sheet
Set ws = ActiveSheet
ws.Name = ComboBox1.Value ' Name Sheet
[UserForm1].Hide ' Close UserForm
ActiveWorkbook.Sheets("Sheet1").Visible = False ' Hide Sheet again
End sub
Now my problem is, if there are two machines with name "Machine Type 1" Excel gets an Error. So what do i have to change in my code, that the second sheet would named e.g. "Machine Type 1 (2)?
Thanks for your help.
you could try this
Private Sub CommandButton1_Click()
If IsSheetThere(ComboBox1.Value) Then 'if some sheet with chosen name already there
Sheets(ComboBox1.Value).Copy Before:=Sheets(10) ' copy the existing sheet
With ActiveSheet 'reference just copied sheet
.UsedRange.Clear 'clear its content
Sheets("Sheet1").UsedRange.Copy ActiveSheet.Range("A1") ' copy Sheet1 content and paste into it
End With
Else 'otherwise
Sheets("Sheet1").Copy Before:=Sheets(Sheets.Count) ' make a copy of "Sheet1" sheet
ActiveSheet.Name = ComboBox1.Value 'and rename it as per chosen name
End If
Me.Hide
End Sub
Function IsSheetThere(shtName As String) As Boolean
On Error Resume Next
IsSheetThere = Not Sheets(shtName) Is Nothing
End Function
the code line:
Sheets(ComboBox1.Value).Copy Before:=Sheets(10) ' copy the existing sheet
is the one that leaves Excel the burden of somehow "counting" the number of already existing sheets with the chosen name, and name the new one appropriately
You can use the following sub which calls the below function, just apply the same logic using .Copy
Sub create_new_sheet_with_name(name As String, wb As Workbook, aftersheet As Variant)
Dim i As Integer
i = 2
If sheet_name_exists(name, wb) Then
Do While sheet_name_exists(name & " (" & i & ")", wb)
i = i + 1
Loop
wb.Sheets.Add(after:=aftersheet).name = name & " (" & i & ")"
Else
wb.Sheets.Add(after:=aftersheet).name = name
End If
End Sub
Function sheet_name_exists(name As String, wb As Workbook) As Boolean
For Each sheet In wb.Worksheets
If sheet.name = name Then
sheet_name_exists = True
Exit Function
End If
Next sheet
sheet_name_exists = False
End Function
here's an example of how to use the sub:
Sub test()
create_new_sheet_with_name "hi", ThisWorkbook, ThisWorkbook.Sheets(1)
'this adds a new sheet named "hi" to thisworkbook after thisworkbook.sheets(1)
End Sub
Technically this isn't an answer to this question... but it's better because it will help you solve this and many other coding tasks on your own.
There is a simple way to create VBA code for most basic tasks.
If there's something Excel can do that you want to be able to do programmatically, just Record a Macro of yourself performing the action(s), and then look at the code that Excel generated.
I have a terrible memory, I can't remember commands I used yesterday. So it's not only quicker and less frustrating for others for me to figure it out myself, but the more often I do that, the quicker I'll learn (without asking others to do the thinking for me on a basic question).
I fact, I'm guess that the majority of veteran VBA coders learned at least partly by analyzing recorded macros. I know I did.

Set Print Area for All Visible Sheets in Workbook

Attempting to set the print area for all visible sheets in my workbook however I am getting an "object required" error on line PageSetup.PrintArea = "$B$2:$K$55
Any help on how to fix this error is appreciated.
Here is a copy of my full code.
Private Sub YesPrint_Click()
Dim Sheet As Worksheet
Dim CompareTool As Workbook
Dim Sheetname As String
Set CompareTool = ThisWorkbook
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
For Each Sheet In CompareTool.Worksheets
If Sheet.Visible = True Then
Sheet.Activate
With ActiveSheet.Name
Range("B2:K55").Select
PageSetup.PrintArea = "$B$2:$K$55"
End With
End If
Next Sheet
End Sub
It looks like there are two issues with the code provided.
First, There is a syntax error in the with statement. Each property accessed inside the with statement should have a dot before it.
Second, the ActiveSheet.Name is not the object that includes the Range and PageSetup Method/object. It's the ActiveSheet object.
The corrected code should look like:
With ActiveSheet
.Range("B2:K55").Select
.PageSetup.PrintArea = "$B$2:$K$55"
End With
The MSDN article for PageSetup provides an example as well:
https://msdn.microsoft.com/en-us/library/office/ff196103.aspx

Resources