Vlookup for multiple Ranges - excel

The below code is for VLOOKUP result in same cell, which is working well, but now I also need VLOOKUP values for Range("K:L"), (R:S) and further.
My vlookup formula is this :
nx = Application.WorksheetFunction.VLookup(batch, Sheets("Batch Card REGISTER").Range("D:E"), 2, False)
Column Index no : 2
Result I need: If i type the Qty in Cl no 10, it will verify the value from the other sheet based on the 'batch'. The column index no for all the ranges is same i.e. 2
Example: If i type 100 in Cl no 10, it will verify in the "Batch Card Register" whether the value of the mentioned batch is 100 or not. If value is not 100 i have added a code so it will return the original value which is 90.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim check
Dim cl As Range
Dim mx As Double
Dim nx As Double
Dim batch
Dim rng As Range
Dim Rg As Range
ActiveSheet.Unprotect "FGIM#22"
For Each cl In Target.Cells
If Target.Column = 10 And Target.Offset(0, -3).Value = "Product_In" Then
Application.EnableEvents = False
batch = Target.Offset(0, -6)
Set Rg = Sheets("Batch Card REGISTER").Range("D:E")
nx = Application.WorksheetFunction.VLookup(batch, Sheets("Batch Card REGISTER").Range("D:E"), 2, False)
If Target.Value And nx <> Target.Value Then
MsgBox "NOTE: Value does not Match" & VBA.Constants.vbNewLine & "Orginal Value from Batch Card Register will be Restored", vbOKOnly, "ENTRY ERROR!"
Target.Value = nx
End If
Application.EnableEvents = True
End If
Verify entry in column J when "Dispatch" in is column G
If Target.Column = 10 And Target.Offset(0, -3).Value = "Dispatch" Then
Application.EnableEvents = False
batch = Target.Offset(0, -6)
Set rng = Sheets("FG Register").Columns("D:I")
mx = Application.WorksheetFunction.VLookup(Target.Offset(0, -6), Sheets("FG Register").Columns("D:I"), 6, False)
If Target.Value And mx < 0 Then
MsgBox "Value in Current Stock cannot exceed " & mx, vbOKOnly, "ENTRY ERROR!"
Target.Value = Target.Value + mx
End If
Application.EnableEvents = True
End If
If Target.Column = 10 Then
check = MsgBox("NOTE: CANNOT be edited after confirmation, Confirm the Entry?", vbYesNo, "Confirm Entry")
If check = vbYes Then
Range("A" & cl.Row & ":J" & cl.Row).Locked = True
Else
Range("C" & cl.Row & ":H" & cl.Row).Locked = False
End If
End If
Next cl
If Not Intersect(Target, Me.Range("A1:AA1000")) Is Nothing Then
ThisWorkbook.Save
End If
End Sub

Related

Automatically add comment with cell edit history having no character limit

