宏提供了格式化所选文本的选项,如果首先选择了一些单元格,它会完美地完成。
然而,错误处理不起作用,我不知道原因:如果在执行宏时没有选择任何内容,它会格式化整个工作表,而不是显示请求进行选择的错误消息。任何想法为什么这不起作用?
来自我的UserForm的代码(" UserForm1"):
Private Sub OKButton_Click()
Dim WorkRange As Range
Dim cell As Range
On Error Resume Next
Set WorkRange = Selection.SpecialCells _
(xlCellTypeConstants, xlCellTypeConstants)
If OptionUpper Then
For Each cell In WorkRange
cell.Value = UCase(cell.Value)
Next cell
End If
' code for the other options...
Unload UserForm1
End Sub
调用宏的代码(" Module1"):
Sub ChangeCase()
If TypeName(Selection) = "Range" Then
UserForm1.Show
Else
MsgBox "Select an area first.", vbCritical
End If
End Sub
我使用的是MS Excel 2010.(希望我没有忘记任何相关信息。)
答案 0 :(得分:0)
您可以将userform代码更改为:
Private Sub OKButton_Click()
Dim WorkRange As Range
Dim cell As Range
' If Selection.Cells.Count > 1 then (I corrected this to the line below, then it worked!
If Selection.Cells.Count = 1 then
If Msgbox("Only one cell selected - do you want to format the whole sheet?", vbyesno) = vbNo then Exit Sub
End If
On Error Resume Next
Set WorkRange = Selection.SpecialCells _
(xlCellTypeConstants, xlCellTypeConstants)
If OptionUpper Then
For Each cell In WorkRange
cell.Value = UCase(cell.Value)
Next cell
End If
' code for the other options...
Unload Me
End Sub
答案 1 :(得分:0)
如果没有选择任何内容,那就是更好的解决方案。
Public Sub IfNoSelection()
Application.ScreenUpdating = False
'Activate your Sheet
Sheets("Name Of Sheet Here").Select
'Select your range without selecting the header (column D)
Range(Cells(2, 4), Cells(Rows.Count, 4)).Select
'This Line Checks if what is selected is selected.
If WorksheetFunction.CountA(Selection) = 0 Then
Else
'enter code here
End If
Application.ScreenUpdating = True
End Sub