Help with splitting Array using VBA?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hello guys, trying to figure out how to split my data.

Here is the code I have which loads my array.
VBA Code:
Sub ArraysTemp()
Dim a As Variant
Dim wb1 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim LR As Long



Set wb1 = Workbooks("TempD.xlsx")


    wb1.Activate
    ActiveSheet.Name = "Source Data"
    Sheets.Add.Name = "Array Data"

Set ws1 = wb1.Worksheets("Source Data")
Set ws2 = wb1.Worksheets("Array Data")

LR = ws1.Range("B" & Rows.count).End(xlUp).Row

a = ws1.Range("A1:F" & LR).Value

ws2.Range("A1").Resize(UBound(a), UBound(a, 2)).Value = a

End Sub

I however would like to learn how to split the first column prior to dumping the array.

This is the original data:

TempD.xlsx
ABCDEF
1Site IDCostAdd OnAdjTaxShipping
212345-XX001$100.00$0.00$0.00$5.00$0.00
312345-XX002$100.00$0.00$0.00$5.00$0.00
412345-XX003$100.00$0.00$0.00$5.00$0.00
512345-XX004$100.00$0.00$0.00$5.00$0.00
612345-XX005$100.00$0.00$0.00$5.00$0.00
712345-XX006$100.00$0.00$0.00$5.00$0.00
812345-XX007$100.00$0.00$0.00$5.00$0.00
912345-XX008$100.00$0.00$0.00$5.00$0.00
1012345-XX009$100.00$0.00$0.00$5.00$0.00
1112345-XX010$100.00$0.00$0.00$5.00$0.00
1212345-XX011$100.00$0.00$0.00$5.00$0.00
1312345-XX012$100.00$0.00$0.00$5.00$0.00
Source Data


And this is the desired result :

TempD.xlsx
ABCDEF
1IDCostAdd OnAdjTaxShipping
2XX001$100.00$0.00$0.00$5.00$0.00
3XX002$100.00$0.00$0.00$5.00$0.00
4XX003$100.00$0.00$0.00$5.00$0.00
5XX004$100.00$0.00$0.00$5.00$0.00
6XX005$100.00$0.00$0.00$5.00$0.00
7XX006$100.00$0.00$0.00$5.00$0.00
8XX007$100.00$0.00$0.00$5.00$0.00
9XX008$100.00$0.00$0.00$5.00$0.00
10XX009$100.00$0.00$0.00$5.00$0.00
11XX010$100.00$0.00$0.00$5.00$0.00
12XX011$100.00$0.00$0.00$5.00$0.00
13XX012$100.00$0.00$0.00$5.00$0.00
Sheet3


I essentially want to keep only the TextAfter "-" using VBA.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
This will do it without loading it into an array.

Book2
ABCDEF
1Site IDCostAdd OnAdjTaxShipping
2XX0011000050
3XX0021000050
4XX0031000050
5XX0041000050
6XX0051000050
7XX0061000050
8XX0071000050
9XX0081000050
10XX0091000050
11XX0101000050
12XX0111000050
13XX0121000050
Sheet1


VBA Code:
Sub XSPLIT()
Dim r As Range:         Set r = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

r.Value = Evaluate(Replace("=RIGHT(@,LEN(@)-FIND(""-"",@))", "@", r.Address))

End Sub
 
Upvote 0
This will do it without loading it into an array.

Book2
ABCDEF
1Site IDCostAdd OnAdjTaxShipping
2XX0011000050
3XX0021000050
4XX0031000050
5XX0041000050
6XX0051000050
7XX0061000050
8XX0071000050
9XX0081000050
10XX0091000050
11XX0101000050
12XX0111000050
13XX0121000050
Sheet1


VBA Code:
Sub XSPLIT()
Dim r As Range:         Set r = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)

r.Value = Evaluate(Replace("=RIGHT(@,LEN(@)-FIND(""-"",@))", "@", r.Address))

