ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Why my selection is lost with MsgBox? (https://www.excelbanter.com/excel-programming/336742-why-my-selection-lost-msgbox.html)

MLevesque

Why my selection is lost with MsgBox?
 
I have a rows selection to delete, and using a msgbox I want to ask user to
do ok or cancel, but when the msgbox is popup, my rows selection is lost :o(
How could I avoid that problem? Thank you!

Tim Williams

Why my selection is lost with MsgBox?
 
Get a reference to the selection before you pop up the messagebox.

dim rngSel as range
set rngSel = selection
'do stuff which might change selection
'operate on rngSel

However, msgbox doesn't change the selection for me, so maybe you're
doing something else in your code?

Tim

"MLevesque" wrote in message
...
I have a rows selection to delete, and using a msgbox I want to ask
user to
do ok or cancel, but when the msgbox is popup, my rows selection is
lost :o(
How could I avoid that problem? Thank you!




Roy Wagner

Why my selection is lost with MsgBox?
 
I agree. Below is my experiment. It stays highlighted until I send the right
cursor keystroke whether I delete the row or not. Watch that you are not
unintentionally releasing the selection somehow.

Roy

Dim x As Integer
Rows(7).Select
x = MsgBox("Really delete this row? Press OK to delete or Cancel to abort.",
vbOKCancel + vbExclamation, "Delete Row?")
If x = vbOK Then
Rows(7).Delete
End If
SendKeys "{RIGHT}" 'to relase the selection




--
(delete .nospam)




"Tim Williams" wrote:

Get a reference to the selection before you pop up the messagebox.

dim rngSel as range
set rngSel = selection
'do stuff which might change selection
'operate on rngSel

However, msgbox doesn't change the selection for me, so maybe you're
doing something else in your code?

Tim

"MLevesque" wrote in message
...
I have a rows selection to delete, and using a msgbox I want to ask
user to
do ok or cancel, but when the msgbox is popup, my rows selection is
lost :o(
How could I avoid that problem? Thank you!





MLevesque

Why my selection is lost with MsgBox?
 
Thank you, but it does not work for me. My code is the following...
if msgbox("Do you really want to delete selected rows?"...) = vbOk then
for each rng in selection
if rng.row = 10 and rng.row <= 20 then
rng.entirerow.delete
endif
next rng
endif

"Tim Williams" wrote:

Get a reference to the selection before you pop up the messagebox.

dim rngSel as range
set rngSel = selection
'do stuff which might change selection
'operate on rngSel

However, msgbox doesn't change the selection for me, so maybe you're
doing something else in your code?

Tim

"MLevesque" wrote in message
...
I have a rows selection to delete, and using a msgbox I want to ask
user to
do ok or cancel, but when the msgbox is popup, my rows selection is
lost :o(
How could I avoid that problem? Thank you!





Tim Williams

Why my selection is lost with MsgBox?
 
Try this:

dim x
if msgbox("Do you really want to delete selected rows?"...) = vbOk
then
Selection.EntireRow.Delete
end if

Problem with your approach is you can't delete rows without affecting
the selection - that causes problems with your loop.

Tim.


"MLevesque" wrote in message
...
Thank you, but it does not work for me. My code is the following...
if msgbox("Do you really want to delete selected rows?"...) = vbOk
then
for each rng in selection
if rng.row = 10 and rng.row <= 20 then
rng.entirerow.delete
endif
next rng
endif

"Tim Williams" wrote:

Get a reference to the selection before you pop up the messagebox.

dim rngSel as range
set rngSel = selection
'do stuff which might change selection
'operate on rngSel

However, msgbox doesn't change the selection for me, so maybe
you're
doing something else in your code?

Tim

"MLevesque" wrote in message
...
I have a rows selection to delete, and using a msgbox I want to
ask
user to
do ok or cancel, but when the msgbox is popup, my rows selection
is
lost :o(
How could I avoid that problem? Thank you!







MLevesque

Why my selection is lost with MsgBox?
 
Everything is ok now. Sorry I did a mistake!
Thank you!!

"Tim Williams" wrote:

Get a reference to the selection before you pop up the messagebox.

dim rngSel as range
set rngSel = selection
'do stuff which might change selection
'operate on rngSel

However, msgbox doesn't change the selection for me, so maybe you're
doing something else in your code?

Tim

"MLevesque" wrote in message
...
I have a rows selection to delete, and using a msgbox I want to ask
user to
do ok or cancel, but when the msgbox is popup, my rows selection is
lost :o(
How could I avoid that problem? Thank you!





MLevesque

Why my selection is lost with MsgBox?
 
I know but I need to verify each row before deletion. I cannot do only a
Selection.EntireRow.Delete


"Tim Williams" wrote:

Try this:

dim x
if msgbox("Do you really want to delete selected rows?"...) = vbOk
then
Selection.EntireRow.Delete
end if

Problem with your approach is you can't delete rows without affecting
the selection - that causes problems with your loop.

Tim.


"MLevesque" wrote in message
...
Thank you, but it does not work for me. My code is the following...
if msgbox("Do you really want to delete selected rows?"...) = vbOk
then
for each rng in selection
if rng.row = 10 and rng.row <= 20 then
rng.entirerow.delete
endif
next rng
endif

"Tim Williams" wrote:

Get a reference to the selection before you pop up the messagebox.

dim rngSel as range
set rngSel = selection
'do stuff which might change selection
'operate on rngSel

However, msgbox doesn't change the selection for me, so maybe
you're
doing something else in your code?

Tim

"MLevesque" wrote in message
...
I have a rows selection to delete, and using a msgbox I want to
ask
user to
do ok or cancel, but when the msgbox is popup, my rows selection
is
lost :o(
How could I avoid that problem? Thank you!








All times are GMT +1. The time now is 09:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com