VBA how to extract with variable lenght..

lemming78

New Member
Joined
Jan 21, 2013
Messages
7
Hello, i have this worksheet
Code:
[TABLE="width: 1431"]
<tbody>[TR]
[TD="colspan: 7"]DateTime,Name,Country,Volatility,Actual,Previous,Consensus[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Data[/TD]
[TD]Ora[/TD]
[TD]Nome[/TD]
[TD]Country[/TD]
[TD]Volatility[/TD]
[TD]Actual[/TD]
[TD]Previous[/TD]
[TD]Consensus[/TD]
[/TR]
[TR]
[TD="colspan: 5"]20121001 02:00:00{Labour Day}{Australia}{0}{}{}{}[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]20121001[/TD]
[TD="align: right"]02:00:00[/TD]
[TD]Labour Day}{Australi[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="colspan: 5"]20121001 02:00:00{National Day}{China}{0}{}{}{}[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"]20121001[/TD]
[TD="align: right"]02:00:00[/TD]
[TD]National Day}{Ch[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

and i need to extract parts of the rows into the proper cells on the right. I have written this piece of code but i don't know how to stop the search function to the first result. It always find the last bracket and not the first, in order to stop.

Code:
Public Sub CalendarSheet()Dim stringa As Variant, i As Integer
'Worksheets.Add After:=Sheets(1)
'ActiveSheet.Name = "Dati"
'nomi in Dati
'Range("A1").Value = "Data"
'Range("B1").Value = "Ora"
'Range("C1").Value = "Nome"
'Range("D1").Value = "Country"
'Range("E1").Value = "Volatility"
'Range("F1").Value = "Actual"
'Range("G1").Value = "Previous"
'Range("H1").Value = "Consensus"
'Range("A1:H1").EntireColumn.AutoFit
'Range("A1:H1").Copy
'sostituzione valori non corretti prima dei simboli valuta
'Worksheets("eventdates").Select
'Range("K1").Select
'ActiveSheet.Paste


Range("A1").Select
Cells.Replace What:="Â", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False


'sistemare in colonne a fianco i dati
i = 2
For Each stringa In Range("A2:A720")
Range("K" & i).Value = Left(stringa, 8)
Range("L" & i).Value = Mid(stringa, 10, 8)
Range("M" & i).Value = Mid(stringa, WorksheetFunction.Search("{", stringa) + 1, Len(stringa) - Range("A" & i).Find("}", , , , , xlPrevious))


i = i + 1
Next


End Sub

Thanks!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Code:
Range("M" & i).Value = split(split(stringa, "{",)(1),"}")(0)

Hope it helps
 
Last edited:
Upvote 0
Code:
Range("M" & i).Value = split(split(stringa, "{",)(1),"}")(0)

Hope it helps


Thanks. It doesn't work. A msgbox is prompted, expression missing is the error

if i modify it this way

Code:
Range("M" & i).Value = Split(Split(stringa, "{")(1), "}")(0)

error is index is not included in the interval

I need to do this by macro because i need a macro for multiple sheets:)
 
Upvote 0
With Text-to-Columns it would be like:

Code:
Sub Test()

    
With Range("A2:A100")
    
    .TextToColumns Destination:=.Range("B1"), DataType:=xlDelimited, Other:=True, OtherChar:="{"
    
    With .Columns("B:H")
        .Replace What:="}", Replacement:="", LookAt:=xlPart
        .AutoFit
    End With
End With
End Sub
 
Upvote 0
With Text-to-Columns it would be like:

Code:
Sub Test()

    
With Range("A2:A100")
    
    .TextToColumns Destination:=.Range("B1"), DataType:=xlDelimited, Other:=True, OtherChar:="{"
    
    With .Columns("B:H")
        .Replace What:="}", Replacement:="", LookAt:=xlPart
        .AutoFit
    End With
End With
End Sub

more than perfect. Thanks!
 
Upvote 0
Thanks. It doesn't work. A msgbox is prompted, expression missing is the error

if i modify it this way

Code:
Range("M" & i).Value = Split(Split(stringa, "{")(1), "}")(0)

error is index is not included in the interval

I need to do this by macro because i need a macro for multiple sheets:)


Sorry my firefox make me crazy this 2 days.

The following should work for you.
Code:
Range("M" & i).Value = Split(Split(stringa, "{")(1), "}")(0)
 
Upvote 0

Forum statistics

Threads
1,203,249
Messages
6,054,378
Members
444,721
Latest member
BAFRA77

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