Formatting Multiple cells with letters and number in VBA - excel

I have been trying to figure this problem out for some time to no avail.
I have a file that tracks different types of invoices. The invoices have both numbers and letters ex. ABC_1234_12345678. I want excel to format the invoice codes by adding the under scores after the user inputs the invoice code(without the underscores). I currently have a code that can do it for single cell but I was wondering how I could change it format a select number of cells ex. A1-A8. I will add my code in the comments.
Thank you for the help, I will be very thankful. :)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngWatch As Range
Dim strOld As String
Dim strNew As String
'What cell is the invoice number in?
Set rngWatch = Range("A1")
'Did user change it?
If Intersect(rngWatch, Target) Is Nothing Then Exit Sub
strOld = rngWatch.Value
'Are there already hypens?
If Len(strOld) = Len(Replace(strOld, "_", "")) Then strNew = Left(strOld, 3) & "_" & Mid(strOld, 4, 3) & "_" & Mid(strOld, 8)
'Turn this off for the momenet
Application.EnableEvents = False
rngWatch.Value = strNew
Application.EnableEvents = True
End If
End Sub

Expand your rngWatch:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngWatch As Range, r As Range
Dim strOld As String
Dim strNew As String
'What cell is the invoice number in?
Set rngWatch = Range("A:A")
'Did user change it?
If Intersect(rngWatch, Target) Is Nothing Then Exit Sub
For Each r In Intersect(Target, rngWatch)
strOld = r.Value
'Are there already hypens?
If Len(strOld) = Len(Replace(strOld, "_", "")) Then
strNew = Left(strOld, 3) & "_" & Mid(strOld, 4, 3) & "_" & Mid(strOld, 8)
'Turn this off for the momenet
Application.EnableEvents = False
r.Value = strNew
Application.EnableEvents = True
End If
Next r
End Sub
Note:
We use a loop in case the user changes several cells in column A simultaneously via Copy/Paste.

This can depend upon how you want the code to run. You could for example create a macro that processes all of the cells within a specific range once it has been run, which to me is a sensible way to do it. You could get the macro to process only the selected cells, which is another option. There are many ways to do this.
I have taken your example code and adjusted it so that any adjustments to cells within the _MyNamedRange named range are processed. Just incase you enter the same code into more than 1 cell, it scans through the intersect using a for loop, but you may want to get rid of this depending on how you see your worksheet functioning. You will need to create a named range _MyNamedRange, where the macro will function.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngWatch As Range
Dim strOld As String
Dim strNew As String
Dim rngCell As Range, rngInter As Range
'What cell is the invoice number in?
Set rngWatch = Range("_MyNamedRange")
'Get intersect of the change
Set rngInter = Intersect(rngWatch, Target)
'Exit of the change does not intersect with the named range
If rngInter Is Nothing Then Exit Sub
'Scan through the intersect cells and adjust the cells
Application.EnableEvents = False
For Each rngCell In rngInter
strOld = rngCell.Value
'Are there already hypens?
strNew = ""
If Len(strOld) = Len(Replace(strOld, "_", "")) Then strNew = Left(strOld, 3) & "_" & Mid(strOld, 4, 3) & "_" & Mid(strOld, 8)
'Update the cell
rngCell.Value = strNew
Next rngCell
Application.EnableEvents = True
End Sub

Related

Convert formula to value once formula has calculated

