2016-08-19 169 views
-1

我必须将列(数字)的值添加到基于列(状态)的字典中。 如果列(状态)有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 
+0

问题没有共享通常会很快关闭代码... –

+0

添加的问题 –

+0

代码@Vasily你能解释一下作为进一步我是新来的VBA? –

回答

0

我认为你有问题用的逻辑,既包含类型的字典一样的价值,因为当你插入值到noerror你不检查你已经插入此数值为error词典,反之亦然,所以你修改代码如下,我想这将解决这一问题:

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 

,也就像我在下面的发表评论previosly提到的,可以对一些问题进行比较,在比较一些文本与实例号,也许这不是你目前的情况,但你应该考虑到“区分大小写”和变量这些任务的类型。要删除区分大小写,您可以使用标准的方法象下面这样:

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

我希望这会有所帮助。

+0

在我看来,你的代码并没有考虑到OP的规范:“如果E存在于数字(不管其他数字的条目是否为空),那么它应该在error_dictionary”_中,因为如果首先遇到给定的_number_ _not-“E”_它被添加到'DOWNLOAD_NOERROR'字典中,并且在那里它只停留在 – user3598756

0

,你可以用“E”,并填写DOWNLOAD_ERROR字典,然后第一处理数据处理与初步其他数据检查尚未在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 
+0

@SalmanBaqri,你通过了吗? – user3598756

相关问题