基于其他单元格值将单元格值添加到字典中

时间:2016-08-19 05:24:04

标签: vba excel-vba excel

我必须根据列(状态)将列(Number)的值添加到Dictionary中。 如果列(状态)具有E,则添加到dictionary_1 然后再添加到dictionary_2。

enter image description here

如果列(数字)同时具有" E"并将其添加到字典中。下面可以做些什么来纠正这个问题?

Set DATA = wkb.Worksheets("download")
DATA.Activate  

currentReadRow_status = START_ROW_DOWNLOAD

currentReadVariable = Trim(CStr(DATA.Cells(currentReadRow, COL_DOWNLOAD_NUMBER)))


download_status = Trim(CStr(DATA.Cells(currentReadRow_status, COL_DOWNLOAD_STATUS)))

  While (currentReadVariable <> "")
  If (download_status = "E") Then
     If Not (DOWNLOAD_ERROR.Exists(currentReadVariable)) Then
     DOWNLOAD_ERROR.Add currentReadVariable, download_status
     End If
  Else
    If Not (DOWNLOAD_NOERROR.Exists(currentReadVariable)) Then
    DOWNLOAD_NOERROR.Add currentReadVariable, download_status
    End If
 End If
    currentReadRow_status = currentReadRow_status + 1
    currentReadVariable = Trim(CStr(DATA.Cells(currentReadRow_status, COL_DOWNLOAD_NUMBER)))
    download_status = Trim(CStr(DATA.Cells(currentReadRow_status, COL_DOWNLOAD_STATUS)))
Wend

2 个答案:

答案 0 :(得分:0)

我认为您对逻辑有问题,两个dicts包含相同的值,因为当您将值插入noerror时,您没有检查是否已将此值插入error字典和vise中反之,所以你的修改后的代码如下,我想它可以解决问题:

While (currentReadVariable <> "")

If (download_status = "E" And _
    Not DOWNLOAD_ERROR.Exists(currentReadVariable) And _
        Not DOWNLOAD_NOERROR.Exists(currentReadVariable)) Then

        DOWNLOAD_ERROR.Add currentReadVariable, download_status

ElseIf (Not DOWNLOAD_NOERROR.Exists(currentReadVariable) And _
            Not DOWNLOAD_ERROR.Exists(currentReadVariable)) Then

        DOWNLOAD_NOERROR.Add currentReadVariable, download_status
End If

currentReadRow_status = currentReadRow_status + 1
currentReadVariable = Trim(CStr(Data.Cells(currentReadRow_status, COL_DOWNLOAD_NUMBER)))
download_status = Trim(CStr(Data.Cells(currentReadRow_status, COL_DOWNLOAD_STATUS)))

Wend

另外,正如我在你的帖子下面的评论中提到的,可能是比较时遇到的一些问题,例如,当你将一些文本与数字进行比较时,可能不是你当前的情况,但你应该考虑到“区分大小写” “和这些任务的变量类型。要消除区分大小写,可以使用以下标准方法:

Sub yourSub()
Dim Dic as Object: Set Dic = CreateObject("Scripting.Dictionary")
Dic.CompareMode = vbTextCompare '<~~~~~ remove case sensitivity for dictionary
'''code'''
End Sub

或使用其他方式:

Option Compare Text '<~~~~~ remove case sensitivity for module
Sub yourSub()
Dim Dic as Object: Set Dic = CreateObject("Scripting.Dictionary")
'''code'''
End Sub

另外,请注意trim,有时最好使用worksheetfunction.trim代替trim,下面的代码会显示您的不同之处:

Sub testTrim()
Dim sTrim$, sWFTrim$, stringcheck$

stringcheck = "1    1   1    11111111"

sTrim = Trim(stringcheck)
sWFTrim = WorksheetFunction.Trim(stringcheck)

MsgBox "This is how `Trim` works: [" & sTrim & _
        "], LEN is: " & Len(sTrim) & vbNewLine & _
        "This is how `worksheetfunction.trim` works:[" & _
        sWFTrim & "], LEN is: " & Len(sWFTrim)

End Sub

enter image description here

我希望它会有所帮助。

答案 1 :(得分:0)

您可以首先使用“E”处理数据并填写DOWNLOAD_ERROR字典,然后通过初步检查处理其他数据 number 不在DOWNLOAD_ERROR字典中

如下:

Dim cell As Range

With wkb.Worksheets("download") '<--| refer wanted worksheet in wanted workbook
    With .Range(.Cells(2, COL_DOWNLOAD_STATUS), .Cells(.Rows.Count, COL_DOWNLOAD_NUMBER).End(xlUp)).Columns(1) '<--| consider its columns A from row 1 down to last non empty row
        For Each cell In .SpecialCells(XlCellType.xlCellTypeConstants) '<--| loop through non blank ("E") cells only and fill DOWNLOAD_ERROR dictionary
            currentReadVariable = Trim(CStr(cell.Offset(, COL_DOWNLOAD_NUMBER - COL_DOWNLOAD_STATUS)))
            If Not (DOWNLOAD_ERROR.Exists(currentReadVariable)) Then DOWNLOAD_ERROR.Add currentReadVariable, download_status
        Next cell

        MsgBox .SpecialCells(XlCellType.xlCellTypeBlanks).Address
        For Each cell In .SpecialCells(XlCellType.xlCellTypeBlanks) '<--| loop through blank cells only and fill DOWNLOAD_ERROR dictionary after preliminary checking
            currentReadVariable = Trim(CStr(cell.Offset(, COL_DOWNLOAD_NUMBER - COL_DOWNLOAD_STATUS)))
            If Not (DOWNLOAD_ERROR.Exists(currentReadVariable)) Then If Not (DOWNLOAD_NOERROR.Exists(currentReadVariable)) Then DOWNLOAD_NOERROR.Add currentReadVariable, download_status
        Next cell

    End With
End With
相关问题