Multiple VBA codes on sheet1

OscarHawk

New Member
Joined
Jan 29, 2024
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
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
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I might do it slightly different.
First off don't loop the entire column
Make your button NOT visible until the entry
Hide it again after the copy

VBA Code:
Private Sub Worksheet_Activate()  ' you could put this code on workbook open
    ActiveSheet.Shapes("CommandButton1").Visible = False
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim lr As Long

    'Unprotect ' imcomment back
    
    Application.EnableEvents = False

    lr = Me.Cells(Rows.Count, "C").End(xlUp).Row
    Set rng = Me.Range("C1:C" & lr)
    
    If Not Intersect(rng, Target) Is Nothing Then
        If Not IsEmpty(rng.Value) Then
            rng.Offset(, -1).Value = Now
            rng.Offset(0, -1).NumberFormat = "hh:mm"
        Else
            rng.Offset(0, -1).ClearContents
        End If
    End If
    
    Application.EnableEvents = True

    'Protect ' uncomment back
    
    ' make your command button visible here
    ' after you do your save make the button NOT visible
    ActiveSheet.Shapes("CommandButton1").Visible = True
    ' handle your save anyway you want
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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