How can I write this VBA event so that when the new sheet is created 'newSheet.Name =…
- User
How can I write this VBA event so that when the new sheet is created 'newSheet.Name = "Task" & Format(Now(), "DDMMYY")' it copies values and cell format but not formulas
Sub StartC() Dim wbk1 As Workbook Dim ws1 As Worksheet Dim newSheet As Worksheet Set wbk1 = ThisWorkbook Set ws1 = ThisWorkbook.Worksheets("Today") Application.ScreenUpdating = False ws1.Activate With ws1.Range("G22") If Not IsDate(.Value) Then MsgBox "No date" ElseIf .Value <> Date Then ' Create new sheet without copying VBA code ws1.Range("G22").Value = ws1.Range("A22").Value Set newSheet = wbk1.Sheets.Add(After:=wbk1.Sheets(wbk1.Sheets.Count)) newSheet.Name = "Task" & Format(Now(), "DDMMYY") newSheet.Visible = False 'ws1.Cells.Copy Destination:=newSheet.Cells ws1.Range("A1:F22").Copy newSheet.Range("A1").PasteSpecial xlPasteValues Application.CutCopyMode = False Application.Wait (Now + TimeValue("0:00:01")) ws1.Visible = True newSheet.Visible = True Debug.Print "Used range in new sheet: " & newSheet.UsedRange.Address Call ClearC Else MsgBox "Date Is Today" End If End With Application.ScreenUpdating = True Application.Wait (Now + TimeValue("0:00:01")) End End Sub
- Assistant
To copy values and cell formats without formulas when creating a new sheet, you can modify the code as follows:
Sub StartC() Dim wbk1 As Workbook Dim ws1 As Worksheet Dim newSheet As Worksheet Set wbk1 = ThisWorkbook Set ws1 = ThisWorkbook.Worksheets("Today") Application.ScreenUpdating = False ws1.Activate With ws1.Range("G22") If Not IsDate(.Value) Then MsgBox "No date" ElseIf .Value <> Date Then ' Create new sheet without copying VBA code ws1.Range("G22").Value = ws1.Range("A22").Value Set newSheet = wbk1.Sheets.Add(After:=wbk1.Sheets(wbk1.Sheets.Count)) newSheet.Name = "Task" & Format(Now(), "DDMMYY") newSheet.Visible = False ' Copy values and formats without formulas ws1.Range("A1:F22").Copy newSheet.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Application.Wait (Now + TimeValue("0:00:01")) ws1.Visible = True newSheet.Visible = True Debug.Print "Used range in new sheet: " & newSheet.UsedRange.Address Call ClearC Else MsgBox "Date Is Today" End If End With Application.ScreenUpdating = True Application.Wait (Now + TimeValue("0:00:01")) End End Sub
- User
G22 value is a date in the following format mm/dd/yyyy. How can I make F2 copy the value from F22 but show it as ' Notes: mm/dd/yyyy
- Assistant
To copy the value from F22 to F2 and display it as "Notes: mm/dd/yyyy" format, you can modify the code as follows:
Sub StartC() Dim wbk1 As Workbook Dim ws1 As Worksheet Dim newSheet As Worksheet Set wbk1 = ThisWorkbook Set ws1 = ThisWorkbook.Worksheets("Today") Application.ScreenUpdating = False ws1.Activate With ws1.Range("G22") If Not IsDate(.Value) Then MsgBox "No date" ElseIf .Value <> Date Then ' Create new sheet without copying VBA code ws1.Range("G22").Value = ws1.Range("A22").Value Set newSheet = wbk1.Sheets.Add(After:=wbk1.Sheets(wbk1.Sheets.Count)) newSheet.Name = "Task" & Format(Now(), "DDMMYY") newSheet.Visible = False ' Copy values and formats without formulas ws1.Range("A1:F22").Copy newSheet.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False ' Copy F22 value to F2 and format cell as "Notes: mm/dd/yyyy" ws1.Range("F22").Copy ws1.Range("F2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False ws1.Range("F2").NumberFormat = "'Notes: 'mm/dd/yyyy" Application.CutCopyMode = False Application.Wait (Now + TimeValue("0:00:01")) ws1.Visible = True newSheet.Visible = True Debug.Print "Used range in new sheet: " & newSheet.UsedRange.Address Call ClearC Else MsgBox "Date Is Today" End If End With Application.ScreenUpdating = True Application.Wait (Now + TimeValue("0:00:01")) End End Sub
- User
I want to use a concantenated formula
- Assistant
To use a concatenated formula to display the value from F22 in F2 as "Notes: mm/dd/yyyy" format, you can modify the code as follows:
Sub StartC() Dim wbk1 As Workbook Dim ws1 As Worksheet Dim newSheet As Worksheet Set wbk1 = ThisWorkbook Set ws1 = ThisWorkbook.Worksheets("Today") Application.ScreenUpdating = False ws1.Activate With ws1.Range("G22") If Not IsDate(.Value) Then MsgBox "No date" ElseIf .Value <> Date Then ' Create new sheet without copying VBA code ws1.Range("G22").Value = ws1.Range("A22").Value Set newSheet = wbk1.Sheets.Add(After:=wbk1.Sheets(wbk1.Sheets.Count)) newSheet.Name = "Task" & Format(Now(), "DDMMYY") newSheet.Visible = False ' Copy values and formats without formulas ws1.Range("A1:F22").Copy newSheet.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False ' Copy F22 value to F2 and format cell using a concatenated formula ws1.Range("F2").Value = "Notes: " & Format(ws1.Range("F22").Value, "mm/dd/yyyy") Application.CutCopyMode = False Application.Wait (Now + TimeValue("0:00:01")) ws1.Visible = True newSheet.Visible = True Debug.Print "Used range in new sheet: " & newSheet.UsedRange.Address Call ClearC Else MsgBox "Date Is Today" End If End With Application.ScreenUpdating = True Application.Wait (Now + TimeValue("0:00:01")) End End Sub
- User
Instead of using VBA, cant I write it as a gormula
- Assistant
Yes, you can achieve the desired result by using a formula directly in the cell instead of VBA.
In cell F2, you can use the following formula:
=CONCATENATE("Notes: ", TEXT(F22, "mm/dd/yyyy"))This formula concatenates the string "Notes: " with the formatted value of cell F22 using the Text function. The result will be displayed as "Notes: mm/dd/yyyy" in cell F2.
By using this formula directly in the cell, you don't need to rely on VBA code to perform this task.
- User
In this VBA event after the line 'newSheet.Visible = True', How can I copy the newSheet.Name = "Task" & Format(Now(), "DDMMYY") to F22 in the new sheet
Sub StartC() Dim wbk1 As Workbook Dim ws1 As Worksheet Dim newSheet As Worksheet Set wbk1 = ThisWorkbook Set ws1 = ThisWorkbook.Worksheets("Today") Application.ScreenUpdating = False ws1.Activate With ws1.Range("G22") If Not IsDate(.Value) Then MsgBox "No date" ElseIf .Value <> Date Then ' Create new sheet without copying VBA code ws1.Range("G22").Value = ws1.Range("A22").Value Set newSheet = wbk1.Sheets.Add(After:=wbk1.Sheets(wbk1.Sheets.Count)) newSheet.Name = "Task" & Format(Now(), "DDMMYY") newSheet.Visible = False 'ws1.Cells.Copy Destination:=newSheet.Cells ws1.Range("A1:F22").Copy 'newSheet.Range("A1").PasteSpecial xlPasteValues newSheet.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Application.Wait (Now + TimeValue("0:00:01")) ws1.Visible = True newSheet.Visible = True Debug.Print "Used range in new sheet: " & newSheet.UsedRange.Address Call ClearC Else MsgBox "Date Is Today" End If End With Application.ScreenUpdating = True Application.Wait (Now + TimeValue("0:00:01")) End End Sub
- Assistant
To copy the value of
newSheet.Nameto cell F22 in the new sheet, you can modify the code as follows:vba