VBA code add ABC character after Dimensions Length Width Height

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hello everyone. I need a piece of VBA code to add ABC characters behind the size as shown. Thank you everyone

1684049649071.png

 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this as one possible way.
Note that this replaces the original data rather than producing the reults in another column like your sample. I wasn't sure what you were after in that regard.

VBA Code:
Sub ABC()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "(\d)( X)"
  a = Range("B3", Range("B" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    If RX.Test(a(i, 1)) Then a(i, 1) = RX.Replace(RX.Replace(a(i, 1), "$1A$2"), "$1B$2") & "C"
  Next i
  Range("B3").Resize(UBound(a)).Value = a
End Sub
 
Upvote 0
Hello everyone. I need a piece of VBA code to add ABC characters behind the size as shown. Thank you everyone

View attachment 91597
VBA Code:
Option Explicit

Sub ABC()
    Dim rng As Range
    Dim strInput As Variant
    Dim strOutput As Variant
    Dim i As Long, j As Long
   
    Set rng = Range("B3:B6")
   
    For i = 0 To rng.Cells.Count - 1
        strInput = Split(rng.Cells(i + 1, 1).Value, " X ")
       
        For j = 0 To UBound(strInput)
            strInput(j) = strInput(j) & WorksheetFunction.Unichar(65 + j)
           
            If j = 0 Then
                strOutput = strInput(j)
            Else
                strOutput = strOutput & " X " & strInput(j)
            End If
        Next j
       
        rng.Cells(i + 1, 2).Value = strOutput
   
    Next i
   
   
End Sub
 
Upvote 0
Another possibility.

VBA Code:
Sub ABC()
Dim lr As Long

lr = Range("B" & Rows.Count).End(xlUp).Row
Range("C3:C" & lr).Formula = "=IF(B3="""","""",SUBSTITUTE(SUBSTITUTE(B3,"" X"",""A X"",1),"" X"",""B X"",2)&""C"")"
Range("C3:C" & lr).Value = Range("C3:C" & lr).Value

End Sub
 
Upvote 0
Solution
Try this as one possible way.
Note that this replaces the original data rather than producing the reults in another column like your sample. I wasn't sure what you were after in that regard.

VBA Code:
Sub ABC()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "(\d)( X)"
  a = Range("B3", Range("B" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    If RX.Test(a(i, 1)) Then a(i, 1) = RX.Replace(RX.Replace(a(i, 1), "$1A$2"), "$1B$2") & "C"
  Next i
  Range("B3").Resize(UBound(a)).Value = a
End Sub
Thank You
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,861
Members
449,472
Latest member
ebc9

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top