LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Goto misused: help to tidy Code


I have a Listbox fed by RowSource delivering data from Cols A-C. The
code below
deletes a selected row from both the ListBox and the root row on the
worksheet. It works fine except that, try as I would, it does the job
uglily. For one, I have violated one of the cardinal principles of -good
programming- by pandering to the use of GO TO in a way which makes the
code poorly structured.

Could someone kindly have a quick study and restructure the logical
flow without having to loop backwards the way I did? Many thanks.

[PS: I would also love the code to allow for multiple row selection and
resultant block deletions, if possible].


David.

Private Sub CmdDelete_Click()

Restart:
If ListBox1.ListIndex = -1 Then 'no selection
ans = MsgBox("Select item to delete", vbYesNo + vbDefaultButton2)
If ans = vbYes Then
ListBox1.Selected(0) = True 'select 1st item for a start
GoTo Skip
Else
ListBox1.ListIndex = -1
Exit Sub
End If
End If

Skip:
If ListBox1.Selected(1) =False True Then
If ListBox1.Selected(ListBox1.ListIndex) = True Then
ansx = MsgBox("Do you wish to delete selection?" & vbCrLf & "
" & ListBox1.List(ListBox1.ListIndex, 0), vbYesNo +
vbDefaultButton2 + vbInformation)
If ansx = vbNo Then Exit Sub
ActiveSheet.Cells(ListBox1.ListIndex + 1, 1).Resize(,
3).ClearContents
On Error Resume Next
ListBox1.Selected(ListBox1.ListIndex) = False
ansx = MsgBox("Do you wish to delete another?", vbYesNo +
vbDefaultButton1 + vbInformation)
If ansx = vbNo Then
GoTo Sortt
Else
GoTo Restart
End If
End If
End If


Sortt:
Columns("a:c").Sort Key1:=Range("A2"), Key2:=Range("b2"),
Key3:=Range("c2"), Header:=xlNo

ListBox1.RowSource = "a1:c" & [a65536].End(xlUp).Row

End Sub


--
davidm
------------------------------------------------------------------------
davidm's Profile: http://www.excelforum.com/member.php...o&userid=20645
View this thread: http://www.excelforum.com/showthread...hreadid=494441

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tidy up multiple find and replace code PSM[_10_] Excel Worksheet Functions 2 April 6th 09 02:00 PM
Goto code Bob Excel Discussion (Misc queries) 4 July 6th 06 05:44 PM
smart & tidy code for many checkBox_Change() Fendic[_16_] Excel Programming 3 August 14th 05 02:52 PM
Tidy Up Pete Excel Discussion (Misc queries) 4 May 9th 05 05:09 PM
Goto Command in Code JimPNicholls Excel Programming 2 September 1st 04 12:38 PM


All times are GMT +1. The time now is 01:51 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"