Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1933|回复: 7

[已解决]EXCEL提取信息,总是不能提取前2行

[复制链接]
发表于 2013-6-9 17:08 | 显示全部楼层 |阅读模式
QQ图片20130609122738.jpg

QQ图片20130609122918.jpg

部分代码

strFind = "<table class=""cpzs_table mt10"">"

    strText = Split(strText, strFind)(1)

    strText = Split(strText, "</table>")(0)

    arr1 = Split(strText, "</tr>")

    m = m + 1

    ReDim result(1 To UBound(arr1), 1 To 6)



    '标题

    result(m, 1) = "期数"

    result(m, 2) = "开奖号码"


    For j = 4 To UBound(arr1)

        If arr1(j) Like "<tr><td>*" Then

            arr2 = Split(arr1(j), "</td>")

            m = m + 1


            '第1列期数

            result(m, 1) = Split(Split(arr1(j), "</td")(0), "<td>")(1)

            '第2列到6列

            For k = LBound(arr2) + 1 To UBound(result, 2) - 1

                result(m, k + 1) = Split(Split(arr2(k), "<span class=""spNum"">")(1), "<")(0)

            Next

        Else

            Exit For

        End If

    Next


66666666666.rar (6.36 KB, 下载次数: 11)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-6-9 18:58 | 显示全部楼层
回复

使用道具 举报

发表于 2013-6-9 19:25 | 显示全部楼层
回复

使用道具 举报

发表于 2013-6-10 07:37 | 显示全部楼层
  1. Sub 查询福彩前4页()

  2.     Dim strDate$
  3.     Dim strUrl$
  4.     Dim strText$
  5.     Dim strFind$
  6.     Dim ARR1, arr2, result(1 To 600, 1 To 6)
  7.     Dim J As Byte, k As Byte, M As Integer
  8.     Dim httpRequest As Object
  9.     Dim btPage As Byte

  10.     M = 1
  11.     '标题

  12.     Dim objRegExp As Object
  13.     Set objRegExp = CreateObject("VBScript.regExp")
  14.     With objRegExp
  15.         .Global = True
  16.         .Pattern = "\<tr\>\<td\>.*"
  17.     End With

  18.     For btPage = 1 To 5
  19.         strUrl = "http://fjtc.com.cn/Line-1105?Page=" & btPage
  20.         Set httpRequest = CreateObject("Msxml2.XMLHTTP.3.0")
  21.         With httpRequest
  22.             .Open "GET", strUrl, False
  23.             .send
  24.             strText = .responseText
  25.         End With

  26.         strFind = "<table class=""cpzs_table mt10"">"
  27.         strText = Split(strText, strFind)(1)
  28.         strText = Split(strText, "</table>")(0)
  29.         ARR1 = Split(strText, "</tr>")

  30.         For J = 3 To UBound(ARR1)
  31.             With objRegExp
  32.                 If .test(ARR1(J)) Then
  33.                     strtemp = .Execute(ARR1(J))(0)
  34.                 Else
  35.                     Exit For
  36.                 End If
  37.             End With
  38.             arr2 = Split(strtemp, "</td>")

  39.             M = M + 1
  40.             '第1列期数
  41.             result(M, 1) = Split(Split(strtemp, "</td")(0), "<td>")(1)
  42.             '第2列到6列
  43.             For k = LBound(arr2) + 1 To UBound(result, 2) - 1
  44.                 result(M, k + 1) = Split(Split(arr2(k), "<span class=""spNum"">")(1), "<")(0)
  45.             Next
  46.         Next
  47.     Next

  48.     Application.ScreenUpdating = False

  49.     With Range("a1")
  50.         .CurrentRegion.ClearContents
  51.         .Resize(M, UBound(result, 2)).Value = result
  52.         .Value = "期数"
  53.         .CurrentRegion.EntireColumn.AutoFit

  54.     End With
  55.     With Range("b1")
  56.         .Value = "号码"
  57.         .Resize(, 5).HorizontalAlignment = xlCenterAcrossSelection
  58.     End With
  59.     Application.ScreenUpdating = True
  60.     MsgBox "提取完成"
  61. End Sub
复制代码
处理第3行的文本,用字符串的方法也可以。
回复

使用道具 举报

