PDA

View Full Version : Is this code correct?



jsc0624
04-17-2007, 05:54 PM
Is this code correct? What's wrong with this code?

I want to delete a certain column if it is totally blank...

let's say column b is empty then the program will automatically delete it.
But then if it contains 1 or more data, then the program will prompt a msgbox that says, "Do you want to delete?" If the user decided to delete the column and press ok the column will be deleted but if the user press the cancel button, the program will do nothing.

Here is the code:


MsgBox "Are you sure you want to delete this column?", vbOKCancel
If vbOKCancel = True Then
theWhole = Application.CountA(ActiveSheet.Range("b:b"))
Else
'do nothing
End If

Paul_Hossler
04-17-2007, 06:20 PM
Use it as a function and test the return


If MsgBox("Are you sure you want to delete this column?", vbOKCancel) = vbCancel Then
'do it
Else
'do nothing
End If

mudraker
04-17-2007, 07:51 PM
Paul I think you will find that your post shoul have been this



If MsgBox("Are you sure you want to delete this column?", vbOKCancel) = vbOK Then
'do it
Else
'do nothing
End If

Bob Phillips
04-18-2007, 01:31 AM
If Application.CountBlank(Columns("B:B")) = Rows.Count Then
Columns("B:B").Delete
Else
If MsgBox("Are you sure you want to delete this column?", vbOKCancel) = vbOK Then
Columns("B:B").Delete
End If
End If

jammer6_9
04-18-2007, 05:27 AM
:hi: Kaibigan


If Application.CountBlank(Columns("B:B")) = Rows.Count Then
inti = MsgBox("Are you sure you want to delete this column", vbYesNo + vbQuestion)
If inti = 6 Then
Columns("B:B").delete
Else
'Do nothing

End If
End If

mdmackillop
04-18-2007, 06:09 AM
You could also use the selection, to keep things flexible (based on XLD's code)

Sub DelCols()
If Application.CountA(Selection.EntireColumn) = 0 Then
Selection.entirecolumn.Columns.Delete
Else
If MsgBox("Are you sure you want to delete this column?", vbOKCancel) = vbOK Then
Selection.EntireColumn.Delete
End If
End If
End Sub

Binoy
04-18-2007, 12:55 PM
bump...

mdmackillop
04-18-2007, 01:06 PM
bump... That's one strike. I think that's all you'll get.

Paul_Hossler
04-18-2007, 01:56 PM
Paul I think you will find that your post should have been this

Uh -- It was a test???:whistle:

Oh, OK - I was in a hurry





If MsgBox("Are you sure you want to delete this column?", vbOKCancel) = vbOK Then
'do it
Else
'do nothing
End If