VBA to get a number for a HTML style value without the "%" suffix

smide

Board Regular
Joined
Dec 20, 2015
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hello.


In cell A1 (Sheet 1) I'm receiving a huge HTML code. I need to extract somehow values for 'top' and 'left' without % suffix.


for: style="top: 54%; left: 13%; I need only 54 and 13


Results should be placed in column B for top values (B2:B300) and column C for left values (C2:C300).




example.


HTML in cell A1: style="top: 69%; left: 8%; background-color: rgb(0, 255, 255); style="top: 3%; left: 61%;....


Results after macro run (Sheet 1):


[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD][/TD]
[TD="align: center"]A[/TD]
[TD="align: center"]B[/TD]
[TD="align: center"]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]style="top: 69%; left: 8%; background-color: rgb(0, 255, 255); style="top: 3%; left: 61%;....[/TD]
[TD]Top[/TD]
[TD]Left[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD="align: center"]69[/TD]
[TD="align: center"]8[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD="align: center"][/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]61[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD="align: center"]....[/TD]
[TD="align: center"]....[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"][/TD]
[TD="align: center"]....[/TD]
[TD="align: center"]....[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this for results starting "B2".
Code:
[COLOR="Navy"]Sub[/COLOR] MG06Sep24
[COLOR="Navy"]Dim[/COLOR] txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] a, b
txt = Replace(Range("A1"), ";", ":")
Sp = Split(txt, ":")
a = 1: b = 1
[COLOR="Navy"]For[/COLOR] n = 0 To UBound(Sp)
    [COLOR="Navy"]If[/COLOR] Sp(n) Like "*top" [COLOR="Navy"]Then[/COLOR]
        a = a + 1
        [COLOR="Navy"]With[/COLOR] Cells(a, "B")
            .Value = Trim(Sp(n + 1))
            .NumberFormat = "general"
            .Value = .Value * 100
        [COLOR="Navy"]End[/COLOR] With
    [COLOR="Navy"]ElseIf[/COLOR] Sp(n) Like "*left" [COLOR="Navy"]Then[/COLOR]
         b = b + 1
        [COLOR="Navy"]With[/COLOR] Cells(b, "C")
            .Value = Trim(Sp(n + 1))
            .NumberFormat = "general"
            .Value = .Value * 100
        [COLOR="Navy"]End[/COLOR] With
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
You could start with something like this:

Code:
Sub Test1()
 
Dim temp
Dim ssTop As String
Dim sLeft As String
ssTop = ""
sLeft = ""
 
temp = Split(Range("A1").Value, " ")
For i = 0 To UBound(temp)
    If InStr(temp(i), "top") Then
        
        ssTop = Left(temp(i + 1), Len(temp(i + 1)) - 2)
        
    End If
    If InStr(temp(i), "left") Then
        sLeft = Left(temp(i + 1), Len(temp(i + 1)) - 2)
    End If
    
If ssTop <> "" And sLeft <> "" Then
 
    MsgBox ssTop & " " & sLeft
    ssTop = ""
    sLeft = ""
End If

 Next i
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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