I'm using the following VBA module to create a Timestamp UDF, which stamps the date once the referenced cell reads "Done":
Function Timestamp(Reference As Range)
If Reference.Value = "Done" Then
Timestamp = Format(Date, "ddd dd mmm")
Else
Timestamp = ""
End If
End Function
The date stays the same even after refreshing / closing and opening the workbook as long as the referenced cell still reads "Done"; however if someone accidentally changes the referenced cell then the date is reset.
I need a VBA code to convert the formula to value once it has calculated, so the date will always stay the same. The solution needs to be automatic rather than manual and I can't enable iterative formulas on this workbook because it's used by multiple users. Any help much appreciated!
You can use the Worksheet Change event for that:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedCells As Range
Set AffectedCells = Intersect(Target, Me.Range("A:A")) ' Range A:A is the range we observe for 'done'
If AffectedCells Is Nothing Then Exit Sub
Dim Cell As Range
For Each Cell In AffectedCells
If Cell.Value = "done" Then
Dim UpdateTimestamp As Boolean
UpdateTimestamp = True
If Range("B" & Cell.Row).Value <> vbNullString Then
UpdateTimestamp = MsgBox("Timestamp exists do you want to update it?", vbQuestion + vbYesNo) = vbYes
End If
If UpdateTimestamp Then
Me.Range("B" & Cell.Row).Value = Format$(Date, "ddd dd mmm")
End If
End If
Next Cell
End Sub
// Edit according comment:
If you want to check multiple ranges for different things you need to slightly change your code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
Dim ObservedRangeA As Range
Set ObservedRangeA = Intersect(Target, Me.Range("A:A")) ' Range A:A is the range we observe for 'done'
If Not ObservedRangeA Is Nothing Then
For Each Cell In ObservedRangeA
If Cell.Value = "done" And Range("B" & Cell.Row).Value = vbNullString Then
Me.Range("B" & Cell.Row).Value = Format$(Date, "ddd dd mmm")
End If
Next Cell
End If
Dim ObservedRangeB As Range
Set ObservedRangeB = Intersect(Target, Me.Range("C:C")) ' Range C:C is the range we observe for ""
If Not ObservedRangeB Is Nothing Then
For Each Cell In ObservedRangeB
If Cell.Value = "" And Range("B" & Cell.Row).Value = vbNullString Then
Me.Range("B" & Cell.Row).Value = Format$(Date, "ddd dd mmm")
End If
Next Cell
End If
End Sub

How do I execute instructions only if the cell value change?

I have a ByVal code to clear the contents of a specific range inside a table, it works. But I need to add a condition for the instructions execute if the RANGE VALUE (content) change, not if I only place the cursor on it.
Also, someone knows how to reference a table column in VBA? Now I'm using an estimated range "C1:C999" but I'll like to use his name "A_[OPERATION]".
This is the code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Application.Intersect(Range("C1:C999"), Range(Target.Address)) Is Nothing Then
Range(Selection, Selection.End(xlToRight)).ClearContents
End If
End Sub
You could use the change event instead.
Here's a link to the documentation:
https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.change
Alternatively, you could save the value of your target cell in a variable and check if the value changed before executing your clear contents.
For your second question, you should probably ask about it in a separate post.
A Worksheet Change
Adjust the table name (tName) and the header (column) name (hName).
I have adjusted it to clear contents in the cells after the column.
If you really need to clear the contents of the column too, then replace cel.Offset(, 1) with cel.
In a table, the current setup will automatically clear the contents of all the cells to the right of the specified column, if a value in the column is manually or via VBA changed. This will not work if the column contains formulas. Non-contiguous deletions are also supported.
The code
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Const ProcName As String = "Worksheet_Change"
On Error GoTo clearError
Const tName As String = "A_"
Const hName As String = "OPERATION"
Dim rng As Range
Set rng = Range(tName & "[" & hName & "]")
Set rng = Intersect(rng, Target)
If rng Is Nothing Then GoTo ProcExit
Application.EnableEvents = False
With ListObjects(tName).HeaderRowRange
Dim LastColumn As Long
LastColumn = .Columns(.Columns.Count).Column
End With
Dim cel As Range
For Each rng In rng.Areas
For Each cel In rng.Cells
With cel.Offset(, 1)
.Resize(, LastColumn - .Column + 1).ClearContents
End With
Next cel
Next rng
CleanExit:
Application.EnableEvents = True
GoTo ProcExit
clearError:
Debug.Print "'" & ProcName & "': " & vbLf _
& " " & "Run-time error '" & Err.Number & "':" & vbLf _
& " " & Err.Description
On Error GoTo 0
GoTo CleanExit
ProcExit:
End Sub
A change in cell value is captured by the Worksheet_Change event-handle.
However, this handle will trigger even if it is a false change. For example, if the cell value before the change is "A", and user just entered "A" again in the cell, the Change event procedure will be triggered anyhow.
To avoid this, we can use Worksheet_Change and Worksheet_SelectionChange together.
Using Worksheet_SelectionChange, we record the old value somewhere, say a Name. Then using Worksheet_Change, we can compare what the user has entered against the Name to see if a true change is made.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Nm As Name: Set Nm = ActiveWorkbook.Names("OldValue")
If Target.Count = 1 Then
'This only record one old cell value.
'To record multiple cells old value, use a hidden Worksheet to do so instead of a Name.
Nm.Comment = Target.Value2
else
Nm.Comment = Target.Value2(1, 1)
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Nm As Name: Set Nm = ActiveWorkbook.Names("OldValue")
If Target.Value2 <> Nm.Comment Then
Debug.Print "True change"
Else
Debug.Print "False change"
End If
End Sub
You can access a table's methods and properties through the listobject object. Below is an example how to do so.
Sub Example()
Dim lo As ListObject
Dim lc As ListColumn
Set lo = Range("Table1").ListObject
Set lc = lo.ListColumns("Column2")
End Sub
That said, for your case, it would be Range("A_").ListObject.ListColumns("OPERATION").DataBodyRange.

