Facebook FQLクエリからの情報をJSON形式で取得し、Excelに貼り付けました。結果の一部は次のとおりです。
"データ": [
{ "name": "Hilton Head Island - TravelTell", "location": { "street": "7 Office Way, Suite 215", "city": "Hilton Head Island", "state": "SC" }, "fan_count": 143234, "talking_about_count": 18234, "were_here_count": 4196 }, { "name": "Hilton Hawaiian Village Waikiki Beach Resort", "location": { "street": "2005 Kalia Road", "city": "Honolulu", "state": "HI" }, "fan_count": 34072, "talking_about_count": 4877, "were_here_count": 229999 }, { "name": "Hilton New York", "location": { "street": "1335 Avenue of the Americas", "city": "New York", "state": "NY" }, "fan_count": 12885, "talking_about_count": 969, "were_here_count": 72206 },
サブストリングを使用してデータを解析し、「name、street、city、state、fan_countなど」を使用して別のワークシートに列を作成しようとしています。列ヘッダーとして。現在、「name:」だけでこれを行うコードを試していますが、documentText=myRange.Textの行にヒットするとエラーが発生します。エラーが何であるかわかりません。
もう1つの問題は、文字列に引用符が含まれていることです。たとえば、SecondTermを "にしたいのですが、" "、"と等しくしようとするとエラーが発生します。
Sub Substring_Test()
Dim nameFirstTerm As String Dim nameSecondTerm As String Dim myRange As Range Dim documentText As String Dim startPos As Long 'Stores the starting position of firstTerm Dim stopPos As Long 'Stores the starting position of secondTerm based on first term's location Dim nextPosition As Long 'The next position to search for the firstTerm nextPosition = 1 'First and Second terms as defined by your example. Obviously, this will have to be more dynamic 'if you want to parse more than justpatientFirstname. firstTerm = "name"": """ secondTerm = """,""" 'Get all the document text and store it in a variable. Set myRange = Sheets("Sheet1").UsedRange 'Maximum limit of a string is 2 billion characters. 'So, hopefully your document is not bigger than that. However, expect declining performance based on how big doucment is documentText = myRange.Text 'Loop documentText till you can't find any more matching "terms" Do Until nextPosition = 0 startPos = InStr(nextPosition, documentText, firstTerm, vbTextCompare) stopPos = InStr(startPos, documentText, secondTerm, vbTextCompare) Debug.Print Mid$(documentText, startPos + Len(firstTerm), stopPos - startPos - Len(secondTerm)) nextPosition = InStr(stopPos, documentText, firstTerm, vbTextCompare) Loop Sheets("Sheet2").Range("A1").Value = documentText
サブ終了