'How to add formula to a value format?
Wishing to create an ID tag that auto indexes as data is saved from a form. The format of the ID follows the formula Left(B(cell),3). & "000"
I found code to index, but unable to shorten the B cell.
Dim sNm As String
Dim i As Integer
Dim nextemptycell As Range
Dim no As Integer
Dim AbcID As Range
Dim AbcNum As Integer
'checking if sheet already exists in workbook
Set wb = ActiveWorkbook
sNm = cbocat 'selection in cbocat will be tested and or created.
For Each Ws In wb.Worksheets
If Ws.Name = sNm Then
Worksheets(sNm).Activate
MsgBox ("Existing sheet updated!")
Set nextemptycell = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1)
Do Until WorksheetFunction.CountA(nextemptycell.EntireRow) = 0
Set nextemptycell = nextemptycell.Offset(1)
Loop
With Ws
Set AbcID = .Cells(.Rows.Count, "A").End(xlUp) 'last used
End With
With AbcID
If .Row > 1 Then AbcNum = Val(.Value)
Set AbcID = .Offset(1)
End With
With AbcID
.Value = AbcNum + 1
.NumberFormat = """.Formula= .Left(b,3)""" & "-000"
End With
nextemptycell(1, 2).Value = cbocat.Value
nextemptycell(1, 3).Value = cbosub.Value
nextemptycell(1, 4).Value = cboreq.Value
nextemptycell(1, 5).Value = cboclas.Value
Exit Sub
End If
Solution 1:[1]
please try with this change, i just replaced the code where you are injecting the ID.
Dim sNm As String
Dim i As Integer
Dim nextemptycell As Range
Dim no As Integer
Dim AbcID As Range
Dim AbcNum As Integer
'checking if sheet already exists in workbook
Set wb = ActiveWorkbook
sNm = cbocat 'selection in cbocat will be tested and or created.
For Each Ws In wb.Worksheets
If Ws.Name = sNm Then
Worksheets(sNm).Activate
MsgBox ("Existing sheet updated!")
Set nextemptycell = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1)
Do Until WorksheetFunction.CountA(nextemptycell.EntireRow) = 0
Set nextemptycell = nextemptycell.Offset(1)
Loop
With Ws
Set AbcID = .Cells(.Rows.Count, "A").End(xlUp) 'last used
End With
With AbcID
If .Row > 1 Then AbcNum = Val(.Value)
Set AbcID = .Offset(1)
End With
With AbcID
' * * * Change Here * * *
.Value = Mid(.Offset(0, 1), 1, 3) & " - " & Format(AbcNum + 1, "000")
End With
nextemptycell(1, 2).Value = cbocat.Value
nextemptycell(1, 3).Value = cbosub.Value
nextemptycell(1, 4).Value = cboreq.Value
nextemptycell(1, 5).Value = cboclas.Value
Exit Sub
End If
Regards!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Edmundo Canales |


