VBA How to Use Split to Split a String and Copy

beartooth91

New Member
Joined
Dec 15, 2024
Messages
38
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hello Again -

I have a column of instrument ranges in Col AR (Range is AR11 to rows.count) of one sheet and want to split each of those strings into two parts - a min and a max - and then copy the min to Column R.....and copy the max to Column S, both in a different worksheet, starting in row 3.
The instrument ranges are different, with most being a '4-20ma' entry in .Cells(x, "AR"). I want to end up with 4ma in .Cells(y, "R") and 20ma in .Cells(y, "S").

Need to use a loop to copy and need to use the Split function, splitting at the hyphen (I think).

I'm pretty good with For If Then Else Next loops, but after the above..... I have no idea and could use your help.

Thanks in advance.
 
This takes the data from Column A starting at A2 and put the output in Columns B and C in the same sheet.
If you supply pertinent data like sheet names, we'll change that for you if you have trouble changing it to fit your needs yourself.

Code:
Sub Or_Maybe_So()
Dim allArr, i As Long, j As Long
allArr = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row).Value    '<----Change required
ReDim Preserve allArr(1 To UBound(allArr), 1 To 3)
    For i = LBound(allArr) To UBound(allArr)
        allArr(i, 2) = Split(allArr(i, 1), "-")(0)
        allArr(i, 3) = Split(allArr(i, 1), "-")(1)
    Next i
    For j = 2 To UBound(allArr, 2)
        Cells(2, j).Resize(UBound(allArr)) = Application.Index(allArr, , j)    '<----Change/sheet name required
    Next j
End Sub
Following up on this: I've been messing around with this and can't get it to work. Changed one line of code (See below) and am getting the "subscript of range" on the allArr(i, 2) = Split(allArr(i, 1), "-")(0) line.

VBA Code:
Sub Or_Maybe_So()
With Workbooks("AISCAN.xlsx").Sheets(1)
Dim allArr, i As Long, j As Long
allArr = Range("C3:C" & Cells(Rows.Count, 1).End(xlUp).Row).Value    '<----Change required
ReDim Preserve allArr(1 To UBound(allArr), 1 To 3)
    For i = LBound(allArr) To UBound(allArr)
        allArr(i, 2) = Split(allArr(i, 1), "-")(0)
        allArr(i, 3) = Split(allArr(i, 1), "-")(1)
    Next i
    For j = 2 To UBound(allArr, 2)
        Cells(2, j).Resize(UBound(allArr)) = Application.Index(allArr, , j)    '<----Change/sheet name required
    Next j
End With
End Sub
 
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Did you read the last sentence in my last post? The BTW line.
Like I mentioned, I copy and paste the request but I don't want all the extra data that is totally unnecessary.
 
Upvote 0
If you use the code from Post #30 after changing the workbook names and sheet names, does it work.
You said that you only changed one line. The code in Post #30 has 19 lines and your "one line changed" code has 14 lines. Hardly a one line change.
Why did you add the "With Workbooks ….. line?
In which workbook/sheet is the data for the array?
If it is in the AISCAN workbook, your "With …. End With" is missing the required periods.
 
Upvote 0
What is the result of
Code:
MsgBox allArr(1, 1): Exit Sub
when you insert that line after the
Code:
ReDim Preserve allArr(1 To UBound(allArr), 1 To 3)
Comment out (put an apostrophe in front of it) after using it or just delete that MsgBox line.

You had the right idea using the "With...End With" statement but there is a little more to it.
 
Last edited:
Upvote 0
If you use the code from Post #30 after changing the workbook names and sheet names, does it work.
You said that you only changed one line. The code in Post #30 has 19 lines and your "one line changed" code has 14 lines. Hardly a one line change.
Why did you add the "With Workbooks ….. line?
In which workbook/sheet is the data for the array?
If it is in the AISCAN workbook, your "With …. End With" is missing the required periods.
The code that I was trying was your original post ....14, I think.....?
 
Upvote 0
What is the result of
Code:
MsgBox allArr(1, 1): Exit Sub
when you insert that line after the
Code:
ReDim Preserve allArr(1 To UBound(allArr), 1 To 3)
Comment out (put an apostrophe in front of it) after using it or just delete that MsgBox line.

You had the right idea using the "With...End With" statement but there is a little more to it.
I use the With - End With alot, but I've had a misunderstanding of it. I'd believed it works when there is more than one workbook/worksheet present and that it would execute on the non-active workbook...... and I've found out that's not true.
 
Upvote 0

Forum statistics

Threads
1,225,178
Messages
6,183,385
Members
453,157
Latest member
Excel_Newone

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