I am using the below code from this link
Need help
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'If (Target.Row > 3 And Target.Row < 155) Then Cells(Target.Row, "AT") = Now()
Const sRng As String = "A5:AQ155" ' change as required
Dim sOld As String
Dim sNew As String
Dim sCmt As String
Dim iLen As Long
Dim bHasComment As Boolean
With Target(1)
If Intersect(.Cells, Me.Range(sRng)) Is Nothing Then
Application.EnableEvents = True
Exit Sub
End If
sNew = .Text
sOld = .Text
.Value = sNew
Application.EnableEvents = True
sCmt = "Edit: " & Format$(Now, "dd Mmm YYYY hh:nn:ss") & " by " & Application.UserName & Chr(10) & "Previous Text :- " & sOld
If Target(1).Comment Is Nothing Then
.AddComment
Else
iLen = Len(.Comment.Shape.TextFrame.Characters.Text)
End If
With .Comment.Shape.TextFrame
.AutoSize = True
.Characters(Start:=iLen + 1).Insert IIf(iLen, vbLf, "") & sCmt
End With
End With
End Sub
Sub Hide_Comments_in_Workbook_Completely()
'This macro hides the comments and comment indicators - users wont know there is a comment within the excel workbook
Application.DisplayCommentIndicator = xlNoIndicator
End Sub
The above code works fine only one problem I am facing.
It creates a history of 9 changes in 9 lines in comments if the changes exceeded or Total Character (with space) in comments is 268 more then the previous line is automatically erased.
Can anyone help me to overcome the above problem?
I want no bindings of changes or character input or line limits.
Thanks in advance and appreciate any help.
Characters is limited in the length of text you can address. You could instead delete and re-add the comment with the new text added.
This worked for me in testing:
Private Sub Worksheet_Change(ByVal Target As Range)
Const sRng As String = "A5:AQ155" ' change as required: must be a contiguous range
Dim c As Range, rng As Range, oldVals, newVals, newValsTgt, usr
Dim col As Long, rw As Long, txt As String, s As String
If Target.Areas.Count > 1 Then Exit Sub 'only handling single Area changes
Set rng = Application.Intersect(Target, Me.Range(sRng))
If rng Is Nothing Then Exit Sub
On Error GoTo haveError
Application.EnableEvents = False 'don't re-trigger event
newValsTgt = ToArray(Target) 'get current Target values
newVals = ToArray(rng) 'get current values for range of interest
Application.Undo 'restore previous values
oldVals = ToArray(rng) 'get pre-update values for range of interest
Target.Value = newValsTgt 'restore the Target range values
Application.EnableEvents = True
usr = Application.UserName
For rw = 1 To UBound(newVals, 1) 'loop over the new values
For col = 1 To UBound(newVals, 2)
If newVals(rw, col) <> oldVals(rw, col) Then 'was the content changed?
Set c = rng.Cells(rw, col)
s = "Edit: " & Format$(Now, "dd Mmm YYYY hh:nn:ss") & _
" by " & usr & Chr(10) & _
"Previous Text :- " & oldVals(rw, col)
If c.Comment Is Nothing Then
c.AddComment s
Else
txt = c.Comment.Text
c.Comment.Delete
c.AddComment s & vbLf & txt
End If
c.Comment.Shape.TextFrame.AutoSize = True
End If
Next col
Next rw
Exit Sub 'normal exit
haveError:
Debug.Print "Error: " & Err.Description
Application.EnableEvents = True 'ensure events are back on
End Sub
'returns the value of both single cells and ranges as an array...
Function ToArray(rng As Range)
Dim rv
If rng.CountLarge = 1 Then
ReDim rv(1 To 1, 1 To 1)
rv(1, 1) = rng.Value
ToArray = rv
Else
ToArray = rng.Value
End If
End Function

Worksheet_Change or Worksheet_SelectionChange event not engaging

