Doug Mutzig
Board Regular
- Joined
- Jan 1, 2019
- Messages
- 57
- Office Version
- 365
- Platform
- Windows
Good morning all!
I am hoping you can help me as I am lost on exactly how to accomplish what I want. I have several VBA codes that do different things that I would like to combine if possible.
First I have a 2 codes that add a comment to a cell when the cell’s value is changed. One code seems to work but overwrites the comment each time it is updated (i.e. if I change the value from 1 to 2 I see the comment, if the then change the value from 2 to TD I only see the original 1 listed).
Code by: jfreitag on this post: https://www.mrexcel.com/forum/excel-questions/662266-excel-vba-add-comment-change-cell-value.html
Code:
I have changed the Range to be a named range (P1Dsched) and removed the Chr(10) linefeeds from the comment text so that everything should be on one line.
The second code does a history of changes by adding to the comment each time a change is made:
Example: 1st change from 1 to 2, 2nd change from 2 to td
Comment shows: Doug Mutzig – td
Doug Mutzig – 2
However, I cannot seem to get the code to run in my workbook.
Code posted here: https://www.youtube.com/watch?v=-gWAsXlfHRI
Code:
What I would like to do is get the best of both codes by:
To add to the complexity I currently have code running in the Worksheet_Change event that looks at the selected ranges (both a day and night named ranges) and makes the texted entered into the cells Uppercase. I am unsure of how to combine everything together or if I can have more than one Worksheet_Change section per worksheet?
I apologize I am not sure where I got the code from.
Code:
I would like to merge the modified 1st code that adds comments, with the 2nd code that changes text to Upper case and add one other code that adds a cell interior fill when a change happens, if a specified cell is not empty.
Code posted by: Gary's Student post from here:https://www.mrexcel.com/forum/excel-questions/662503-change-cells-color-when-any-value-changes.html
Code:
I have modified by to the following:
Code:
Is it possible to combine all three codes into one? Is that the most efficient way to do it? or should I have multiple worksheet changes?
Thank you all very much for your help on this!
I am hoping you can help me as I am lost on exactly how to accomplish what I want. I have several VBA codes that do different things that I would like to combine if possible.
First I have a 2 codes that add a comment to a cell when the cell’s value is changed. One code seems to work but overwrites the comment each time it is updated (i.e. if I change the value from 1 to 2 I see the comment, if the then change the value from 2 to TD I only see the original 1 listed).
Code by: jfreitag on this post: https://www.mrexcel.com/forum/excel-questions/662266-excel-vba-add-comment-change-cell-value.html
Code:
Code:
Option Explicit
Public preValue As Variant
Private Sub worksheet_change(ByVal Target As Range)
If Intersect(Target, Range("J4:P4")) Is Nothing Or Target.Cells.Count > 1 Then Exit Sub
Target.ClearComments
Target.AddComment.Text Text:="Previous Value was " & preValue & Chr(10) & Chr(10) & "Modified " & Format _
(Date, "mm-dd-yyyy") & Chr(10) & Chr(10) & "By " & Environ("UserName")
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target = "" Then
preValue = "a blank"
Else: preValue = Target.Value
End If
End Sub
I have changed the Range to be a named range (P1Dsched) and removed the Chr(10) linefeeds from the comment text so that everything should be on one line.
The second code does a history of changes by adding to the comment each time a change is made:
Example: 1st change from 1 to 2, 2nd change from 2 to td
Comment shows: Doug Mutzig – td
Doug Mutzig – 2
However, I cannot seem to get the code to run in my workbook.
Code posted here: https://www.youtube.com/watch?v=-gWAsXlfHRI
Code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
Dim ws As Worksheet
For Each c In Target If c.Comment Is Nothing And c.Value = "" Then
With c.AddComment
.Visible = False
.Text Application.UserName & "-" & c.Value
End With
ElseIf Not c.Comment Is Nothing And c.Value = "" Then
c.Comment.Text Application.UserName & "-" & c.Value & vbNewLine & c.Comment.Text
End If
Next
End Sub
What I would like to do is get the best of both codes by:
- Having the comment track the changes as a growing list like the second code, and work like the first code. (I believe the Target.ClearComments part of the 1st code need to be removed so that previous comments stay, and then a new line added but I am not sure how to accomplish this.)
- Expand the comment box so that each comment on a change shows on one line
- Enable the code to work on 2 different ranges on the worksheet. I have a day and a night schedule (P1Dsched, P1Nsched) that I would like to have the code run on.
To add to the complexity I currently have code running in the Worksheet_Change event that looks at the selected ranges (both a day and night named ranges) and makes the texted entered into the cells Uppercase. I am unsure of how to combine everything together or if I can have more than one Worksheet_Change section per worksheet?
I apologize I am not sure where I got the code from.
Code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Target, Range("P1Dsched")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
If Not (Application.Intersect(Target, Range("P1Nsched")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Target, Range("P5Dsched")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
If Not (Application.Intersect(Target, Range("P5Nsched")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False
.Value = UCase(.Value)
Application.EnableEvents = True
End If
End With
End If
End Sub
I would like to merge the modified 1st code that adds comments, with the 2nd code that changes text to Upper case and add one other code that adds a cell interior fill when a change happens, if a specified cell is not empty.
Code posted by: Gary's Student post from here:https://www.mrexcel.com/forum/excel-questions/662503-change-cells-color-when-any-value-changes.html
Code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)Dim Rng As Range, r As Range
Set Rng = Range("C26:M40")
If Intersect(Target, Rng) Is Nothing Then Exit Sub
For Each r In Target
Application.EnableEvents = False
r.Interior.ColorIndex = 4
Application.EnableEvents = True
Next
End Sub
I have modified by to the following:
Code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, r As Range
Set Rng = Range("P6Dsched")
Set rRng = ActiveSheet.Range("B8")
If IsEmpty(rRng.Value) Then Exit Sub
If Intersect(Target, Rng) Is Nothing Then Exit Sub
For Each r In Target
Application.EnableEvents = False
r.Interior.ColorIndex = 46
Application.EnableEvents = True
Next
End Sub
Is it possible to combine all three codes into one? Is that the most efficient way to do it? or should I have multiple worksheet changes?
Thank you all very much for your help on this!
Last edited by a moderator: