VBA How to Use Split to Split a String and Copy

beartooth91

New Member
Joined
Dec 15, 2024
Messages
37
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.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
use a combination of string functions (replace 4-20ma below with your looped cell references)
left("4-20ma",1) & mid("4-20ma",instr("4-20ma","ma"))
mid("4-20ma",Instr("4-20ma","-")+1)

That will only be as good as the consistency of your data that you posted.
 
Upvote 0
Assuming Column A has the instrument names and Column B has the ranges, try this:
VBA Code:
Option Explicit

Sub TextSplit()

Dim r As Range
Dim SplitText() As String
Dim MachineNames As Range
Dim ws As Worksheet

Worksheets("Sheet2").Activate

Set ws = Worksheets("Sheet1")

For Each r In ws.Range("A2", ws.Range("A2").End(xlDown))
    Range("A" & r.Row).Value = r.Value
    SplitText = Split(r.Offset(0, 1).Value, "-")
    Range("B" & r.Row).Value = SplitText(0) & Right(r.Offset(0, 1).Value, 2)
    Range("C" & r.Row).Value = SplitText(1)
Next r
   
End Sub
 
Upvote 0
Upvote 0
This is what I'm trying to do. And getting a Compile Error: Expected Array on the first copy line.

VBA Code:
Sub Split_String()

Dim splitsiglvl As String, finalRow As Long, e As Long, i As Long

e = Workbooks(AISCAN.xlsx).Sheets(1).Range("C" & Rows.Count).End(xlUp).Row

With Workbooks("NIC Master Database.xlsm").Worksheets("Analog Inputs")

  finalRow = .Cells(11, "B").SpecialCells(xlLastCell).Row

  For i = 11 To finalRow

    If .Cells(i, "AR").Value Like "4-20*" Then

       splitsiglvl = Split(.Cells(i, "AR").Value)

       splitsiglvl(0).Copy Destination:=Workbooks(AISCAN.xlsx).Sheets(1).Range("R" & e)

       splitsiglvl(1).Copy Destination:=Workbooks(AISCAN.xlsx).Sheets(1).Range("S" & e)

    End If

  Next i

End Sub
 
Upvote 0
If you want "splitsiglvl" to be an array, you need ellipses in your variable declaration, i.e.
Rich (BB code):
Dim splitsiglvl() As String
 
Upvote 0
Well, can't get it to work. Current code as follows, below. I'm getting a Compile Error: Invalid Qualifier on the first copy line.

VBA Code:
Sub Split_String()

Dim splitsiglvl() As String, finalRow As Long, e As Long, i As Long
e = Workbooks("AISCAN.xlsx").Sheets(1).Cells(Rows.Count, "C").End(xlUp).Row + 1
Workbooks("AISCAN.xlsx").Worksheets(1).Activate
'Set ws = Workbooks("NIC Master Database.xlsm").Worksheets("Analog Inputs")
With Workbooks("NIC Master Database.xlsm").Worksheets("Analog Inputs")
  finalRow = Workbooks("NIC Master Database.xlsm").Worksheets("Analog Inputs").Cells(11, "B").SpecialCells(xlLastCell).Row
  
  For i = 11 To finalRow
    If .Cells(i, "AR").Value Like "4-20*" Then
       splitsiglvl = Split(.Cells(i, "AR").Value)
       splitsiglvl(0).Copy Destination:=Workbooks(AISCAN.xlsx).Sheets(1).Range("R" & e)
       splitsiglvl(1).Copy Destination:=Workbooks(AISCAN.xlsx).Sheets(1).Range("S" & e)
    End If
  Next i
End With
End Sub
 
Upvote 0
A few other things:
1. If AISCAN.xlsx is the actual name of your workbook, it needs to be placed between double-quotes, otherwise it is treated as a variable.
2. I believe "Copy" is used with ranges, not values. You should just be able to set the values equal to your variables, i.e.
VBA Code:
Workbooks("AISCAN.xlsx").Sheets(1).Range("R" & e).Value = splitsiglvl(0)
Workbooks("AISCAN.xlsx").Sheets(1).Range("S" & e).Value = splitsiglvl(1)
 
Upvote 0
Are you sure it should be Workbooks(AISCAN.xlsx) rather than Workbooks("AISCAN.xlsx") in the copy lines?
I.AM.THE.LORD.OF.THE.IDIOTS

Thanks, still getting the same compile error. As the next reply suggests; maybe this can't be copied.....?
 
Upvote 0

Forum statistics

Threads
1,225,131
Messages
6,183,035
Members
453,146
Latest member
scarabeovini

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