After some time passed using Sheet27 both events stopped working. The code for the events are on Sheet27. No other Sub is called. There are four events for Sheet27
The file is located on OneDrive and
I'm using Office365 on Windows 10 latest build. I built this app for other users who may have different Excel versions (2010 to Latest) and I'm not sure if this is going to repeat in other versions. Any light shone on this issue would be greatly appreciated.
When it stopped working I had to exit Excel entirely and reopen the file. I tried closing and reopening the file but that didn't work.
I thought it could have been the graphics card because it's a little bit older but when exiting Excel and reopening file worked, it cancelled that notion.
Maybe something along the code is causing it to stop working that I cannot see due to lack of expertise.
Maybe to do with the Excel Application or the Excel Workbook itself and not the Worksheet_Change or Worksheet_SelectionChange event because it works flawlessly when the file newly opened.
Here is Sheet27 Code: I labeled the sections that stopped working
Option Explicit
Dim RowNum As Long
Private Sub Worksheet_Change(ByVal Target As Range) 'Stopped working after some use
Application.EnableEvents = False
'This section enables and disables rows 4 through 12 via cell D3 Value of 1-10, 10 being max
'Row 3 is always shown
Select Case Range("D3").Value
Case "": Range("4:12").EntireRow.Hidden = True 'If D3 is intentionally blank
Case 1: Range("4:12").EntireRow.Hidden = True
Case 2
Rows("4:4").EntireRow.Hidden = False
Rows("5:12").EntireRow.Hidden = True
Case 3
Rows("4:5").EntireRow.Hidden = False
Rows("6:12").EntireRow.Hidden = True
Case 4
Rows("4:6").EntireRow.Hidden = False
Rows("7:12").EntireRow.Hidden = True
Case 5
Rows("4:7").EntireRow.Hidden = False
Rows("8:12").EntireRow.Hidden = True
Case 6
Rows("4:8").EntireRow.Hidden = False
Rows("9:12").EntireRow.Hidden = True
Case 7
Rows("4:9").EntireRow.Hidden = False
Rows("10:12").EntireRow.Hidden = True
Case 8
Rows("4:10").EntireRow.Hidden = False
Rows("11:12").EntireRow.Hidden = True
Case 9
Rows("4:11").EntireRow.Hidden = False
Rows("12:12").EntireRow.Hidden = True
Case 10
Rows("4:12").EntireRow.Hidden = False
Case Is > 10: MsgBox "Maximum 10 employees. If you need more than 10, add more after posting these 10.", vbInformation, "Maximum 10 Rows"
End Select
Application.EnableEvents = True
'***************************************************************
'Monthly or weekly Employee
Application.EnableEvents = False
RowNum = Target.Row
If Not Intersect(Target, Range("F3:F12")) Is Nothing Then 'Employee Name Field. Dropdown list - 10 Rows - F3:F12
'Get last row in Sheet Posting to
Range("V3").Value = Worksheets(Range("B1").Value).Range("B9999").End(xlUp).Row + 1
If Range("M" & RowNum).Value = "12" Then '12=Monthly or 52=Weekly. M3 has Index/Match formula associated to Employee Name
Range("G" & RowNum).Value = "1" 'If M3=12 then Monthly paid employee value is 1 (multiplier for monthly wage on posting Month sheet)
Else
Range("G" & RowNum & ":L" & RowNum).Value = "" 'clear associated data in G3:L3 if M3=52 (weekly paid employee)
End If
End If
Application.EnableEvents = True
'***********************************************************************
'If Loan Balance is 0 or less show warning
Application.EnableEvents = False
Dim LoanDue As Variant
Dim EmpName As String
RowNum = Target.Row
EmpName = Range("F" & RowNum).Value
LoanDue = Range("P" & RowNum).Value
If Not Intersect(Target, Range("J3:J12")) Is Nothing Then
If LoanDue < 0 Then
Target.Value = ""
Target.Select
MsgBox EmpName & "'s Loan Balance is Zero." & vbNewLine & _
"Entered payment was cleared." & vbNewLine & _
"Please notify Admin on " & EmpName & "'s record to verify or make changes.", _
vbExclamation, "Loan Payment Error"
End If
End If
Application.EnableEvents = True
End Sub
Private Sub PayDateInfoOnLbl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 'This section works all the time
'Show Note
ActiveSheet.Shapes.Range(Array("TxtBxPayDate")).Visible = msoTrue
End Sub
Private Sub PayDateInfoOffLbl_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 'This section works all the time
'Hide Note
ActiveSheet.Shapes.Range(Array("TxtBxPayDate")).Visible = msoFalse
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'Stopped working after some use
Dim VacBal As String
Dim SickBal As String
Dim EmpName As String
RowNum = Target.Row
VacBal = Range("R" & RowNum).Value 'INDEX/MATCH Formula in cell to retrieve data from EmployeeInfo sheet
SickBal = Range("T" & RowNum).Value 'INDEX/MATCH Formula in cell to retrieve data from EmployeeInfo sheet
EmpName = Range("F" & RowNum).Value
If Not Intersect(Target, Range("K3:K12")) Is Nothing Then
If VacBal = "" Then Exit Sub
MsgBox EmpName & " has " & VacBal & " Vacation Day(s) remaining.", vbInformation, "Vacation Days Balance"
End If
If Not Intersect(Target, Range("L3:L12")) Is Nothing Then
If SickBal = "" Then Exit Sub
MsgBox EmpName & " has " & SickBal & " Sick Day(s) remaining.", vbInformation, "Sick Days Balance"
End If
End Sub