End Sub
Thank you for this option. Ideally I'd like to be able to do it within the array. I intend on trying to do some additional calculations hence why I'm loading the data into the array and then spitting it back out.
 
Upvote 0
Or loading it into an array and pasting it to another sheet. More like what you were asking for.

VBA Code:
Sub WITHARRAY()
Dim wb As Workbook:         Set wb = ActiveWorkbook
Dim ws1 As Worksheet:       Set ws1 = wb.Sheets("Sheet1")
Dim ws2 As Worksheet:       Set ws2 = wb.Sheets("Sheet2")
Dim AR() As Variant

With ws1
    AR = .Range("A1:F" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
End With

For i = 2 To UBound(AR)
    AR(i, 1) = Split(AR(i, 1), "-")(1)
Next i

ws2.Range("A1").Resize(UBound(AR), UBound(AR, 2)).Value = AR

End Sub
 
Upvote 0
Or loading it into an array and pasting it to another sheet. More like what you were asking for.

VBA Code:
Sub WITHARRAY()
Dim wb As Workbook:         Set wb = ActiveWorkbook
Dim ws1 As Worksheet:       Set ws1 = wb.Sheets("Sheet1")
Dim ws2 As Worksheet:       Set ws2 = wb.Sheets("Sheet2")
Dim AR() As Variant

With ws1
    AR = .Range("A1:F" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
End With

For i = 2 To UBound(AR)
    AR(i, 1) = Split(AR(i, 1), "-")(1)
Next i

ws2.Range("A1").Resize(UBound(AR), UBound(AR, 2)).Value = AR

End Sub
This one works. Thank you so much for this!
 
Upvote 0
Mods, please remove if this requires a new thread.

Just had a follow on question to this, Is there a way of adding an "If" statement to this?

I get a runtime error if one of the cells on Range("A") does not contain "-" Trying to bypass this someway somehow.

Book4
ABCDEF
1Site IDCostAdd OnAdjTaxShipping
212345-XX0011000050
312345-XX0021000050
412345-XX0031000050
512345-XX0041000050
612345-XX0051000050
712345-XX0061000050
812345-XX0071000050
912345-XX0081000050
1012345-XX0091000050
1112345-XX0101000050
1212345-XX0111000050
13123451000050
Sheet1


I tried playing around with something like this with no success of course

VBA Code:
If InStr(1, (Range("A2" & i).Value), "-") > 0 Then
 
Upvote 0
This seems to do the trick.

VBA Code:
Sub WITHARRAY()
Dim wb As Workbook:         Set wb = ActiveWorkbook
Dim ws1 As Worksheet:       Set ws1 = wb.Sheets("Sheet1")
Dim ws2 As Worksheet:       Set ws2 = wb.Sheets("Sheet2")
Dim AR() As Variant
Dim tmp As String

With ws1
    AR = .Range("A1:F" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
End With

For i = 2 To UBound(AR)
    tmp = AR(i, 1)
    If InStr(tmp, "-") Then AR(i, 1) = Split(tmp, "-")(1)
Next i

ws2.Range("A1").Resize(UBound(AR), UBound(AR, 2)).Value = AR

End Sub
 
Upvote 0
Solution
This seems to do the trick.

VBA Code:
Sub WITHARRAY()
Dim wb As Workbook:         Set wb = ActiveWorkbook
Dim ws1 As Worksheet:       Set ws1 = wb.Sheets("Sheet1")
Dim ws2 As Worksheet:       Set ws2 = wb.Sheets("Sheet2")
Dim AR() As Variant
Dim tmp As String

With ws1
    AR = .Range("A1:F" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
End With

For i = 2 To UBound(AR)
    tmp = AR(i, 1)
    If InStr(tmp, "-") Then AR(i, 1) = Split(tmp, "-")(1)
Next i

ws2.Range("A1").Resize(UBound(AR), UBound(AR, 2)).Value = AR

End Sub
Sure does. Truly appreciate it.
 
Upvote 0

Forum statistics

Threads
1,215,360
Messages
6,124,489
Members
449,166
Latest member
hokjock

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