当我从HTML解析数据时,我遇到了字符串之间的间距问题。这很难解释,但我会尽我所能。所以我的代码运行一个HTML文件,并将每个数据表复制到excel的A列中的一个单元格中。因此,当代码编译时,我在单元格A1到A10(例如)中填充了每个单元格中的大量数据。我想要做的就是扫描细胞并寻找某些字符串。示例代码为:
sot = Application.WorksheetFunction.CountIf(Range("A:A"), "eggs and bacon")
我要找的字符串是带空格的多个单词。但是,我注意到,当解析HTML数据并将其传输到excel时,字母之间的间距会混乱。例如,如果我单击进入单元格并按下单元格上的左右箭头光标,则闪烁的光标线有时会在字母的正中间结束,而不是应该是两个字母之间的两个字母。这导致我的代码不起作用,因为无法正确确定字符串之间的空格数,因此无法找到字符串。
因此,当确实存在"鸡蛋和培根时,sot返回值0。数据中的字符串。
如果您有任何我可以做的事情/如果您之前已经看过或遇到过这个问题,请告诉我!我变得非常沮丧,因为我花了很多时间来制作解析HTML的代码和扫描我的数据寻找关键字的代码,但它只是因为从HTML转到Excel时间距变得混乱而无法工作。非常感谢。
以下是解析数据的代码:
Private Sub HTMLParser()
'This code will go to the html page and parse the page for the relevant data and put it into excel
'Select Cell A1 So Code Works As Intended
Range("A1").Select
'Declare variables
Dim ie As Object, i As Long, strText As String
Dim doc As Object, hTable As Object, hBody As Object, hTR As Object, hTD As Object
Dim tb As Object, bb As Object, tr As Object, td As Object
Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet
Set wb = Excel.ActiveWorkbook
Set ws = wb.ActiveSheet
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
y = 1 'Column A in Excel
z = 1 'Row 1 in Excel
'Enter Desired URL
ie.navigate "MY URL HERE", , , , "Content-Type: application/x-www-form-urlencoded" & vbCrLf
'Wait until page fully loads
Do While ie.Busy: DoEvents: Loop
Do While ie.readyState <> 4: DoEvents: Loop
Set doc = ie.document
Set hTable = doc.getElementsByTagName("table")
'Loop through HTML Tags and paste every HTML table cell into excel
For Each tb In hTable
Set hBody = tb.getElementsByTagName("tbody")
For Each bb In hBody
Set hTR = bb.getElementsByTagName("tr")
For Each tr In hTR
Set hTD = tr.getElementsByTagName("td")
y = 1 ' Resets back to column A
For Each td In hTD
ws.Cells(z, y).Value = td.innerText
y = y + 1
Next td
DoEvents
z = z + 1
Next tr
Exit For
Next bb
Exit For
Next tb
End Sub