te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>VBA ActiveX ComboBox causing excel to crash - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

VBA ActiveX ComboBox causing excel to crash - Stack Overflow

programmeradmin4浏览0评论

I have a table within a worksheet that is designed to help Career Managers plot the future roles that employees will hold. It has columns to record each employee’s current position details and the start and end date of that position + the details and start and end dates of up to three subsequent future positions. There is a ComboBox called CmboPERS at the top of the worksheet, which allows the user to select an employee from the table. The user can then click a command button called "Open Form" to open a UserForm that populates with the employee's details and allows the user to create a plan for the employee's future positions.

The user can enter future position plans within this form that will then be added to the table in the worksheet once the update record button is clicked.

To select a position to add to the employee's career plan the user clicks the ‘Select’ button. This opens a sub UserForm to help them find the position within the anization through a series of dependent Comboboxes. The ComboBoxes within this sub UserForm are populated from a different table on a separate worksheet within the workbook.

This all works perfectly for the first employee selected. CmboPERS works, and both user forms run without issue and the employee's career plans are updated in the relevant columns within the table.

However, when the user tries to interact with CmboPERS again to select another employee, excel crashes while trying to run the ComboBox Event for CmboPERS. The event (e.g. dropbuttonclick) initiates, and through the debugger I have been able to confirm that it seems to run through the code, but when it hits the end sub line the programme just crashes.

After a bunch of testing I have determined that this does not occur if the user does not interact with the comboboxes in the sub userform. i.e. the user can amend the start and end dates within the Employee user form and then click update record and will be able to then interact with CmboPERS to select another employee with no issue. It is something about interacting with the cmboboxes in the sub user form that is preventing CmboPERS from working.

The code for CmboPERS is

Option Explicit
Dim Dict As Object
Dim pList As Variant
Private IsChanged As Boolean
 
Private Sub CmboPERS_DropButtonClick()
 
    pList = ThisWorkbook.Sheets("Posting plot").ListObjects("PERS").ListColumns("PER").DataBodyRange.Value
    Set Dict = CreateObject("scripting.dictionary")
    Dict.CompareMode = vbTextCompare
    Dim i As Long
 
    For i = LBound(pList) To UBound(pList)
        If Len(pList(i, 1)) > 7 Then
        Dict(pList(i, 1)) = Empty
        End If
    Next
        CmboPERS.List = Dict.Keys
        Dict.RemoveAll
        CmboPERS.ListRows = Application.WorksheetFunction.Min(6, CmboPERS.ListCount)
End Sub
  
Private Sub CmboPERS_Change()
 
    Dim i As Long
    Me.CmboPERS.ListRows = Application.WorksheetFunction.Min(6, Me.CmboPERS.ListCount)
 
    If Not IsChanged Then
        With Me.CmboPERS
            If .Value <> "" Then
                .ListRows = Application.WorksheetFunction.Min(6, .ListCount)
                .DropDown
                If Len(.Text) Then
                    For i = .ListCount - 1 To 0 Step -1
                        If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then
                            .RemoveItem i
                        End If
                    Next
 
                End If
            End If
        End With
 
    End If
 
End Sub

'code to manage the user typing text into CmboPERS
 
Private Sub CmboPERS_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
 
    IsChanged = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)
    If (KeyCode = vbKeyReturn Or KeyCode = vbKeyBack) Then
        Dim i As Long
 
        For i = LBound(pList) To UBound(pList)
            If Len(pList(i, 1)) > 7 Then
                Dict(pList(i, 1)) = Empty
            End If
        Next
        CmboPERS.List = Dict.Keys
 
        Dict.RemoveAll
    End If
 
End Sub
 

Parts of the code for the Sub User Form "SelPost" are below. Note that just interacting with the first ComboBox - "ComboBox1" and then clicking the cancel button on the form causes CmboPERS to stop functioning and excel to crash.

Option Explicit
Dim kList As Variant
Dim Post As Variant
Private IsArrow As Boolean
Dim d As Object

Private Sub CmdBCancel_Click()
   d.RemoveAll
   ComboBox1.Clear
   ComboBox2.Clear
   ComboBox3.Clear
   ComboBox4.Clear
   ComboBox5.Clear
   ComboBox6.Clear
   Unload SelPost