发表于 2013-6-10 07:43 | 显示全部楼层    本楼为最佳答案   
字符串方法略快0.02秒。
关键是简单易懂。
  1. Sub 查询福彩前4页2()

  2.     Dim strDate$
  3.     Dim strUrl$
  4.     Dim strText$
  5.     Dim strFind$
  6.     Dim ARR1, arr2, result(1 To 600, 1 To 6)
  7.     Dim J As Byte, k As Byte, M As Integer
  8.     Dim httpRequest As Object
  9.     Dim btPage As Byte

  10.     M = 1
  11.     '标题


  12.     For btPage = 1 To 5
  13.         strUrl = "http://fjtc.com.cn/Line-1105?Page=" & btPage
  14.         Set httpRequest = CreateObject("Msxml2.XMLHTTP.3.0")
  15.         With httpRequest
  16.             .Open "GET", strUrl, False
  17.             .send
  18.             strText = .responseText
  19.         End With

  20.         strFind = "<table class=""cpzs_table mt10"">"
  21.         strText = Split(strText, strFind)(1)
  22.         strText = Split(strText, "</table>")(0)
  23.         ARR1 = Split(strText, "</tr>")

  24.         For J = 3 To UBound(ARR1)
  25.             strtemp = ARR1(J)
  26.             If strtemp Like "*<tr><td>*" Then
  27.                 strtemp = Mid(strtemp, InStr(strtemp, "<tr><td>"))
  28.             Else
  29.                 Exit For
  30.             End If

  31.             arr2 = Split(strtemp, "</td>")

  32.             M = M + 1
  33.             '第1列期数
  34.             result(M, 1) = Split(Split(strtemp, "</td")(0), "<td>")(1)
  35.             '第2列到6列
  36.             For k = LBound(arr2) + 1 To UBound(result, 2) - 1
  37.                 result(M, k + 1) = Split(Split(arr2(k), "<span class=""spNum"">")(1), "<")(0)
  38.             Next
  39.         Next
  40.     Next

  41.     Application.ScreenUpdating = False

  42.     With Range("a1")
  43.         .CurrentRegion.ClearContents
  44.         .Resize(M, UBound(result, 2)).Value = result
  45.         .Value = "期数"
  46.         .CurrentRegion.EntireColumn.AutoFit

  47.     End With
  48.     With Range("b1")
  49.         .Value = "号码"
  50.         .Resize(, 5).HorizontalAlignment = xlCenterAcrossSelection
  51.     End With
  52.     Application.ScreenUpdating = True
  53.     MsgBox "提取完成"
  54. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-6-10 11:03 | 显示全部楼层
hwc2ycy 发表于 2013-6-10 07:43
字符串方法略快0.02秒。
关键是简单易懂。

可不可以不要标题?第一行直接是号码的
QQ20130609122918.jpg

变成 QQ图片20130609122918.jpg
回复

使用道具 举报

发表于 2013-6-10 11:27 | 显示全部楼层
  1. Sub 查询福彩前4页2()

  2.     Dim strDate$
  3.     Dim strUrl$
  4.     Dim strText$
  5.     Dim strFind$
  6.     Dim strTemp$
  7.     Dim ARR1, arr2, result(1 To 600, 1 To 6)
  8.     Dim J As Byte, k As Byte, M As Integer
  9.     Dim httpRequest As Object
  10.     Dim btPage As Byte

  11.     For btPage = 1 To 5
  12.         strUrl = "http://fjtc.com.cn/Line-1105?Page=" & btPage
  13.         Set httpRequest = CreateObject("Msxml2.XMLHTTP.3.0")
  14.         With httpRequest
  15.             .Open "GET", strUrl, False
  16.             .send
  17.             strText = .responseText
  18.         End With

  19.         strFind = "<table class=""cpzs_table mt10"">"
  20.         strText = Split(strText, strFind)(1)
  21.         strText = Split(strText, "</table>")(0)
  22.         ARR1 = Split(strText, "</tr>")

  23.         For J = 3 To UBound(ARR1)
  24.             strTemp = ARR1(J)
  25.             If strTemp Like "*<tr><td>*" Then
  26.                 strTemp = Mid(strTemp, InStr(strTemp, "<tr><td>"))
  27.             Else
  28.                 Exit For
  29.             End If

  30.             arr2 = Split(strTemp, "</td>")

  31.             M = M + 1
  32.             '第1列期数
  33.             result(M, 1) = Split(Split(strTemp, "</td")(0), "<td>")(1)
  34.             '第2列到6列
  35.             For k = LBound(arr2) + 1 To UBound(result, 2) - 1
  36.                 result(M, k + 1) = Split(Split(arr2(k), "<span class=""spNum"">")(1), "<")(0)
  37.             Next
  38.         Next
  39.     Next

  40.     Application.ScreenUpdating = False

  41.     With Range("a1")
  42.         .CurrentRegion.ClearContents
  43.         .Resize(M, UBound(result, 2)).Value = result
  44.         .CurrentRegion.EntireColumn.AutoFit
  45.     End With
  46.    
  47.     Application.ScreenUpdating = True
  48.     MsgBox "提取完成"
  49. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-6-10 13:22 | 显示全部楼层
hwc2ycy 发表于 2013-6-10 11:27

thank you
and 另一个问题
求同时不出数组的间隔次数
QQ图片20130610132243.jpg
如计算一行中同时不出现1,2,3,就是最后一行符合条件
sheet2.rar (217.69 KB, 下载次数: 3)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-27 10:19 , Processed in 0.777895 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表