Extract value into columns using VBA

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys,
I want to create a code to split the content of a cell into 3 colurmns but it does not work out. Please help to make it through.

onlymac.jpg

What the code should do is to convert the those numbers in the left to MAC address and put it into column B, and those numbers after "X=" and "Y=" to column C and D respectively.

Here is my unfinished code
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myText As String
Dim macText As String
Dim c As Range
Dim lrow As Long
Dim EntryMac As Long
Dim EntryX As Long
Dim EntryY As Long


lrow = Range("A1").End(xlDown).Row

If Target.CountLarge > 1 Then Exit Sub
On Error GoTo ErrHandler:


    If Len(Target) < 12 Then Exit Sub  'check for correct length
    If Target.Column <> 1 And Target.Row = 1 Then Exit Sub 'only Column A and Row 2 downwards
    If Target.Value = "" Then Exit Sub
    Application.EnableEvents = False
    
    If Not Intersect(Target, Range("A2:A" & lrow)) Is Nothing Then
    
    For Each c In Range("b2:b" & lrow)
        
         macText = Left(Target.Value, 12)
         myText = Left(macText.Value, 2) _
        & "-" & Mid(macText.Value, 3, 2) _
        & "-" & Mid(macText.Value, 5, 2) _
        & "-" & Mid(macText.Value, 7, 2) _
        & "-" & Mid(macText.Value, 9, 2) _
        & "-" & Right(macText.Value, 2)
      
    EntryMac = Left(Target, 12)
    EntryX = InStr(Target, "X=" - InStr(Target, "Y="))
    EntryY = InStr(Target, "Y=")
          c.Value = UCase(myText)
        
    
    mcell.Value = macText
    
    Next c
  
    
    Application.EnableEvents = True
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
See if this does what you need:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lrow As Long
    Dim arr As Variant
    
    lrow = Range("A1").End(xlDown).Row
   
    If Target.CountLarge > 1 Then Exit Sub

    If Len(Target) < 12 Then Exit Sub  'check for correct length
   
    ' XXX Changed next line, since either not column A OR row 1 should cause it to exit
    If Target.Column <> 1 Or Target.Row = 1 Then Exit Sub 'only Column A and Row 2 downwards
   
    If Target.Value = "" Then Exit Sub
    Application.EnableEvents = False
   
    If Not Intersect(Target, Range("A2:A" & lrow)) Is Nothing Then

        arr = Split(Application.Trim(Target), " ")
        Target.Offset(0, 1) = Format(arr(0), "00-00-00-00-00-00")
        Target.Offset(0, 2) = Mid(arr(1), 3, Len(arr(1)) - 2)
        Target.Offset(0, 3) = Mid(arr(2), 3, Len(arr(1)) - 2)
 
    End If
   
    Application.EnableEvents = True
End Sub
 
Upvote 0
Do you really need VBA?
This can be done pretty easily with formulas, i.e.

B2 formula:
Excel Formula:
=LEFT(A2,FIND(" ",A2)-1)

C2 formula:
Excel Formula:
=TRIM(MID(A2,FIND("X=",A2)+2,4))

D2 formula:
Excel Formula:
=TRIM(MID(A2,FIND("Y=",A2)+2,4))
 
Upvote 0
See if this does what you need:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lrow As Long
    Dim arr As Variant
  
    lrow = Range("A1").End(xlDown).Row
 
    If Target.CountLarge > 1 Then Exit Sub

    If Len(Target) < 12 Then Exit Sub  'check for correct length
 
    ' XXX Changed next line, since either not column A OR row 1 should cause it to exit
    If Target.Column <> 1 Or Target.Row = 1 Then Exit Sub 'only Column A and Row 2 downwards
 
    If Target.Value = "" Then Exit Sub
    Application.EnableEvents = False
 
    If Not Intersect(Target, Range("A2:A" & lrow)) Is Nothing Then

        arr = Split(Application.Trim(Target), " ")
        Target.Offset(0, 1) = Format(arr(0), "00-00-00-00-00-00")
        Target.Offset(0, 2) = Mid(arr(1), 3, Len(arr(1)) - 2)
        Target.Offset(0, 3) = Mid(arr(2), 3, Len(arr(1)) - 2)
 
    End If
 
    Application.EnableEvents = True