Comment Used To Track Changes

I have encountered a few issues with some code in VBA. I am trying to have the changes made to a cells on an excel sheet show up in comments on the cell the change was made to and I wish for these changes to be stored in a list so I can view them all later. I have tried lots of different pieces of code I have found to try and implement it into the code but none have worked.
Any ideas on how to get this to work?
Worksheet
The below code is what I am currently using
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, 2)
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, 3)
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 implemented a few formulas on the worksheet but don't see any reason why it would matter in this situation since they only track quantity of items with the same unique identifier.
I also tried some code that added comments to the cells as they were changed that worked but always returned the previous cell value as blank. It is not actually added into the current code though.
Option Explicit
Public preValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
Target.ClearComments
Target.AddComment.Text Text:="Previous Value was " & preValue & Chr(10) & "Revised " & Format(Date, "mm-dd-yyyy") & 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
By and large, the code below should do what you want. I marveled at your use of A4 and C4 to express addition and subtraction. As it is, whatever you change in those two cells, apart from clearing them, will result in a quantity of 1 being added or subtracted. I would have expected that a quantity must be entered there which is processed. If the quantity is fixed at 1 the system appears too elaborate.
Anyway, here's the code. I guess you'll be able to modify it to better suit your needs.
Private Sub Worksheet_Change(ByVal Target As Range)
' 038
Dim LookUp As Variant ' subject
Dim Action As Variant ' add = 1, subtract = -1, find = 2
Dim Fnd As Range ' Result of Find method
Dim Txt As String ' comment text
With Target
If (.Row <> 4) Or (.CountLarge > 1) Then Exit Sub
LookUp = Cells(4, "E").Value
On Error Resume Next
Action = Array(0, 1, 0, -1, 0, 2)(.Column)
End With
If Action And (LookUp <> "") Then
' C8 to end of column C
With Range(Cells(8, "C"), Cells(Rows.Count, "C").End(xlUp))
Set Fnd = .Find(LookUp, .Cells(.Cells.Count), xlValues, xlWhole, xlByRows)
End With
End If
If Fnd Is Nothing Then
Select Case Action
Case -1
MsgBox """" & LookUp & """ not found.", vbInformation, "Can't subtract"
Action = -2
Case 2
MsgBox """" & LookUp & """ not found.", vbInformation, "No record"
Action = -2
Case Else
Set Fnd = Cells(Rows.Count, "C").End(xlUp).Offset(1)
Fnd.Value = LookUp
End Select
End If
With Fnd
If Abs(Action) <> 2 Then
With .Offset(0, 1)
If .Comment Is Nothing Then
.AddComment
Else
Txt = Chr(10)
End If
Txt = "Previous Qty = " & .Value & Chr(10) & _
"Revised " & Format(Date, "mm-dd-yyyy") & Chr(10) & _
"by " & Environ("UserName") & Txt
.Comment.Text Txt, 1, False
.Value = Val(.Value) + Action
With .Offset(0, 2)
.NumberFormat = "m/d/yyyy h:mm AM/PM"
.Value = Now
End With
End With
ElseIf Action = 2 Then
.EntireRow.Select
End If
End With
If Action <> 2 Then Target.Select
End Sub

Excel vba error while capturing old value

