简体   繁体   English

列表框显示不在范围内的日期

[英]List Box shows dates that aren't in the range

I'm having trouble with my Listbox . 我的Listbox遇到问题。 When I run the following code for the first time, it always runs showing only 1 date which is 30/12/1899 . 当我第一次运行以下代码时,它将始终只显示1个日期,即30/12/1899 The range that I've specified only contains 6 dates which are 8/1/2014, 9/1/2014, 14/1/2014, 24/1/2014, 24/1/2014 and 02/02/2014 . 我指定的范围仅包含6个日期,分别是8/1 / 2014、9 / 1 / 2014、14 / 1 / 2014、24 / 1 / 2014、24 / 1/2014和02/02/2014 Once I stop the form and run it again, all the required dates show up. 一旦我停止了表单并再次运行它,所有必需的日期就会显示出来。

I've just started learning VBA on Excel so I'm still struggling to understand the concepts. 我刚刚开始在Excel上学习VBA,所以我仍在努力理解这些概念。 Is there something that I'm missing? 有什么我想念的吗? The reason for no duplicates is that I can't show the 2 dates (24/01/2014). 没有重复的原因是我无法显示2个日期(2014年1月24日)。

Private Sub UserForm_Activate()

    Dim AllCells As Range, Cell As Range
    Dim NoDupes As New Collection
    Dim i As Integer, j As Integer
    Dim Swap1, Swap2, Item
    Dim wksJobDetail As Worksheet

'The items are in A2:A7
    Set AllCells = Range("A2:A7")

'Point the variable to JobSchedule worksheet
    Set wksJobDetail = Application.Workbooks("xxxxx.xlsm").Worksheets("JobSchedule")
    wksJobDetail.Activate

'Statement ignores any errors regarding duplicates and duplicate dates aren't added
    On Error Resume Next
    For Each Cell In AllCells
        NoDupes.Add Format(CDate(Cell.Value), "dd/mm/yyyy"), _
            CStr(Format(CDate(Cell.Value), "dd/mm/yyyy"))
    Next Cell

'Add non-duplicated items into lstDate
    For Each Item In NoDupes
        JobDetail.lstDate.AddItem Item
    Next Item
End Sub

Set AllCells = Range("A2:A7") will reference the active worksheet which may or may not be wksJobDetail . Set AllCells = Range("A2:A7")将引用活动工作表,该工作表可能是wksJobDetail ,也可能不是。

The second time you run it wksJobDetail has been activated. 第二次运行它时,wksJobDetail已激活。

Try putting the Set AllCells = Range("A2:A7") statement after: 尝试在以下位置放置Set AllCells = Range("A2:A7")语句:

Set wksJobDetail = Application.Workbooks("xxxxx.xlsm").Worksheets("JobSchedule") wksJobDetail.Activate 设置wksJobDetail = Application.Workbooks(“ xxxxx.xlsm”)。Worksheets(“ JobSchedule”)wksJobDetail.Activate

I think it has something to do with how you format your data in Excel and the proper way of referencing source range. 我认为这与您如何在Excel中格式化数据以及引用源范围的正确方法有关。

Try this: First, check if the dates are correctly entered as dates in Excel like below. 尝试以下操作:首先,检查日期是否正确输入为Excel中的日期,如下所示。

在此处输入图片说明

Then make this line explicit: 然后使这一行明确:

Set AllCells = Range("A2:A7")

and change to this: 并更改为:

Set AllCells = Sheets("JobSchedule").Range("A2:A7") 

Now, run your code which I've rewritten below adding On Error Goto 0 . 现在,运行下面重写On On Goto 0的代码

Dim AllCells As Range, Cell As Range, Item
Dim NoDupes As New Collection

Set AllCells = Sheets("JobSchedule").Range("A2:A7")

On Error Resume Next '~~> Ignore Error starting here
For Each Cell In AllCells
    NoDupes.Add Format(CDate(Cell.Value), "dd/mm/yyyy"), _
        CStr(Format(CDate(Cell.Value), "dd/mm/yyyy"))
Next Cell
On Error GoTo 0 '~~> Stops ignoring error

For Each Item In NoDupes
    JobDetail.lstDate.AddItem Item
Next Item

And that should give you the result you want. 那应该给您想要的结果。 Also, I suggest to use Initialize Event instead of Activate . 另外,我建议使用Initialize Event而不是Activate
Everytime you use OERN , do not forget to use OEG0 to reset the error handling. 每次使用OERN时 ,请不要忘记使用OEG0重置错误处理。
Otherwise, you will not be able to trap other errors not related to the adding existing item in Collection . 否则,您将无法捕获与在Collection中添加现有项目无关的其他错误。

Bonus: 奖金:

Another way to do this is to use a Dictionary instead. 执行此操作的另一种方法是改用Dictionary You need to add reference to Microsoft Scripting Runtime . 您需要添加对Microsoft脚本运行时的引用。 I rewrote part of your code which will have the same effect. 我重写了部分代码,它们将具有相同的效果。 The advantage of a Dictionary is that it offers other helpful properties that you can use. 词典的优点是它提供了您可以使用的其他有用的属性。

Private Sub UserForm_Initialize()
    Dim AllCells As Range, Cell As Range
    Dim d As Dictionary

    Set AllCells = Sheets("Sheet1").Range("A2:A7")
    Set d = New Dictionary

    For Each Cell In AllCells
        d.Item(Format(CDate(Cell.Value), "dd/mm/yyyy")) = _
            CStr(Format(CDate(Cell.Value), "dd/mm/yyyy"))
    Next Cell
    JobDetail.lstDate.List = d.Keys
End Sub

As you can see, we removed one Loop by using Keys property which is an array of all unique keys. 如您所见,我们使用Keys属性删除了一个Loop,该属性是所有唯一键的数组。
I hope this somehow helps. 我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM