Consulting

Results 1 to 4 of 4

Thread: Solved: Why doesn't this work?

  1. #1

    Solved: Why doesn't this work?

    Hi,

    I have written the following code to take a name from a user then delete a worksheet with the relevant name.

    After it deletes the sheet i want it to search down a sheet called "data" and then remove the corresponding name for the list.

    My loop proecdure works fine on its own but when combined as below it throws up errors namely "400"

    [VBA]
    Sub Removestaffmember()
    Dim Staff As String
    Dim Answer As String

    Staff = InputBox("Please Enter The Name Of The Staff Member You Want To Remove", " Remove Staff Member")

    Answer = MsgBox("Deleting will permanently remove the record, do you want to continue?", vbOKCancel)
    Application.DisplayAlerts = False

    If Answer <> vbOK Then Exit Sub

    ThisWorkbook.Sheets("" & Staff).delete

    ThisWorkbook.Sheets("Data").Activate
    Range("A1").Select
    Do While ActiveCell <> ("" & Staff)
    ActiveCell.Offset(1, 0).Activate
    Loop
    ActiveCell.delete

    Application.DisplayAlerts = True
    End Sub
    [/VBA]
    Any help would be much appreciated as to what im doing wrong

    Mark

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]
    Option Explicit
    Sub Removestaffmember()
    Dim Staff As String
    Dim Answer As Long
    Staff = InputBox("Please Enter The Name Of The Staff Member You Want To Remove", " Remove Staff Member")
    Answer = MsgBox("Deleting will permanently remove the record, do you want to continue?", vbOKCancel)
    Application.DisplayAlerts = False
    If Answer <> vbOK Then Exit Sub
    ThisWorkbook.Sheets(Staff).Delete
    ThisWorkbook.Sheets("Data").Columns(1).Find(Staff).Delete
    Application.DisplayAlerts = True
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try

    ActiveCell.ClearContents

    instead of

    ActiveCell.delete
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Thanks a million MD that was perfect, much appreciated!

Posting Permissions

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