简体   繁体   中英

split string vba

I'm looking to split the string that the codes gives me. The output of the code is for example, "DPJobState-02-Mar-2020" then "DPJobState-03-Mar-2020" and so on, I want to have as an output only "02-Mar-2020" for that worksheet

Dim contador As Integer
contador = 2
Dim result As Workbook
Set result = Workbooks.Open("C:\Users\apractica\Desktop\Macro\Durst\ResultadosDurst.xlsm")
Dim m_2 As Variant

For Each ws In wb.Worksheets
    result.Worksheets(1).Cells(contador, 1).Value = ws.name
    contador = contador + 1
Next

Use Split . If you want two substrings returned, make the limit argument 2 , and then use (1) to return the 2nd substring.

So with your example:

 Split("DPJobState-02-Mar-2020","-",2)(1)

The resulting two substrings are

  • "DPJobState"
  • "02-Mar-2020"

And (1) returns the 2nd since this is a zero-based array.

Though maybe more simply, if your sheet names always end with a date formatted as "dd-mmm-yyyy", then you could just use Right$ :

result.Worksheets(1).Cells(contador, 1).Value = Right$(ws.name, 11)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM