vba十万刚数据遍历怎么快

vba十万刚数据遍历怎么快,第1张

首先要有一个数组,然后设置一个变量X,这个X的属性是Variant,可变型,如果设置为其它类型就错,没有为什么,这个是VBA内部规则。

然后引入循环中进行比较,比较方法就是这个X变量的值和要查找的值之间比较。

这里要做一个判断语句,也就是IF...Then...End 或者其它判断语句。

如果两者相等,那么就找到了,说明当前数组元素值是要查找的值。

然后,进行相应的数据计算或者其它 *** 作。

如果要继续查找,返回继续循环,如果不想找了,就退出循环,可以使用Exit语句。

尝试用下边代码试试:

Sub OpenAndClose()

  Dim MyFile As String

  Dim s As String

  Dim count As Integer

  MyFile = Dir(文件夹目录 &"*.xlsx")

  '读入文件夹中的第一个.xlsx文件 

  count = count + 1       '记录文件的个数  

  s = s &count &"、" &MyFile

  Do While MyFile <>""

      MyFile = Dir        '第二次读入的时候不用写参数  

      If MyFile = "" Then

          Exit Do         '当MyFile为空的时候就说明已经遍历完了,这时退出Do,否则还要运行一遍  

      End If

      count = count + 1

      If count Mod 2 <>1 Then

          s = s &vbTab &count &"、" &MyFile

      Else

          s = s &vbCrLf &count &"、" &MyFile

      End If

  Loop

  Debug.Print s

End Sub

另外,可以考虑用python试试

Sub cnft_click()

    Dim arrAllData, arrb, c As Range, rg As Range, i, a, r, col, n

    Application.ScreenUpdating = False

    

    n = 30 '显示最近的n天记录,可调整

    

    Set rg = Range(Cells(2, 1), Cells(65536, 1).End(xlUp))

    On Error Resume Next

    For Each c In rg

        arrAllData = GetStockAllData(c.Text, n)

        If TypeName(arrAllData) <> "Empty" Then

            ReDim arrb(1 To 1, 0 To UBound(arrAllData))

            For i = 0 To UBound(arrAllData)

                arrb(1, i) = arrAllData(i, 1) '只取第2项

            Next i

            With c.Offset(, 3).Resize(1, UBound(arrb, 2))

                .ClearContents

                .Cells = arrb

            End With

        End If

    Next

    Application.ScreenUpdating = True

End Sub

Function GetStockAllData(StockCode As String, n)

    On Error Resume Next

    Dim s

    s = StockCode

    If Len(s) <> 6 Then Exit Function

    

    '判断上证还是深证

    Select Case Left(s, 2)

        Case 60

            s = "sh" & s

        Case 0

            s = "sz" & s

        Case Else

            s = "sh600000"

    End Select

    

    Dim iDate1 As String, iDate2 As String, i, j

    iDate1 = Format(Date, "yyyymmdd")

    iDate2 = Format(Date - n, "yyyymmdd")

    s = "http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=" _

        & s & Chr(38) & "end_date=" & iDate1 & Chr(38) & "begin_date=" & iDate2

    

    '开始读取XML内容

    Dim XML, objNode, objAtr As Object, nCntChd, nCntAtr As Long

    Set XML = CreateObject("Microsoft.XMLDOM")

    With XML

        .async = False

        .Load (s)

    End With

    Set objNode = XML.documentElement

    nCntChd = objNode.ChildNodes.Length - 1 'XML的记录个数

    If nCntChd < 1 Then GoTo 10000

    nCntAtr = objNode.ChildNodes.Item(0).Attributes.Length - 1

    

    Dim arrA

    ReDim arrA(0 To nCntChd, 0 To nCntAtr)

    '开始遍历

    For i = 0 To nCntChd '遍历每一天(1条/天)

        Set objAtr = objNode.ChildNodes.Item(i)

        'bl="" v="84145" l="20.960" c="22.390" h="22.540" o="20.960" d="2014-10-29"

        arrA(i, 1) = objAtr.Attributes.Item(1).Text '只取第2项(上例中o="20.960")

        '遍历每条记录里的所有记录项,从0开始

'        For j = 0 To nCntAtr

'            arrA(i, j) = objAtr.Attributes.Item(j).Text

'        Next j

    Next i

    

10000:

    Set objAtr = Nothing

    Set objNode = Nothing

    Set XML = Nothing

    If Err.Number <> 0 Then

        MsgBox ("查不到股票(" & StockCode & ")信息")

        Err.Clear

    End If

    On Error GoTo 0

    GetStockAllData = arrA

End Function

改了改,看行不


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6748311.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-27
下一篇2023-03-27

发表评论

登录后才能评论

评论列表(0条)

    保存