Excel VBA - Dictionary - 存储和检索值 [英] Excel VBA - Dictionary - storing and retrieving values

查看:209
本文介绍了Excel VBA - Dictionary - 存储和检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在下表和Excel VBA - 字典的帮助下 - 我正在尝试捕获所有的细节 -
1)第一步是搜索结果输出列 - 如果值为否 - 那么我们需要用他们适当的标题来读取所有的值。
2)所以对于第二个记录 - 即Name = XYZ - 我们需要存储所有的细节。根据科目数列 - 我们需要存储所有科目及其相应标记的价值 - 将用于进一步计算,并生成结果列。

I am working on below table and with help of Excel VBA - Dictionary - I am trying to capture all the details - 1) First step is to search in "Results Out" Column - if the value is "No" - then we need to read all the values with their appropriate header. 2) So for 2nd record - i.e., Name = XYZ - we need to store all the details. Based on No. of Subjects column - we need to store value of all the subjects and their corresponding marks - will be used for further calculation and generate the "Result" column.

我有一部分工作 - 像我能够捕获细节 - 但不能存储所有主题和标记的细节:

I got it partially working - like I am able to capture details - but not able to store details of all the subject and their marks:

Sr. No. Results Out?    Result  Name    Age No. of Subjects Subject Names   Marks
1           Yes          Pass   ABC      21       3          Maths           10
                                                             Science         26
                                                             History         34
2           No                  XYZ      10       2          Maths           24
                                                             Science         36

以下是我使用的部分工作的代码:

Below is the code that I have used that is partially working:

Public Sub test_dict()

Dim dict As New Scripting.dictionary
Set dict = New dictionary

sSheetIndex = 1
intTargetRow = 2

Set objUsedRange = Worksheets.Item(3).UsedRange

For Iter = 1 To objUsedRange.Columns.Count
   sCellName = objUsedRange.Cells(1, Iter)
   sCellValue = objUsedRange.Cells(intTargetRow, Iter)
   dict.Item(sCellName) = sCellValue

Next


For i = 0 To dict.Count - 1
    s = dict.Items()(i)
    Debug.Print dict.Keys()(i) & " " & dict.Items()(i)
    Debug.Print s
Next i

End Sub


推荐答案

使用以下代码解决问题 - 必须使用2个单独的字典:

Resolved the issue with below code - had to use 2 seperate dictionaries:

Public Sub test_dict()

Dim dict As New Scripting.dictionary
Set dict = New dictionary

sSheetIndex = 1
intTargetRow = 2

Set objUsedRange = Worksheets.Item(3).UsedRange

For Iter = 1 To objUsedRange.Columns.Count
   sCellName = objUsedRange.Cells(1, Iter)
   sCellValue = objUsedRange.Cells(intTargetRow, Iter)
   dict.Item(sCellName) = sCellValue

    If sCellName = "Subject Names" Then
        Call test_dict_2
    End If

Next

For i = 0 To dict.Count - 1
    s = dict.Items()(i)
    Debug.Print dict.Keys()(i) & " " & dict.Items()(i)
    Debug.Print s
Next i

End Sub



Public Sub test_dict_2()

Dim dict_2 As New Scripting.dictionary
Set dict_2 = New dictionary

sSheetIndex = 1
intTargetRow = row_counter

Set objUsedRange = Worksheets.Item(3).UsedRange

For Iter = 1 To objUsedRange.Columns.Count
   sHeader = objUsedRange.Cells(1, Iter)
   sCellValue = objUsedRange.Cells(intTargetRow, Iter)

    If sHeader = "No. of Subjects" Then
        mv_cnt = sCellValue
    End If

    If sHeader = "Subject Names" Then

        Dim a
        a = Iter + mv_cnt
        For Iter_2 = Iter To (a - 1)

            sHeader = objUsedRange.Cells(1, Iter)
            sCellName = objUsedRange.Cells(intTargetRow, Iter)
            sCellValue = objUsedRange.Cells(intTargetRow, Iter + 1)
            dict_2.Item(sCellName) = sCellValue
            intTargetRow = intTargetRow + 1

        Next

        intTargetRow = row_counter

    End If

Next

For i = 0 To dict_2.Count - 1
    s = dict_2.Items()(i)
    Debug.Print dict_2.Keys()(i) & " " & dict_2.Items()(i)
    Debug.Print s
Next i

Set dict_2 = Nothing

End Sub

这篇关于Excel VBA - Dictionary - 存储和检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