I am getting an error when I am trying to capture an old value from a cell:
run-time error '13' Type mismatch.
This is the code I am using:
Dim oldValue As Variant
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
oldValue = Target(1, 1).Value
MsgBox oldValue
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'check if one of the target columns is changed
If Target.Cells.Column = 6 Or Target.Cells.Column = 9 Or Target.Cells.Column = 10 Or Target.Cells.Column = 11 Then
'Set variables
Dim LogActivity As String
Dim cRow As Integer
Dim pRowCount As Integer
Dim wsPBS As Worksheet
Dim wsHistoric As Worksheet
Set wsPBS = Sheets("PBS")
Set wsHistoric = Sheets("Historic")
cRow = Target.Cells.Row
pRowCount = wsHistoric.Range("A" & Rows.Count).End(xlUp).Row + 1
'Check for blanks on PBS sheet and exit if entry is not complete
Dim BlankCount As Integer
BlankCount = 0
If wsPBS.Range("D" & cRow).Value = "" Then BlankCount = BlankCount + 1
If wsPBS.Range("E" & cRow).Value = "" Then BlankCount = BlankCount + 1
If wsPBS.Range("F" & cRow).Value = "" Then BlankCount = BlankCount + 1
If wsPBS.Range("H" & cRow).Value = "" Then BlankCount = BlankCount + 1
If wsPBS.Range("I" & cRow).Value = "" Then BlankCount = BlankCount + 1
If wsPBS.Range("J" & cRow).Value = "" Then BlankCount = BlankCount + 1
If BlankCount >= 1 Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
If Target.Cells.Column = 6 Then LogActivity = "Owner change"
If Target.Cells.Column = 9 Then LogActivity = "Status change"
If Target.Cells.Column = 10 Then LogActivity = "Priority change"
If Target.Cells.Column = 11 Then LogActivity = "Completion rate"
Range("C" & cRow & ":O" & cRow).Select
Selection.Copy
wsHistoric.Select
wsHistoric.Range("F" & pRowCount).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
wsHistoric.Range("A" & pRowCount).Value = Date
wsHistoric.Range("B" & pRowCount).Value = Time
wsHistoric.Range("C" & pRowCount).Value = Application.UserName
wsHistoric.Range("D" & pRowCount).Value = LogActivity
wsHistoric.Range("E" & pRowCount).Value = oldValue
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End If
End Sub
The value is supposed to be stored in a global dim called 'oldValue' so I can use it later on in my code.
The cell I am clicking does contain a string.
Any suggestions?
The main issue:
You're Selecting within the Worksheet_Change event.
Range("C" & cRow & ":O" & cRow).Select
Selection.Copy
That fires the Selection_Change event again, overwriting oldValue.
No need to Select here. See How to avoid using Select in Excel VBA.
Range("C" & cRow & ":O" & cRow).Copy
The secondary (yet still very important issue):
In your original version of the selection change:
Dim oldValue As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
oldValue = Target.Value
End Sub
This will throw a type mismatch error if Target doesn't contain a String or something that can be coerced to a String.
In your instance, that was because Target actually was multiple cells: Range("C" & cRow & ":O" & cRow).
But your code would also throw an error if you selected a cell with an error value (#N/A, #DIV/0, etc.).
The fix:
First of all, avoid using Select, as already noted.
If for some (rare) reason you absolutely need to Select, then toggle events off and on:
Private Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
... do your stuff
Application.EnableEvents = True
End Sub
Lastly, within the selection change, instead of assuming that you'll only select a string, or only select one cell, add some validation.
Dim oldValue As Variant
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge <> 1 Then Exit Sub ' ignore a multi-cell selection
If IsError(Target.Value) Then Exit Sub ' ignore selection of errors
oldValue = Target.Value
End Sub
Try identifying a single cell withing Target:
oldValue = Target(1,1).Value

WorksheetChange Event to Concatenate Row and First Letter of First Name + First Letter of Last Name

I think the code should be something like this, but I'm getting an error on this line where I am trying to handle the first and last names. Basically, I want to create a code in Column A, which is the first letter of the person's first name and first letter of the person's last name, concatenated with the row number. The row will be the active row (always Column A) and the first and last names will be stored in Column B.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 1 Then Exit Sub
On Error GoTo ErrHandler
Application.EnableEvents = False
If Target.Column = 1 Then
Target.Offset(0, 0).FormulaR1C1 = "=ROW()"
TV1 = Target.Offset(0, 0).FormulaR1C1
Target.Offset(0, 0).FormulaR1C1 = "=UPPER(LEFT(R[" & "=ROW()" & "]C[1],1)&MID(R[" & "=ROW()" & "]C[1],FIND("" "",R[" & "=ROW()" & "]C[1],1)+1,1))"
TV2 = Target.Offset(0, 0).FormulaR1C1
Target.Offset(0, 0).Value = TV2 & "-" & TV1
End If
End Sub
I don't like to avoid dealing with more than a single cell as the Target. It isn't hard to deal with multiple cells.
After disabling events and performing your processing, you are not turning them back on again. You code will only run once without manually turning events back on.
If you are putting first and last names into column B, shouldn't the processing be subject to column B and not column A?
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Columns("B")) Is Nothing Then
On Error GoTo ErrHandler
Application.EnableEvents = False
Dim trgt As Range
For Each trgt In Intersect(Target, Target.Parent.UsedRange, Columns("B"))
trgt = StrConv(Trim(trgt.Value2), vbProperCase)
If CBool(InStr(2, trgt.Value2, Chr(32))) Then
trgt.Offset(0, -1) = _
UCase(Left(trgt.Value2, 1)) & _
UCase(Mid(trgt.Value2, InStr(1, trgt.Value2, Chr(32)) + 1, 1)) & _
Format(trgt.Row, "000")
End If
Next trgt
End If
ErrHandler:
Application.EnableEvents = True
End Sub
I've added some trim and proper-case conversion to auto-correct the values being typed into column B.
In the following image, I copied the names from G5:G8 and pasted them into B2:B5.
I would do this differently. Why write formulas when you can do it simply in VBA?
I've made some annotations to your original code also:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 1 Then Exit Sub
Application.EnableEvents = False
' No error handler in your code
'On Error GoTo ErrHandler
' don't need to check if column 1 since we already did that and exited the sub if it was not
' If Target.Column = 1 Then
'Target.Offset(0,0) = Target
'Target.Offset(0, 0).FormulaR1C1 = "=ROW()"
'TV1 = Target.Offset(0, 0).FormulaR1C1
'Target.Offset(0, 0).FormulaR1C1 = "=UPPER(LEFT(R[" & "=ROW()" & "]C[1],1)&MID(R[" & "=ROW()" & "]C[1],FIND("" "",R[" & "=ROW()" & "]C[1],1)+1,1))"
'TV2 = Target.Offset(0, 0).FormulaR1C1
'Target.Offset(0, 0).Value = TV2 & "-" & TV1
'Just do the creation in VB
With Target
.Value = .Row & Left(.Offset(0, 1), 1) & Left(Split(.Offset(0, 1))(1), 1)
End With
'If you have more than two space-separated words in the name, then something like
Dim V As Variant
With Target
V = Split(.Offset(0, 1))
.Value = .Row & Left(V(0), 1) & Left(V(UBound(V)), 1)
End With
'Don't forget to reenable events
Application.EnableEvents = True
End Sub
Also, since the names are in Column B, why are you testing for a change in Column A? There could be reasons, but if there are not, it might be smoother to check for changes in column B.
I figured it out!!
If Target.Column = 1 Then
Target.Offset(0, 0).FormulaR1C1 = "=ROW()"
TV1 = Target.Value
Target.Offset(0, 0).FormulaR1C1 = "=UPPER(LEFT(RC[1],1)&MID(RC[1],FIND("" "",RC[1],1)+1,1))"
TV2 = Target.Value
Target.Value = TV2 & "-" & TV1
End If

Resources