VBA 小知识

1. 循环

Dim i As Integer

For i = 0 To 100
    body
Next

While i < 100
    body
Wend

 

2. 键值数据结构

    create dictionary object
    Set dictMembers = CreateObject("scripting.dictionary")
    add a key
    If Not dictMembers.exists(Key) Then
        add value
        Dim value(1 To 3) As Integer
        tempdata(1) = 0
        tempdata(2) = 0
        tempdata(3) = 0
        dictMembers.Add Key, value
    End If
    
    Dim keys()
    keys = dictMembers.keys
    
    read dictionary
    Dim value
    For i = 0 To dictMembers.Count - 1
        Key = keys(i)
        value = dictMembers.Item(keys(i))
        Me.Cells(i + 1, 2).value = value(1)
        Me.Cells(i + 1, 3).value = value(2)
        Me.Cells(i + 1, 4).value = value(3)
    Next

3. 数据图表chart

get the first chart
    Me.ChartObjects(1).Activate
    ‘set datasource
    ActiveChart.SetSourceData Source:=Range("A8:F" & i - 1)
    While ActiveChart.SeriesCollection.Count > 2
        ‘remove useless series
        ActiveChart.SeriesCollection(1).Delete
        ActiveChart.SeriesCollection(1).Delete
        ActiveChart.SeriesCollection(1).Delete
    Wend
    display value
    ActiveChart.SeriesCollection(1).ApplyDataLabels
    ActiveChart.SeriesCollection(2).ApplyDataLabels

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。