Hi, Looking for some help on this subject.
I currently have 2 VBA codes that will work independently, e.g. both work effectively alone but not together.
The first of these codes auto populates the time in each adjacent cell of column B when data is added to Column C
The second code is assigned to a button and saves the file with a new file number when complete, dependant of the date e.g. CalorieCounter-020224
Both codes are shown below - Is there any way of being able to run both of these codes on the same sheet.
Any help is greatly appreciated
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Unprotect
Set WorkRng = Intersect(Application.ActiveSheet.Range("C:C"), Target)
xOffsetColumn = -1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "hh:mm"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
Protect
End Sub
Private Sub CommandButton1_Click()
Dim path As String
path = "E:\Calorie Counter\"
Dim CalCo As Long
CalCo = Range("D2")
Dim fname As String
fname = CalCo & " - " & Range("J2")
Application.DisplayAlerts = False
'1) Create a copy of the Calorie Counter in a new workbook
wsInput.Copy
ActiveSheet.Shapes("CommandButton1").Delete
'2) Save the new workbook to the specified Calorie Counter folder with the proper filename (save the file as an .xlsx)
'3) Close the new Calorie Counter file
With ActiveWorkbook
.SaveAs FileName:=path & fname, FileFormat:=51
.Close
End With
'4) Display the message box with the next Calorie Counter no
MsgBox "Your next Calorie Count number is " & CalCo + 1
'5) Then update the calorie counter on the template
Range("D2") = CalCo + 1
'6) Save updates to the Calorie Counter template
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub
I currently have 2 VBA codes that will work independently, e.g. both work effectively alone but not together.
The first of these codes auto populates the time in each adjacent cell of column B when data is added to Column C
The second code is assigned to a button and saves the file with a new file number when complete, dependant of the date e.g. CalorieCounter-020224
Both codes are shown below - Is there any way of being able to run both of these codes on the same sheet.
Any help is greatly appreciated
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Unprotect
Set WorkRng = Intersect(Application.ActiveSheet.Range("C:C"), Target)
xOffsetColumn = -1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "hh:mm"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
Protect
End Sub
Private Sub CommandButton1_Click()
Dim path As String
path = "E:\Calorie Counter\"
Dim CalCo As Long
CalCo = Range("D2")
Dim fname As String
fname = CalCo & " - " & Range("J2")
Application.DisplayAlerts = False
'1) Create a copy of the Calorie Counter in a new workbook
wsInput.Copy
ActiveSheet.Shapes("CommandButton1").Delete
'2) Save the new workbook to the specified Calorie Counter folder with the proper filename (save the file as an .xlsx)
'3) Close the new Calorie Counter file
With ActiveWorkbook
.SaveAs FileName:=path & fname, FileFormat:=51
.Close
End With
'4) Display the message box with the next Calorie Counter no
MsgBox "Your next Calorie Count number is " & CalCo + 1
'5) Then update the calorie counter on the template
Range("D2") = CalCo + 1
'6) Save updates to the Calorie Counter template
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub