Need help to improve my VBA code

smide

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

I'm using the following code to parse my raw report from row1-Sheet5 and transfer data to Sheet4, creating this table:

OPQR
1
2id
shop
store
factory
33456231525
43467121628
53468241933
6................

<tbody>
</tbody>
Data in row1-Sheet5 are organized in the following pattern: id - shop - store - factory

My problem is reflected in the fact that sometimes between the two id's there are no this "shop-store-factory" cells which means that my prices are often in the wrong row in above table (next to the wrong id).

I would like to re-arrange this code so in the case that between the two id's there are no this cells which contains significant text (shop-store-factory), then, in the table, that row stays empty.

example:

(id-shop-store-factory) (id-shop-store-factory) (id) (id-shop-store-factory) (id-shop-store-factory) ....


OPQR
1
2idshopstorefactory
33456231525
43467121628
53468
63470241933
73471271139

<tbody>
</tbody>
Here is the code I used so far:
Code:
Sub Salary()

[B]' extract id's from row 1 Sheet5
[/B]
Dim C As Long, X As Long, Data As Variant, Result As Variant
  Data = Sheets("Sheet5").Range("A1", Sheets("Sheet5").Cells(1, Columns.Count).End(xlToLeft))
  ReDim Result(1 To UBound(Data, 2), 1 To 1)
  For C = 1 To UBound(Data, 2)
    If Data(1, C) Like "*""[Ii][Dd]"":#*" And _
       Left(LCase(Data(1, C)), 14) <> "annual:[{""id"":" And _
       Left(LCase(Data(1, C)), 15) <> "account:[{""id"":" Then
      X = X + 1
      Result(X, 1) = Mid(Data(1, C), InStrRev(Data(1, C), ":") + 1)
    End If
  Next
  Sheets("Sheet4").Range("O3").Resize(UBound(Result)) = Result


[B]'extract shop-store-factory prices from row 1 Sheet5
[/B]
Dim a As Variant, b As Variant
  Dim i As Long, k As Long, pos As Long
  
  With Sheets("Sheet5")
    a = .Range("A1", .Cells(1, .Columns.Count).End(xlToLeft)).Value
  End With
  ReDim b(1 To UBound(a, 2), 1 To 3)
  For i = 1 To UBound(a, 2) - 2
    pos = InStr(1, a(1, i), "{""shop"":", vbTextCompare)
    If pos > 0 And IsNumeric(Mid(a(1, i), pos + 8)) Then
      k = k + 1
      b(k, 1) = Val(Mid(a(1, i), pos + 8))
      b(k, 2) = Val(Mid(a(1, i + 1), InStr(1, a(1, i + 1), ":") + 1))
      b(k, 3) = Val(Mid(a(1, i + 2), InStr(1, a(1, i + 2), ":") + 1))
    End If
  Next i
  If k > 0 Then
    Sheets("Sheet4").Range("P3:R3").Resize(k).Value = b
  End If

End Sub
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
your new table is the same as the original table ie first 2 rows are identical


Yes, but the whole point is in row five (or row three if you exclude first two) which is empty because there is no this shop-store-factory cells between two id's (between 3rd and 4th id).
Of course the 3rd id is equal to 3468 ie the contents of that cell is: {"id":3468
 
Last edited:
Upvote 0
if your data was this
id188
id2
id366
id4
id544
is your desired output
id188
id366
id544

<colgroup><col span="2"></colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,215,950
Messages
6,127,906
Members
449,411
Latest member
AppellatePerson

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