Closed Thread
Results 1 to 5 of 5

Using VBA to name a range and delete a value instead of named range in wkbk

Hybrid View

HeyInKy Using VBA to name a range and... 11-03-2014, 04:34 PM
Leith Ross Re: Using VBA to name a range... 11-03-2014, 06:11 PM
HeyInKy Re: Using VBA to name a range... 11-03-2014, 06:41 PM
HeyInKy Re: Using VBA to name a range... 11-06-2014, 01:28 PM
FDibbins Re: Using VBA to name a range... 11-06-2014, 04:20 PM
  1. #1
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    Using VBA to name a range and delete a value instead of named range in wkbk

    ...a continuation from this thread.

    In summary, it seems the .list property (and variants, such as .listindex) don't like it when the "list" is only a single value. So in the linked thread above, I solved my dependence on a named dynamic range in my workbook by using code to name my range, and counting the rows of the range... if rows count = 1 (which will causes an error in the .list property), then do <this> action, but if rows count > 1, then we can use .list.

    So I have an employee list in ws("Admin Menu") in column B. Column A contains a record number (1, 2, 3...) of the number of employees. The list begins on row 2 underneath a header row, and $B$2 is always "Former Employee." In my named dynamic range, the range begins on $B$3. I have a userform to delete an employee. It populates a combo box with a list of the employees, and once one is selected, first finds all matching employees in the worksheet("Data") and changes that employee's names to "Former Employee" - this way data associated with that employee is kept, but records for that employee name are not - and then deletes the employees name from the list on the ws("Admin Menu"). Here's my code:

    Private Sub UserForm_Initialize()
      
      Dim rngDelEmpList As Range
    
    On Error Resume Next
      
      With Sheets("Admin Menu")
        Set rngDelEmpList = .Range("B3", .Range("B" & Rows.Count).End(xlUp))
      If rngDelEmpList.Rows.Count = 1 Then
        Me.cbo5.AddItem rngDelEmpList
      Else
        Me.cbo5.List = rngDelEmpList.Value
      End If
      End With
    
    End Sub
    
    Private Sub DelCancel_Click()
    
      Unload Me
    
    End Sub
    
    Private Sub DelEmp_Click()
      Dim answer As VbMsgBoxResult
      Dim c As Long
    
    On Error Resume Next
    
      If cbo5.ListIndex = -1 Then
        MsgBox "No employee selected."
      Else
    
        answer = MsgBox("Are you sure you want to delete " & cbo5.Value & "?", vbYesNo + vbExclamation, "Delete Employee")
        If answer = vbYes Then
    
          With ws3
    
            Worksheets("Data").Range("EmpNameData").Replace _
                What:=cbo5.Value, Replacement:="Former Employee", _
                SearchOrder:=xlByColumns
          End With
    
          Range("EmpDelList").Cells(cbo5.ListIndex + 1, 1).Delete xlShiftUp
          Range("A" & Cells.Rows.Count).End(xlUp).Select
          Selection.ClearContents
    
          MsgBox cbo5.Value & " has been deleted."
          Unload Me
        End If
      End If
    
    End Sub

    I would like to change my routine for the DelEmp_Click() by NOT using the named range "EmpDelList" and instead naming the range with the VBA code used in the initialization (I am keeping the "EmpNameData" range - at least for now)... but the listindex property keeps tripping me up. Any help?

    Again, sorry for not being able to upload a sample file at this time!!

    -HeyInKy

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using VBA to name a range and delete a value instead of named range in wkbk

    Hello HeyInKy,

    Can you pull together a workbook that shows a before and after example of the your problem and post that?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    Re: Using VBA to name a range and delete a value instead of named range in wkbk

    Sample Project.xlsm

    Use the "Add Employee" button to add the following employees: Employee 1, Employee 2, Employee 3, and Employee 4. (These are already pre-listed on the "Data" worksheet).

    I need the "Delete Employee" to use code - not named ranges in the workbook formulas - to populate the list, and "Former Employee" should not show up either. Then once a name is selected and the delete command is executed, it should remove the selected employee from the list on the "Admin Menu" as well as the corresponding number, and then move everything up 1 row (if the deleted employee was not on the last row).

    It also - and it does this correctly now - find all matching names on the "data" worksheet and change them to "Former Employee."

  4. #4
    Forum Contributor
    Join Date
    04-14-2013
    Location
    Ky
    MS-Off Ver
    Excel 2013
    Posts
    344

    Re: Using VBA to name a range and delete a value instead of named range in wkbk

    Still trying to fix this... here's where I'm at for the delete command button:

    Private Sub DelEmp_Click()
      Dim answer As VbMsgBoxResult
      Dim c As Long
      Dim rngDelEmpList As Range
    
    On Error Resume Next
    
      If cbo5.Value = "Former Employee" Then
        MsgBox "You cannot delete a 'Former Employee.'"
      End If
    
      If cbo5.Value = "" Then
        MsgBox "No employee selected."
      Else
    
        answer = MsgBox("Are you sure you want to delete " & cbo5.Value & "?", vbYesNo + vbExclamation, "Delete Employee")
        If answer = vbYes Then
    
          With ws3
    
            Worksheets("Data").Range("EmpNameData").Replace _
                what:=cbo5.Value, Replacement:="Former Employee", _
                SearchOrder:=xlByColumns
          End With
    
      
      c = Worksheets("Administrative Menu").Range("B3").CurrentRegion.Rows.Count
      Set Found = Worksheets("Administrative Menu").Columns("B").Find(what = Me.cbo5.Value, LookIn:=xlValues, lookat:=xlWhole)
      Found.Delete (xlShiftUp)
      
          
          MsgBox cbo5.Value & " has been deleted."
          Unload Me
        End If
      End If
    
    End Sub
    Any help is greatly appreciated.

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,053

    Re: Using VBA to name a range and delete a value instead of named range in wkbk

    OP posted in Commercial Services forum, so this thread is now closed
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to delete named range with #REF?
    By m3atball in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-02-2014, 02:09 AM
  2. [SOLVED] Using code to delete a named range
    By JimDandy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-29-2013, 05:16 PM
  3. [SOLVED] How to automatically delete named range by name?
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-16-2013, 04:02 PM
  4. Delete Worksheets not on list or in named range?
    By Unpackedrope in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-09-2013, 04:12 PM
  5. [SOLVED] delete all oleobjects in named range
    By Willardio in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-24-2012, 02:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1