Sub IsMergeCell()
If Range("A1").MergeCells=True Then
Msgbox "包含合併儲存格"
Else
Msgbox"沒有包含合併儲存格"
End If
End Sub
Sub IsMergeCell()
If IsNull (Range("A1:E10").MergeCells) Then
Msgbox "包含合併儲存格"
Else
Msgbox"沒有包含合併儲存格"
End If
End Sub
使用巨集程式碼在儲存格中建立公式
在EXCEL中函數與公式無疑是實現強大功能的重要組成部份. 在某些情況下, 在VBA中使用公式能更簡單快捷地實現使用者所需要的結果. 下列為如何使用程式碼在儲存格中輸入公式的例子:
Sub UsedFormula()
with Range("A3")
If .HasFormula=True Then 'range物件的HasFormula屬性判斷出儲存格A3中有公式存在
MsgBox "A3儲存格中已有公式"
Else
.Formula="=A1&A2" '為儲存格輸入公式
.FormulaHidden=True '設定將指定儲存格中的公司隱藏(工作表處於保護狀態時才生效)
End if
End with
End Sub
Sub UsedFormula()
with Range("A3")
If .HasFormula=True Then 'range物件的HasFormula屬性判斷出儲存格A3中有公式存在
MsgBox "A3儲存格中已有公式"
Else
.Formula="=A1&A2" '為儲存格輸入公式
.FormulaHidden=True '設定將指定儲存格中的公司隱藏(工作表處於保護狀態時才生效)
End if
End with
End Sub
TARGET.COLUMN使用一簡例
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo ERRORFREE
'利用TARGET.COLUMN功能,讓游標到達F欄時,自動跳到下一ROW的D欄備用
If Target.Row < 19 Then Exit Sub '起始行號為19
If Target.Column = 6 Then
Target.Offset(1, -2).Select
End If
If Target.Offset(-1, -2).Value = "貼錯Label" Then
Beep
MsgBox "貼錯Label"
End If
If Target.Offset(-1, -2).Value = "重覆,再掃描" Then
Beep
MsgBox "重覆,再掃描"
End If
ERRORFREE:
Exit Sub
End Sub
On Error GoTo ERRORFREE
'利用TARGET.COLUMN功能,讓游標到達F欄時,自動跳到下一ROW的D欄備用
If Target.Row < 19 Then Exit Sub '起始行號為19
If Target.Column = 6 Then
Target.Offset(1, -2).Select
End If
If Target.Offset(-1, -2).Value = "貼錯Label" Then
Beep
MsgBox "貼錯Label"
End If
If Target.Offset(-1, -2).Value = "重覆,再掃描" Then
Beep
MsgBox "重覆,再掃描"
End If
ERRORFREE:
Exit Sub
End Sub
一次性修改31份相同內容的工作紙內容
Sub ex()
For i = 1 To 31
With Sheets(CStr(i))
.Unprotect "12345" '解開保護
.[S2] = "21:00"
.[I12] = Sheets("1").[I12].Value
Sheets("1").[N1:N20].Copy .[N1]
' 一個WITH...END WITH中包含另一個/多個WITH...END WITH
With .[A15:A99].Validation '在A15至A99之間設立參照絕對值M1至M15的下拉式選單
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$M$1:$M$15"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
With .[B15:B99].Validation '在B15:B99之間設立下拉式選單
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$L$1:$L$15"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
With .[F15:F99].Validation '在F15至F99之間設立下拉式選單
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$K$1:$K$15"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
.Protect "12345" '設定保護
End With
Next
End Sub
For i = 1 To 31
With Sheets(CStr(i))
.Unprotect "12345" '解開保護
.[S2] = "21:00"
.[I12] = Sheets("1").[I12].Value
Sheets("1").[N1:N20].Copy .[N1]
' 一個WITH...END WITH中包含另一個/多個WITH...END WITH
With .[A15:A99].Validation '在A15至A99之間設立參照絕對值M1至M15的下拉式選單
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$M$1:$M$15"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
With .[B15:B99].Validation '在B15:B99之間設立下拉式選單
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$L$1:$L$15"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
With .[F15:F99].Validation '在F15至F99之間設立下拉式選單
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$K$1:$K$15"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.IMEMode = xlIMEModeNoControl
.ShowInput = True
.ShowError = True
End With
.Protect "12345" '設定保護
End With
Next
End Sub
將檔案另存新檔,檔名結構為"年月日.XLS"
以下程式為將另存的新檔存入同檔資料夾內:
With ThisWorkbook
.SaveCopyAs .Path & "\" & Format(Date, "yymmdd") & ".xls"
End With
With ThisWorkbook
.SaveCopyAs .Path & "\" & Format(Date, "yymmdd") & ".xls"
End With
訂閱:
文章 (Atom)