PLEASE TAKE A LOOK AT THIS CODE- MY BOSS IS AFTER MY HEAD FOR THIS FORM


Posted by Faye on November 14, 2001 5:28 AM

Hi,

I hope this is not too much to look at!!!!
I posted this earlier and no one has responded, I am going on Holdiday on Friday and this has to be handed over to my Boss before I go. I just can't seem to get this done and I am running out of time.

What I want to achieve here is that; when the message box asks if I want to continue and I say no,I want the worksheet to be saved and quit Excel. I have gotten it to work for my other workbooks but I don't know what is happening with this one.

If response = vbyes then
Textbox1.text=""
Textbox2.Text=""

Textbox1.Setfocus

Else
Unload Me.

Dim SelText As String
Dim i As Integer

Const PerilsCol = 19 'Perils in colum S
SelText = " ' "


With PerilsLbx
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
SelText = Right(SelText & ListBox1.List(i) & " ", Len(SelText & ListBox1.List(i) & " ") - 1)
End If
Next i

End With
'put value in next available cell in perils column
Cells(65536, PerilsCol).End(xlUp).Offset(1).Value = SelText
End If

End Sub

I substituted the "unload me" bit with code to save the workbook and exit excel and I get the error message (Runtime error 13 type mismatch) and it also shows the screen asking if I want to save the document, which I don't get with the other workbooks, but when I save the workbook and reopen it, the column for the listbox is empty. I even went as far as putting - If vbno then (execute code) and it still does not work. Could someone take a look at the above code and see if they could tell me what is wrong.
I find that when I get the error message it displays the code I have in Sheet1, but it does not highlight anything so that I can figure out what is wrong. Code for sheet1 is below.


Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCells As Range
Application.EnableEvents = False
If Not Intersect(Target, Columns("A:Z")) Is Nothing Then
For Each rCells In Intersect(Target, Columns("A:Z"))
rCells = UCase(rCells.Text)
Next
End If
Application.EnableEvents = True
End Sub


Thanks,
Faye

Posted by Dank on November 14, 2001 5:40 AM

With PerilsLbx For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) Then SelText = Right(SelText & ListBox1.List(i) & " ", Len(SelText & ListBox1.List(i) & " ") - 1) End If Next i End With 'put value in next available cell in perils column Cells(65536, PerilsCol).End(xlUp).Offset(1).Value = SelText End If

Not entirely sure what you're asking for. Do you want the correct code to save the workbook and close Excel? If so, you should substitute Unload Me for:-

Activeworkbook.Save
Activeworkbook.Close False
Application.Quit

Is this what you're after?

Daniel.

Posted by Juan Pablo on November 14, 2001 5:44 AM

This code worked for me

Sub ExitwithNoSave()
ThisWorkbook.Saved = True
Application.Quit
End Sub

You could replace
ThisWorkbook.Saved = True

with
Application.DisplayAlerts = False
ThisWorkbook.Close
Application.DisplayAlerts = True

but, that's three lines, and i only use one...

Juan Pablo Hi, With PerilsLbx For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) Then SelText = Right(SelText & ListBox1.List(i) & " ", Len(SelText & ListBox1.List(i) & " ") - 1) End If Next i End With 'put value in next available cell in perils column Cells(65536, PerilsCol).End(xlUp).Offset(1).Value = SelText End If

Posted by Faye on November 14, 2001 6:33 AM

That is exactly what I want to do, but whenever I use anything except Unload Me the code that I have for the List box, which is directly under unload me, stops working.



Posted by Juan Pablo on November 14, 2001 7:11 AM

I don't understand why, if you're trying to quit the form, you're dimensioning and doing things AFTER the Unload Me ? i don't know if this could be part of the problem, but it's very strange any way.

Juan Pablo