Go to matching cell in a row range based on drop down list

I have a list of names in a row, A2 to AAS2, I also have a drop-down list containing all of those names. I would like some VBA code that when the list is changed excel jumps to the cell matching the item in the list. Could someone please help me with this? Thank you.
The names are just text, no named ranges.
Here is what I have tried so far:
Private Sub FindTicker()
Dim MyVariable As String
MyVariable = Range("L1").Value
Application.Goto Reference:=Range(MyVariable)
End Sub
And Also
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> [L1].Address Then
Exit Sub
Else
JumpToCell
End If
End Sub
Sub JumpToCell()
Dim xRg, yRg As Range
Dim strAddress As String
strAddress = ""
Set yRg = Range("A2:AAS2")
For Each xRg In yRg
'MsgBox Cell.Value
If xRg.Value = ActiveCell.Value Then
strAddress = xRg.Address
End If
Next
If strAddress = "" Then
MsgBox "The Day You Selected in Cell D4 Was Not Found On " & ActiveSheet.Name, _
vbInformation, "Ticker Finder"
Exit Sub
Else
Range(strAddress).Offset(0, 1).Select
End If
End Sub
When I tried using both of these when I changed the drop-down list nothing happened. No errors or anything.
Lots of ways to do this and with some tweaks your code above could work but its a bit inefficient and more complicated than it needs to be. The simplest way would be to use the Find method of the Range class to locate the cell:
Lets say your drop-down list of names is in cell A1 on sheet MySheet and the long list is in column C. Use the Find method to set a range variable to equal the first cell containing the item in cell A1.
Dim rng As Range
Dim ws As Worksheet
Set ws = Sheets("MySheet")
Set rng = ws.Range("C:C").Cells.Find(ws.Range("A1"), lookat:=xlWhole)
If Not rng Is Nothing Then ' the item was found
rng.Select
Else
MsgBox "This item is not in the list", vbInformation
End If

Copy paste rows meeting conditions stated from a multiple selection in a drop-down list (with no repetition)

I have:
1) one worksheet named "Data" for my products data base;
2) one worksheet named "Quotation ENG" for products quotations based on selected products from the data base;
3) one worksheet named "Manager" with dropdown lists to make the criteria selections.
Then, I have two pieces of code running fine independently.
One named Sub Quote is for copy-pasting part of the rows from my database to the quotation sheet when a criterion is met,
And one named Sub Worksheet_Change (credits: TrumpExcel) is for enabling multiple selections in a dropdown list.
I'm quite clueless on how to change the code of my Sub Quote module to make the copy-paste operation possible if the dropdown lists enable multiple criteria. ANy guidance is welcome :)
Sub Quote()
Dim Source As Worksheet
Dim Target As Worksheet
Dim Company As String
Dim InfoA As String
Dim Finalrow As Integer
Dim I As Integer
Set Source = Worksheets("Data")
Set Target = Worksheets("Quotation ENG")
Company = Worksheets("Manager").Range("E5").Value 'Where one dropdown list is located.
InfoA = Worksheets("Manager").Range("E7").Value 'Where one dropdown list is located.
Source.Select
Finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For I = 2 To Finalrow
If Cells(I, 1) = Company And Cells(I, 2) = InfoA Then
Source.Range(Cells(I, 16), Cells(I, 23)).Copy Target.Range("A200").End(xlUp).Offset(1, 0).Resize(1, 8)
End If
Next I
Target.Select
Range("A1").Select
End Sub
=============================================================
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$E$5" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
I refactored some parts of your code and turned the company variable into an array so it can store several values. Please read the comments inside the code.
As a suggestion, try to use Excel structured tables to store your data. It's gonna be easier to work with them in the future.
Replace your current Quote Sub for this:
Sub Quote()
Dim Source As Worksheet
Dim Target As Worksheet
Dim Company() As String ' Converted the company variable to an array
Dim InfoA As String
Dim Finalrow As Integer
Dim counter As Integer
Dim I As Integer
Set Source = Worksheets("Data")
Set Target = Worksheets("Quotation ENG")
Company = Split(Worksheets("Manager").Range("E5").Value, ",") 'Where one dropdown list is located.
InfoA = Worksheets("Manager").Range("E7").Value 'Where one dropdown list is located.
' Added the source sheet and removed the select as it slows down your code
Finalrow = Source.Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each company contained in the array
For counter = 0 To UBound(Company)
' Loop through each data row
For I = 2 To Finalrow
' Added Company(counter) so you can access each array element and wrapped it with trim to delete extra spaces
If Source.Cells(I, 1) = Trim(Company(counter)) And Source.Cells(I, 2) = InfoA Then
Source.Range(Source.Cells(I, 16), Source.Cells(I, 23)).Copy Target.Range("A200").End(xlUp).Offset(1, 0).Resize(1, 8)
End If
Next I
Next counter
' Activate worksheet
Target.Activate
' Refer to the object full path
Target.Range("A1").Select
End Sub
Let me know if it works.