End Sub

Private Sub UserForm_Activate()
   Dim WS As Worksheet
   Set WS = ThisWorkbook.Sheets("ALL POSN")
   Dim Tbl As ListObject
   Set Tbl = WS.ListObjects("ALLPOSNS")
   kList = Tbl.DataBodyRange.Value
   Set d = CreateObject("scripting.dictionary")
   
   d.CompareMode = vbTextCompare

   Dim i As Long

   For i = LBound(kList) To UBound(kList)
      d(kList(i, 8)) = Empty
   Next

   ComboBox1.List = d.Keys
   d.RemoveAll
End Sub

Private Sub ComboBox1_Change()

   Dim i As Long

   If Not IsArrow Then
      With ComboBox1
        .ListRows = Application.WorksheetFunction.Min(4, .ListCount)
        .DropDown
        If Len(.Text) Then
          For i = .ListCount - 1 To 0 Step -1
            If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then .RemoveItem i
          Next
          .DropDown
        End If
     End With
   End If

End Sub

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
IsArrow = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)

   If (KeyCode = vbKeyReturn Or KeyCode = vbKeyBack) Then
      Dim i As Long
      For i = LBound(kList) To UBound(kList)
         d(kList(i, 8)) = Empty
      Next
      ComboBox1.List = d.Keys
      If Not ComboBox1.ListIndex = -1 Then
          Frame2.Visible = True
      End If
      d.RemoveAll
    End If
End Sub



I have a table within a worksheet that is designed to help Career Managers plot the future roles that employees will hold. It has columns to record each employee’s current position details and the start and end date of that position + the details and start and end dates of up to three subsequent future positions. There is a ComboBox called CmboPERS at the top of the worksheet, which allows the user to select an employee from the table. The user can then click a command button called "Open Form" to open a UserForm that populates with the employee's details and allows the user to create a plan for the employee's future positions.

The user can enter future position plans within this form that will then be added to the table in the worksheet once the update record button is clicked.

To select a position to add to the employee's career plan the user clicks the ‘Select’ button. This opens a sub UserForm to help them find the position within the anization through a series of dependent Comboboxes. The ComboBoxes within this sub UserForm are populated from a different table on a separate worksheet within the workbook.

This all works perfectly for the first employee selected. CmboPERS works, and both user forms run without issue and the employee's career plans are updated in the relevant columns within the table.

However, when the user tries to interact with CmboPERS again to select another employee, excel crashes while trying to run the ComboBox Event for CmboPERS. The event (e.g. dropbuttonclick) initiates, and through the debugger I have been able to confirm that it seems to run through the code, but when it hits the end sub line the programme just crashes.

After a bunch of testing I have determined that this does not occur if the user does not interact with the comboboxes in the sub userform. i.e. the user can amend the start and end dates within the Employee user form and then click update record and will be able to then interact with CmboPERS to select another employee with no issue. It is something about interacting with the cmboboxes in the sub user form that is preventing CmboPERS from working.

The code for CmboPERS is

Option Explicit
Dim Dict As Object
Dim pList As Variant
Private IsChanged As Boolean
 
Private Sub CmboPERS_DropButtonClick()
 
    pList = ThisWorkbook.Sheets("Posting plot").ListObjects("PERS").ListColumns("PER").DataBodyRange.Value
    Set Dict = CreateObject("scripting.dictionary")
    Dict.CompareMode = vbTextCompare
    Dim i As Long
 
    For i = LBound(pList) To UBound(pList)
        If Len(pList(i, 1)) > 7 Then
        Dict(pList(i, 1)) = Empty
        End If
    Next
        CmboPERS.List = Dict.Keys
        Dict.RemoveAll
        CmboPERS.ListRows = Application.WorksheetFunction.Min(6, CmboPERS.ListCount)
End Sub
  
Private Sub CmboPERS_Change()
 
    Dim i As Long
    Me.CmboPERS.ListRows = Application.WorksheetFunction.Min(6, Me.CmboPERS.ListCount)
 
    If Not IsChanged Then
        With Me.CmboPERS
            If .Value <> "" Then
                .ListRows = Application.WorksheetFunction.Min(6, .ListCount)
                .DropDown
                If Len(.Text) Then
                    For i = .ListCount - 1 To 0 Step -1
                        If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then
                            .RemoveItem i
                        End If
                    Next
 
                End If
            End If
        End With
 
    End If
 
