|
楼主 |
发表于 2009-6-6 14:52
|
显示全部楼层
原帖由 linyancheng 于 2009-6-5 21:45 发表
請教,突然發現,如果執行到一半,取消輸入時,會出現偵錯,要如何才能避免掉?
改成這樣就能避免掉了:
Sub 儲存格尾加文字()
Dim 儲存格 As Range
Dim 選擇 As Range
Dim 文字 As String
Dim 分隔 As String
Dim 內容 As String
Dim 回答 As Integer
Dim 已選 As String
' Dim 數量 As Long
' Dim i As Long
' Dim 欄數 As Integer
' Dim 起欄 As Integer
On Error GoTo line
已選 = Selection.Address
Set 選擇 = Application.InputBox("選擇儲存格範圍以加入文字:", "選擇儲存格", 已選, , , , , 8)
選擇.Select
' 數量 = Selection.Count
' 欄數 = Selection.Columns.Count
' 起欄 = ActiveCell.Column
文字 = Application.InputBox("加入儲存格尾的文字或數字為:", "輸入文字", , , , , , 2)
If 文字 = "False" Then
Exit Sub
End If
分隔 = Application.InputBox("加入分隔符號為", "輸入分隔符號", "-", , , , , 2)
If 分隔 = "False" Then
分隔 = ""
回答 = MsgBox("要取消嗎?" & Chr(13) & "若選擇是,將結束程序。" & Chr(13) & "若選擇否,將省略分隔符號繼續。", vbYesNo + vbDefaultButton2 + vbQuestion, "請注意")
If 回答 = vbYes Then
Exit Sub
End If
End If
For Each 儲存格 In Selection
內容 = 儲存格.Value
儲存格.Value = 內容 & 分隔 & 文字
Next
' For i = 1 To 數量
' 內容 = ActiveCell.Value
' ActiveCell.Value = 內容 & 分隔 & 文字
' If ActiveCell.Column = 起欄 + 欄數 - 1 Then
' ActiveCell.Offset(1, 1 - 欄數).Select
' Else
' ActiveCell.Offset(0, 1).Select
' End If
' Next i
line:
End Sub
Sub 儲存格首加文字()
Dim 儲存格 As Range
Dim 選擇 As Range
Dim 文字 As String
Dim 分隔 As String
Dim 內容 As String
Dim 回答 As Integer
Dim 已選 As String
' Dim 數量 As Long
' Dim i As Long
' Dim 欄數 As Integer
' Dim 起欄 As Integer
On Error GoTo line
已選 = Selection.Address
Set 選擇 = Application.InputBox("選擇儲存格範圍以加入文字:", "選擇儲存格", 已選, , , , , 8)
選擇.Select
' 數量 = Selection.Count
' 欄數 = Selection.Columns.Count
' 起欄 = ActiveCell.Column
文字 = Application.InputBox("加入儲存格尾的文字或數字為:", "輸入文字", , , , , , 2)
If 文字 = "False" Then
Exit Sub
End If
分隔 = Application.InputBox("加入分隔符號為", "輸入分隔符號", "-", , , , , 2)
If 分隔 = "False" Then
分隔 = ""
回答 = MsgBox("要取消嗎?" & Chr(13) & "若選擇是,將結束程序。" & Chr(13) & "若選擇否,將省略分隔符號繼續。", vbYesNo + vbDefaultButton2 + vbQuestion, "請注意")
If 回答 = vbYes Then
Exit Sub
End If
End If
For Each 儲存格 In Selection
內容 = 儲存格.Value
儲存格.Value = 文字 & 分隔 & 內容
Next
' For i = 1 To 數量
' 內容 = ActiveCell.Value
' ActiveCell.Value = 內容 & 分隔 & 文字
' If ActiveCell.Column = 起欄 + 欄數 - 1 Then
' ActiveCell.Offset(1, 1 - 欄數).Select
' Else
' ActiveCell.Offset(0, 1).Select
' End If
' Next i
line:
End Sub
Sub 儲存格尾加數列()
Dim 選擇 As Range
Dim 起數 As Long
Dim 間距 As Long
Dim 分隔 As String
Dim 內容 As String
Dim 回答 As Integer
Dim 已選 As String
Dim 數量 As Long
Dim i As Long
Dim 欄數 As Integer
Dim 起欄 As Integer
On Error GoTo line
已選 = Selection.Address
Set 選擇 = Application.InputBox("選擇儲存格範圍以加入文字:", "選擇儲存格", 已選, , , , , 8)
選擇.Select
數量 = Selection.Count
欄數 = Selection.Columns.Count
起欄 = ActiveCell.Column
起數 = Application.InputBox("加入起數為", "輸入數字", 1, , , , , 1)
If 起數 = 0 Then
MsgBox "起數為0"
End If
間距 = Application.InputBox("加入間距為", "輸入數字", 1, , , , , 1)
If 間距 = 0 Then
MsgBox "間距為0"
End If
分隔 = Application.InputBox("加入分隔符號為", "輸入分隔符號", "-", , , , , 2)
If 分隔 = "False" Then
分隔 = ""
回答 = MsgBox("要取消嗎?" & Chr(13) & "若選擇是,將結束程序。" & Chr(13) & "若選擇否,將省略分隔符號繼續。", vbYesNo + vbDefaultButton2 + vbQuestion, "請注意")
If 回答 = vbYes Then
Exit Sub
End If
End If
For i = 1 To 數量
內容 = ActiveCell.Value
ActiveCell.Value = 內容 & 分隔 & 起數 + 間距 * (i - 1)
If ActiveCell.Column = 起欄 + 欄數 - 1 Then
ActiveCell.Offset(1, 1 - 欄數).Select
Else
ActiveCell.Offset(0, 1).Select
End If
Next i
line:
End Sub
Sub 儲存格首加數列()
Dim 選擇 As Range
Dim 起數 As Long
Dim 間距 As Long
Dim 分隔 As String
Dim 內容 As String
Dim 回答 As Integer
Dim 已選 As String
Dim 數量 As Long
Dim i As Long
Dim 欄數 As Integer
Dim 起欄 As Integer
On Error GoTo line
已選 = Selection.Address
Set 選擇 = Application.InputBox("選擇儲存格範圍以加入文字:", "選擇儲存格", 已選, , , , , 8)
選擇.Select
數量 = Selection.Count
欄數 = Selection.Columns.Count
起欄 = ActiveCell.Column
起數 = Application.InputBox("加入起數為", "輸入數字", 1, , , , , 1)
If 起數 = 0 Then
MsgBox "起數為0"
End If
間距 = Application.InputBox("加入間距為", "輸入數字", 1, , , , , 1)
If 間距 = 0 Then
MsgBox "間距為0"
End If
分隔 = Application.InputBox("加入分隔符號為", "輸入分隔符號", "-", , , , , 2)
If 分隔 = "False" Then
分隔 = ""
回答 = MsgBox("要取消嗎?" & Chr(13) & "若選擇是,將結束程序。" & Chr(13) & "若選擇否,將省略分隔符號繼續。", vbYesNo + vbDefaultButton2 + vbQuestion, "請注意")
If 回答 = vbYes Then
Exit Sub
End If
End If
For i = 1 To 數量
內容 = ActiveCell.Value
ActiveCell.Value = 起數 + 間距 * (i - 1) & 分隔 & 內容
If ActiveCell.Column = 起欄 + 欄數 - 1 Then
ActiveCell.Offset(1, 1 - 欄數).Select
Else
ActiveCell.Offset(0, 1).Select
End If
Next i
line:
End Sub |
|