End Sub
Hi, the arr(0) output does not show with hypen in between the figures
View attachment 60323
also I added UCASE to the code to make output in capital letters : Target.Offset(0, 1) = UCase(Format(arr(0), "00-00-00-00-00-00"))
 
Last edited:
Upvote 0
Do you really need VBA?
This can be done pretty easily with formulas, i.e.

B2 formula:
Excel Formula:
=LEFT(A2,FIND(" ",A2)-1)

C2 formula:
Excel Formula:
=TRIM(MID(A2,FIND("X=",A2)+2,4))

D2 formula:
Excel Formula:
=TRIM(MID(A2,FIND("Y=",A2)+2,4))
Hi Joe4,
I prefer VBA method better. Thanks
 
Upvote 0
Hi Joe4,
I prefer VBA method better. Thanks
You could use VBA to drop those formulas into columns B, C, and D of whatever row you are adding a value to column A in!
;)
 
Upvote 0
That code to put the formulas in the cells upon the updating of column A would look like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.CountLarge > 1 Then Exit Sub
    
'   If update is to column A after row 2
    If Target.Column = 1 And Target.Row > 1 Then
        Application.EnableEvents = False
        Range("B" & Target.Row).FormulaR1C1 = "=LEFT(RC[-1],FIND("" "",RC[-1])-1)"
        Range("C" & Target.Row).FormulaR1C1 = "=TRIM(MID(RC[-2],FIND(""X="",RC[-2])+2,4))"
        Range("D" & Target.Row).FormulaR1C1 = "=TRIM(MID(RC[-3],FIND(""Y="",RC[-3])+2,4))"
        Application.EnableEvents = True
    End If
    
End Sub
 
Upvote 0
Here is another Change event procedure that will also work...
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Column = 1 And Target.Row > 1 Then Target.TextToColumns Target.Offset(, 1), xlDelimited, , 1, 0, 0, 0, 1, 1, "=", Array(Array(1, 1), Array(2, 9), Array(3, 1), Array(4, 9), Array(5, 1))
End Sub
 
Upvote 0
Sadly format text only works on numeric values, here is a modified version.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim macText As String
    Dim lrow As Long
    Dim arr As Variant
    Dim i As Long

    lrow = Range("A1").End(xlDown).Row

    If Target.CountLarge > 1 Then Exit Sub

'   If update is to column A after row 2
    If Target.Column = 1 And Target.Row > 1 Then

        Application.EnableEvents = False

        arr = Split(Application.Trim(Target), " ")

        macText = ""
        For i = 1 To 11 Step 2
            macText = macText & "-" & Mid(arr(0), i, 2)
        Next i

        Target.Offset(0, 1) = Right(UCase(macText), Len(macText) - 1)
        Target.Offset(0, 2) = Mid(arr(1), 3, Len(arr(1)) - 2)
        Target.Offset(0, 3) = Mid(arr(2), 3, Len(arr(1)) - 2)

        Application.EnableEvents = True

    End If

End Sub
 
Upvote 0
Oh, MAC addresses have dashes in them! I wasn't aware of that. Okay, here is my previously posted event code procedure modified to do that...
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Column = 1 And Target.Row > 1 Then
    Target.TextToColumns Target.Offset(, 1), xlDelimited, , 1, 0, 0, 0, 1, 1, "=", Array(Array(1, 1), Array(2, 9), Array(3, 1), Array(4, 9), Array(5, 1))
    Target.Offset(, 1) = Format(Target.Offset(, 1), "@@-@@-@@-@@-@@-@@")
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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