Create log history for a cell value change in a column

I create a log history worksheet and save the change detail of other worksheets.
Dim oldValue As Variant
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim sSheetName As String
sSheetName = "Data"
If ActiveSheet.Name <> "LogDetails" Then
Application.EnableEvents = False
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = ActiveSheet.Name & "-" & Target.Address(0, 0)
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 1).Value = oldValue
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Target.Formula
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 3).Value = Environ("username")
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 4).Value = Now
Sheets("LogDetails").Columns("A:D").AutoFit
Application.EnableEvents = True
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
oldValue = Target.Formula
End Sub
That works for a single cell, e.g.:
If A1 stores "ABC" and changes to "123", Log Detail will save the Cell address, old value, new value, username and date/time.
The big problem is when I select a whole column, such as all of column(B). It will get the error
"type not match".
I know the problem is
oldValue = Target.Value
How can I save the change of a column?
I'm not entirely sure if that solves your problem, but try the following modification to your Workbook_SheetSelectionChange procedure:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Count > 1 Then
Target(1).Select
Exit Sub
End If
oldValue = Target.Value
oldAddress = Target.Address
End Sub
Every time the user selects more than one cell the event procedure will change that selection (which raises another change event, with a single-cell target this time) and exit without doing anything else. The criterion for when this selection change should happen can of course be refined to allow for a more specific behavior.
This should make it much harder for the average user to intentionally or accidentally modify more than one cell at a time.
To address questions from your comments:
the undo function of excel cannot be used
This is true. Excel doesn't know how to reverse the actions your code has taken. You need to build this functionality yourself. See this question + accepted answer.
the formula change show in log sheet cannot not show properly, it will show 0 or #Value!
Yes, that's by design. With the line
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = Target.Formula
you tell Excel to set that cell's value to a formula. Which Excel then automatically tries to evaluate. (Leading to the errors you're experiencing)
Try the following:
' Prepend the formula with an apostrophe
Sheets("LogDetails").Range("A" & Rows.Count).End(xlUp).Offset(0, 2).Value = "'" & Target.Formula
This will force Excel to treat the cell value as text and thus it will simply show the formula without evaluating it.
copy and paste a range only show the first cell changes, is it cannot be fix?
This is due to oldValues being an array while you only ever access its first value. See my implementation:
Option Explicit
Dim oldValues As Variant
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Const LogSheet As String = "LogDetails"
If Sh.Name = LogSheet Then Exit Sub
Application.EnableEvents = False
With Worksheets(LogSheet)
Dim idxRows As Long
For idxRows = 1 To Target.Rows.Count
Dim idxCols As Long
For idxCols = 1 To Target.Columns.Count
Dim ChangedCell As Range
Set ChangedCell = Target.Rows(idxRows).Columns(idxCols)
Dim LogRow As Long
LogRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
Dim LogRange As Range
Set LogRange = .Range(.Cells(LogRow, 1), .Cells(LogRow, 5))
LogRange(1).Value = Sh.Name & "!" & ChangedCell.Address(False, False)
LogRange(2).Value = "'" & oldValues(idxRows, idxCols) ' error here when pasting a range of different size than has been selected before pasting
LogRange(3).Value = ChangedCell.Formula
LogRange(4).Value = Environ("username")
LogRange(5).Value = Now
Next idxCols
Next idxRows
.Columns("A:E").AutoFit
End With
Application.EnableEvents = True
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
oldValues = Target.Formula
End Sub
This has the weakness that when a user copies multiple cells and then selects a single cell and pastes, it will error out, due to mismatching indices. (It works when you copy, e.g. 3 cells in a row, then select 3 other cells in a row and paste.) Not sure how to avoid that. We'd need to capture the size of the pasted range to update oldValues accordingly. As Excel doesn't expose a Workbook_SheetBeforePaste event, that seems rather tricky.

Resources