I want to create cell wise log. The first image shows concat value in the third column. Now if I change the value in the first and second columns, I want the new concat value to be added [append] in the third column, not removing old data.
How to do that?
Solved the issue with VBA in Excel. added comment in this below code.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo myerror
Application.EnableEvents = False
Dim c As Range
'K1:AC1 is the column range where the date field exits
For Each c In Range("K1:AC1")
'if the cell value has the current date
If c.Value = Date Then
'if column E is blank
If Cells(Target.Row, "E").Value = "" Then
Cells(Target.Row, c.Column).Value = "created today"
'if column E is not blank
Else
'Cells(Target.Row, c.Column).Value = 'created today'
'Cells(Target.Row, "H").Value = 'Gobinda Nandi (Dev)'
'Cells(Target.Row, "E").Value = '(Status: In Progress)'
'Cells(Target.Row, "J").Value = 'WIP'
Cells(Target.Row, c.Column).Value = Cells(Target.Row, c.Column).Value & vbNewLine & "[" & Format(Now, "HH:MM Am/Pm") & "] " & Cells(Target.Row, "H").Value & " (Status: " & Cells(Target.Row, "E").Value & "), Comment: " & Cells(Target.Row, "J").Value
'OUTPUT:
'created today
'[11:37 AM] Gobinda Nandi (Dev) (Status: In Progress), Comment: WIP
End If
End If
Next c
'making empty cell
Cells(Target.Row, "J").Value = ""
myerror:
Application.EnableEvents = True
'error handling code
End Sub
OUTPUT:
Related
I am fairly new to writing code in excel VBA. Most of this code is some I have tried to replicated based on what other people have wrote. The problem I am having is I have a quantity counter and when a barcode is scanned into the cell (A4) it will add the barcode to a new cell (Starts at C8 and goes down) and if this barcode is already scanned once and is scanned again it will add one to the quantity. Now I am trying to add a date and time next to it as a barcode is scanned. This works but has an issue I can't figure out. The barcode must be scanned twice for the date to appear in the proper cell. This is an issue because it raises the quantity up one more than it should. Please help.
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Address
Case "$A$4"
If Target.Value <> "" Then
On Error Resume Next
Set xitem = Range("C8", Range("C" & Rows.Count) _
.End(xlUp)).Find(Range("A4").Value)
With xitem.Offset(0, -1)
.Value = .Value + 1
.Offset(0, 1).Select
End With
With xitem.Offset(0, 1)
.Value = Date & " " & Time
.NumberFormat = "m/d/yyyy h:mm AM/PM"
End With
On Error GoTo 0
If xitem Is Nothing Then
With Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
.Value = Target.Value
.Offset(0, -1) = 1
End With
End If
Range("A4") = ""
Range("A4").Select
End If
Adds quantity
Case "$C$4"
If Target.Value <> "" Then
On Error Resume Next
Set xitem = Range("C8", Range("C" & Rows.Count).End(xlUp)) _
.Find(Range("C4").Value)
With xitem.Offset(0, -1)
.Value = .Value - 1
End With
If xitem Is Nothing Then MsgBox Target & " cannot be found " _
& "and cannot be removed."
Range("C4") = ""
Range("C4").Select
On Error GoTo 0
End If
Removes quantity (I am going to add an out time to this just trying to get the initial scan time in first)
Case "$E$4" 'find
If Target.Value <> "" Then
On Error Resume Next
Set xitem = Range("C8", Range("C" & Rows.Count).End(xlUp)) _
.Find(Range("E4").Value)
If xitem Is Nothing Then
MsgBox Target & " was not found."
Range("E4").Select
End If
Range("E4") = ""
xitem.Select
On Error GoTo 0
End If
End Select
End Sub
This is what I am using to take me directly to a barcode that has already been scanned.
Sorry if this post is badly formatted never posted before. Any and all help with this issue is appreciated. A photo of the spread sheet is also attached.
You are repeating some things within your code which you only need to do once, like the Find() for example.
Here's one alternative approach:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Adding As Boolean, Finding As Boolean, Subtracting As Boolean
Dim f As Range, v
Select Case Target.Address(True, True)
Case "$A$4": Adding = True
Case "$C$4": Subtracting = True
Case "$E$4": Finding = True
Case Else: Exit Sub
End Select
v = Trim(Target.Value)
If Len(v) = 0 Then Exit Sub
Set f = Me.Range("C8").Resize(1000, 1).Find(v, lookat:=xlWhole)
If Adding Then
If f Is Nothing Then
'not found: add as new row
Set f = Me.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
f.Value = v
End If
f.Offset(0, 1).Value = f.Offset(0, 1).Value + 1
doDate f.Offset(0, -1)
Target.Value = ""
ElseIf Subtracting Then
If f Is Nothing Then
MsgBox v & " not found for subtraction!"
Else
f.Offset(0, 1).Value = f.Offset(0, 1).Value - 1
doDate f.Offset(0, -1)
Target.Value = ""
End If
Else 'finding
If Not f Is Nothing Then
f.EntireRow.Select
Target.Value = ""
Else
MsgBox v & " not found."
End If
End If
If Adding Or Subtracting Then Target.Select
End Sub
Sub doDate(c As Range)
With c
.NumberFormat = "m/d/yyyy h:mm AM/PM"
.Value = Now
End With
End Sub
I have this simple bit of code that automates some dates and stuff when adding line items to a sheet. It works well, but when I insert a line in to the spreadsheet [right-click the line name > insert] an error occurs.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim plusWeek
plusWeek = Now() + 7
For Each cell In Target
'========adds closed date, deleted date if status degenerates=========
If cell.Column = 13 And cell = "Closed" Then
Target.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
End If
If cell.Column = 13 And cell = "In-Progress" Then
Target.Offset(0, -2) = ""
End If
If cell.Column = 13 And cell = "Open" Then
Target.Offset(0, -2) = ""
End If
'========adds date added if date is embty and description is not empty========
If cell.Column = 8 And IsEmpty(Target.Offset(0, 1)) And Not IsEmpty(Target.Offset(0, 0)) Then
Target.Offset(0, 1) = Format(Now(), "yyyy-mm-dd")
Target.Offset(0, 2) = Format(plusWeek, "yyyy-mm-dd")
Target.Offset(0, 5) = "Open"
End If
'========deletes date added if description is empty========
'If cell.Column = 8 And IsEmpty(Target.Offset(0, 0)) Then
' Target.Offset(0, 1) = ""
'End If
Next cell
End Sub
if I paste a line, add a line or delete a line, error 1004 occurs. The debugger highlights this line, but I can't understand where the error comes from.
If cell.Column = 8 And IsEmpty(Target.Offset(0, 1)) And Not
IsEmpty(Target.Offset(0, 0)) Then
Something like this should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range, rng As Range
Dim plusWeek
plusWeek = Now() + 7
Set rng = Application.Intersect(Target, Me.Range("H:H,M:M"))
If rng Is Nothing Then Exit Sub
On Error GoTo haveError '<< make sure events don't get left turned off
Application.EnableEvents = False '<< turn events off
For Each cell In rng.Cells
'========adds closed date, deleted date if status degenerates=========
If cell.Column = 13 Then
Select Case cell.Value
Case "Closed": cell.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
Case "In-Progress", "Open": cell.Offset(0, -2) = ""
End Select
End If
'========adds date added if date is embty and description is not empty========
If cell.Column = 8 And IsEmpty(cell.Offset(0, 1)) And Not IsEmpty(cell) Then
cell.Offset(0, 1) = Format(Now(), "yyyy-mm-dd")
cell.Offset(0, 2) = Format(plusWeek, "yyyy-mm-dd")
cell.Offset(0, 5) = "Open"
End If
'========deletes date added if description is empty========
'If cell.Column = 8 And IsEmpty(Target.Offset(0, 0)) Then
' Target.Offset(0, 1) = ""
'End If
Next cell
haveError:
Application.EnableEvents = True
End Sub
I am trying to create a nonvolatile date stamp in Column A cells as entries are made in B, C and D cells in the same row.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 2 To 10000
If Cells(i, “B”).Value <> “” And _
Cells(i, “C”).Value <> “” And _
Cells(i, “D”).Value <> “” And _
Cells(i, “A”).Value = “” Then
Cells(i, "A").Value = Date & " " & Time
Cells(i, "A").NumberFormat = "m/d/yyyy h:mm AM/PM"
End If
Next
Range("A:A").EntireColumn.AutoFit
End Sub
I made it go to 10000 for the simple fact I do not know how to tell it to go as long as entries are entered.
It appears that you want to receive a datestamp once columns B:D are filled and column A is still empty.
If you write values back to the worksheet, disable event handling and provide error control.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("B:D"), Target) Is Nothing Then
On Error GoTo exit_handler
Application.EnableEvents = False
Dim r As Range
For Each r In Intersect(Range("B:D"), Target).Rows
If Cells(r.Row, "B").Value <> vbNullString And Cells(r.Row, "C").Value <> vbNullString And _
Cells(r.Row, "D").Value <> vbNullString And Cells(r.Row, "A").Value = vbNullString Then
Cells(i, "A").Value = Now
Cells(i, "A").NumberFormat = "mm/dd/yyyy h:mm AM/PM"
End If
Next t
End If
exit_handler:
Application.EnableEvents = True
End Sub
Try this to get rid of the loop:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
If Target.Count = 1 And Target.Column > 1 And Target.Column < 5 Then
If Cells(Target.Row, "B").Value <> "" And Cells(Target.Row, "C").Value <> "" And Cells(Target.Row, "D").Value <> "" And Cells(Target.Row, "A").Value = "" Then
Cells(Target.Row, 1).Value = Now
Cells(Target.Row, 1).NumberFormat = "m/d/yyyy h:mm AM/PM"
Range("A:A").EntireColumn.AutoFit
End If
End If
End Sub
In short, when you make a change on column B C or D, it will check if All 3 for that Row are filled and then put the time stamp if it doesnt have one. Skipping the loop. If you are pasting data instead of typing it, it will not work, instead use the loop from Pawel's answer.
I dynamically update the cells in columns A and B, and join both values on each row (using &) and place the values in column C.
My purpose is fulfilled by detecting duplicate names when firstName (Column A values) and LastName (column B values) are entered twice. An empty value (observed when the msgbox is displayed) pops up when I delete the duplicate name followed by the first occurrence.
This is an issue at times, especially because sometimes the msgbox does not go away. ie the code crashes.
How can I prevent the empty value, or msgBox from being displayed? I suspect something is wrong with my if statement.
VBA code I placed in the worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If WorksheetFunction.CountIf(Range("c1:c12"), Target.Offset(0, 1).Value) > 1 And _
Target.Offset(0, 1).Value <> " " Then
MsgBox Target.Offset(0, 1).Value & " is a Duplicate Entry" & vbNewLine & _
" ENTER A NEW NAME", vbInformation, "Duplicate Detected"
Target.Offset(0, 0).Value = " "
Target.Offset(0, 0).Select
ElseIf WorksheetFunction.CountIf(Range("c1:c12"), Target.Offset(0, 2).Value) > 1 And _
Target.Offset(0, 1).Value <> " " Then
MsgBox Target.Offset(0, 2).Value & " is a Duplicate Entry" & vbNewLine & _
" ENTER A NEW NAME", vbInformation, "Duplicate Detected"
Target.Offset(0, 0).Value = " "
Target.Offset(0, 0).Select
Else: Exit Sub
End If
End Sub
If i wanted to create a sheet with
-2 -1 0
ColA ColB ColC
First1 Last1 First1Last1
First2 Last2 First2Last2
First3 Last3 First3Last3
First4 Last4
I would personally start with conditional formatting for ColC to flag what is a duplicate, in case there is an issue, which circumvents a messagebox.
If i did need a messagebox, i would set up similar to what you have:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Columns(3)) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Application.CountIfs(Range("C1:C12"),Target.Value) > 1 Then 'checks for first/last name
MsgBox("The name " & Target.Offset(0,-2).Value & " " & Target.Offset(0,-1).Value & " already exists." & vbNewLine & "Please enter a new name.")
End If
End Sub
Edit1:
Given the data entry for colA and colB, would this be more appropriate? I utilized the row of the target, so the negative offset shouldn't be of concern, since you know that colA is first name and colB is last name.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Dim r as long
r = target.row
If isempty(cells(r,1)) or isempty(cells(r,2)) then exitsub
If Application.CountIfs(Range("B1:B12"),cells(r,2).Value,Range("A1:A12"),cells(r,1).Value) > 1 Then 'checks for first/last name
MsgBox("The name " & cells(r,1).Value & " " & cells(r,2).Value & " already exists." & vbNewLine & "Please enter a new name.")
End If
End Sub
Edit2:
In verifying the use of no values and some values, this macro has been working for my testing (i added the clear contents and .select so you are back on the line you should be adding data); i also added a range specification related to the intersect in case you are adding values like first/last to a random place outside of a1:b12:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range(Cells(1, 1), Cells(12, 2))) Is Nothing Then Exit Sub
Dim r As Long
r = Target.Row
If IsEmpty(Cells(r, 1)) Or IsEmpty(Cells(r, 2)) Then Exit Sub
If Application.CountIfs(Range("B1:B12"), Cells(r, 2).Value, Range("A1:A12"), Cells(r, 1).Value) > 1 Then 'checks for first/last name
MsgBox ("The name " & Cells(r, 1).Value & " " & Cells(r, 2).Value & " already exists." & vbNewLine & "Please enter a new name.")
Cells(r, 1).ClearContents
Cells(r, 2).ClearContents
Cells(r, 1).Select
End If
End Sub
I been having an issue. What I'm trying to accomplish is compare four columns, if the cells match then return two cells in the same row.
For an example I'm comparing both A&B to D&E with an output of F&G on the same row. The destination doesn't matter much as I can change it.
What I have done only compares two columns, which works, but it also adds other cells that shouldn't apply to that particular line.
Sub Add_XY()
For Each cell In ThisWorkbook.Sheets("Data").UsedRange.Columns("K").Cells
Dim offs As Long: offs = 2 ' <-- Initial offset, will increase after each match
compareValue = cell.Value & "-" & cell.Offset(, 1).Value
ThisWorkbook.Sheets("Data").Range("K6").Value = compareValue
If Not compareValue = "-" Then
For Each compareCell In ThisWorkbook.Sheets("P&T Data").UsedRange.Columns("AI").Cells
'For Each compareCell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
If compareCell.Value & "-" & compareCell.Offset(, 1).Value = compareValue Then
ThisWorkbook.Sheets("Data").Range("K6").Value = compareCell.Value & "-" & compareCell.Offset(, 1).Value 'test return value
cell.Offset(, offs).Value = compareCell.Offset(, 5).Value
cell.Offset(, offs + 1).Value = compareCell.Offset(, 6).Value
offs = offs + 4 ' <-- now shift the destination column by 4 for next match
Else
End If
Next compareCell
End If
Next cell
End Sub
Working with the data entered exactly as shown in your picture.
Sub Test()
For Each cell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
compareValue = cell.Value & "-" & cell.Offset(0, 1).Value
If Not compareValue = "-" Then
For Each compareCell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
If compareCell.Offset(0, 3).Value & "-" & compareCell.Offset(0, 4).Value = compareValue Then
cell.Offset(0, 8) = cell.Offset(0, 5)
cell.Offset(0, 9) = cell.Offset(0, 6)
Else
End If
Next compareCell
End If
Next cell
End Sub