Building custom solutions that extend, automate, and integrate Microsoft 365 apps.
Dear @Sunday WorkTV,
Welcome to Microsoft Q&A Forum!
Thank you for your request regarding the macro to convert amounts based on item type. I’ve reviewed and tried my best to research and refine the code to improve its clarity, efficiency, and maintainability. Below is the updated version, followed by a brief explanation of the changes, you can consider consulting it to see if it can help you:
Sub ConvertAmountByEventType_Simple()
Dim ws As Worksheet
Dim colAmount As String, colType As String
Dim lastRow As Long, i As Long
Dim typeText As String
Set ws = ActiveSheet
colAmount = InputBox("Enter the column letter for Amount (e.g. D):", "Amount Column")
If colAmount = "" Then Exit Sub
colType = InputBox("Enter the column letter for Item Type (CR/DR):", "Item Type Column")
If colType = "" Then Exit Sub
lastRow = ws.Cells(ws.Rows.Count, colAmount).End(xlUp).Row
For i = 1 To lastRow
With ws
If IsNumeric(.Cells(i, colAmount).Value) And .Cells(i, colAmount).Value <> "" Then
typeText = UCase(.Cells(i, colType).Value)
Select Case True
Case InStr(typeText, "CR") > 0
.Cells(i, colAmount).Value = Abs(.Cells(i, colAmount).Value)
Case InStr(typeText, "DR") > 0
.Cells(i, colAmount).Value = -Abs(.Cells(i, colAmount).Value)
End Select
End If
End With
Next i
End Sub
Here are the points in my codes:
1.I simplified cell access: removed the use of separate Range variables (amountCell, typeCell) and accessed cells directly using .Cells(i, colAmount) and .Cells(i, colType) for cleaner code.
2.I used with ws block: this reduces repetition of ws. and improves readability by grouping all worksheet-related operations.
3.I replaced If...else If with select case: this makes the logic easier to follow and more scalable if additional conditions are needed in the future.
4.I preserved original functionality: the macro still converts "CR" amounts to positive and "DR" amounts to negative, based on the item type column.
Besides that, the macro has been updated by consulting ideas in these documents from Microsoft. For reference, here are official documentation links that explain the key VBA concepts used in the code, you can take a look as well:
- Select Case statement (VBA) | Microsoft Learn
- With statement (VBA) | Microsoft Learn
- Worksheet.Cells property (Excel) | Microsoft Learn
- InputBox function (Visual Basic for Applications) | Microsoft Learn
As a Microsoft Q&A moderator, my role is to guide discussions and connect users with helpful resources. While I don’t have the right to access to your individual account or your environment in order to help you further. However, I still try my best to support you using the resources available. So, please kindly try my suggestion above.
I hope this information can help you in your case. Wish you a pleasant day!
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.