End Sub

'code to manage the user typing text into CmboPERS
 
Private Sub CmboPERS_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
 
    IsChanged = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)
    If (KeyCode = vbKeyReturn Or KeyCode = vbKeyBack) Then
        Dim i As Long
 
        For i = LBound(pList) To UBound(pList)
            If Len(pList(i, 1)) > 7 Then
                Dict(pList(i, 1)) = Empty
            End If
        Next
        CmboPERS.List = Dict.Keys
 
        Dict.RemoveAll
    End If
 
End Sub
 

Parts of the code for the Sub User Form "SelPost" are below. Note that just interacting with the first ComboBox - "ComboBox1" and then clicking the cancel button on the form causes CmboPERS to stop functioning and excel to crash.

Option Explicit
Dim kList As Variant
Dim Post As Variant
Private IsArrow As Boolean
Dim d As Object

Private Sub CmdBCancel_Click()
   d.RemoveAll
   ComboBox1.Clear
   ComboBox2.Clear
   ComboBox3.Clear
   ComboBox4.Clear
   ComboBox5.Clear
   ComboBox6.Clear
   Unload SelPost
End Sub

Private Sub UserForm_Activate()
   Dim WS As Worksheet
   Set WS = ThisWorkbook.Sheets("ALL POSN")
   Dim Tbl As ListObject
   Set Tbl = WS.ListObjects("ALLPOSNS")
   kList = Tbl.DataBodyRange.Value
   Set d = CreateObject("scripting.dictionary")
   
   d.CompareMode = vbTextCompare

   Dim i As Long

   For i = LBound(kList) To UBound(kList)
      d(kList(i, 8)) = Empty
   Next

   ComboBox1.List = d.Keys
   d.RemoveAll
End Sub

Private Sub ComboBox1_Change()

   Dim i As Long

   If Not IsArrow Then
      With ComboBox1
        .ListRows = Application.WorksheetFunction.Min(4, .ListCount)
        .DropDown
        If Len(.Text) Then
          For i = .ListCount - 1 To 0 Step -1
            If InStr(1, .List(i), .Text, vbTextCompare) = 0 Then .RemoveItem i
          Next
          .DropDown
        End If
     End With
   End If

End Sub

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
IsArrow = (KeyCode = vbKeyUp) Or (KeyCode = vbKeyDown)

   If (KeyCode = vbKeyReturn Or KeyCode = vbKeyBack) Then
      Dim i As Long
      For i = LBound(kList) To UBound(kList)
         d(kList(i, 8)) = Empty
      Next
      ComboBox1.List = d.Keys
      If Not ComboBox1.ListIndex = -1 Then
          Frame2.Visible = True
      End If
      d.RemoveAll
    End If
End Sub



Share Improve this question edited 2 days ago Chely Jackson asked Feb 18 at 4:04 Chely JacksonChely Jackson 511 silver badge6 bronze badges 2
  • 1 I think the code you post is far too long, try to reduce it the the necessary parts. Then, some things are not clear: What is RecordForm: Is this an instance of the child form, or is this the Form Object name and you are using the default instance? And (in the second code), what is SelPost? If I where you, I would get rid of the infamous Unload command (that destroys the form object) and instead use the Hide-method of the form. – FunThomas Commented 2 days ago
  • Hi @FunThomas, I have reduced the amount of code posted. :-) RecordForm is the name of the first UserForm that is opened when the user clicks the 'Open Form' command button. SelPost is the sub UserForm that opens when the user clicks one of the 'Select' command buttons within RecordForm. Originally I was using Hide but for some reason that caused a dropdown image of ComboBox2"" in **SelPost to appear on the worksheet after the user clicked the 'Update Record' command button in RecordForm. No idea why that was happening, but Unload seemed to fix the issue. – Chely Jackson Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

Not sure why this has solved the problem, but I deleted the code lines to clear the ComboBoxes within the sub UserForm SelPost before unloading the userForm and the issue disappeared.

If anyone can explain why this worked, please share.

发布评论

评论列表(0)

  1. 暂无评论