Sub 九九剩法()
Dim i As Integer
Dim j As Integer
For i = 1 To 9
? ? ? ? For j = 1 To 9
? ? ? ? ? ?im = i Mod 2
? ? ? ? ? k = i & "×" & j & "=" & i * j
? ? ? ? ? ? If i <= j Then
? ? ? ? ? ? Sheets("Sheet1").Cells(j, i) = k
? ? ? ? ? ? Cells(j, i).Interior.ColorIndex = 3
If im = 0 Then
Cells(j, i).Interior.ColorIndex = 6
End If
End If
Next j
Next i
End Sub
案列二
Sub fun()
MsgBox "Entered value is" & Range("A1").Value & vbNewLine & Range("B1").Value
? ?'輸出字符串entered value is單元格A1的值以及換行單元格B1的值
End Sub
案列三
Sub fun()
Dim answer As Integer
answer = MsgBox("Are you sure you want to empty the sheet?", 4 + 32, "Empty? sheet")
'msgbox函數(shù)4代表“是”和“否”;32代表警告窗口
answer = MsgBox("Are you sure you want to empty the sheet?", vbYesNo + vbQuestion, "Empty Sheet")
'msgbox函數(shù)vbYesNo代表“是”和“否”;VBQuestion代表警告窗口
If answer = vbYes Then
Cells.ClearContents '如果點(diǎn)擊answer對(duì)話框是yes的,那么將清空所有單元格內(nèi)容
Else
'do nothing
End If
End Sub
宏的錄制方法
Sub 宏41()
'
' 宏41 宏
'
' 快捷鍵: Ctrl+s
'
Selection.NumberFormatLocal = "0.00%" '單元格里面變成0.00%
End Sub
Sub 宏2()
'
' 宏2 宏
'
'
Selection.NumberFormatLocal = _
"_ ¥* #,##0.00_ ;_ ¥* -#,##0.00_ ;_ ¥* ""-""??_ ;_ @_ " '單元格里面的數(shù)字變成價(jià)錢金額
End Sub
Sub 宏6()
'
' 宏6 宏
'
'
ActiveCell.FormulaR1C1 = "salse" '在單元格中填寫sales
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "production" '在單元格中填寫production
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "logistics" '在單元格中填寫logistics
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub
Private Sub CommandButton3_Click()
Range("E4").Formula = "=B12*2"
End Sub
Private Sub CommandButton2_Click()
Range("E4").FormulaR1C1 = "=R12C2*2" '單元格的絕對(duì)引用
End Sub
Private Sub CommandButton1_Click()
Range("E4").FormulaR1C1 = "=R[8]C[-3]*2"
'相對(duì)單元格的引用結(jié)果跟第一個(gè)一樣
End Sub
打開一個(gè)工作表并給工作表加密!
Sub 按鈕1_Click()
If Application.InputBox("請(qǐng)輸入密碼:") = 123 Then
Range("A1").Value = 10
Else
MsgBox "密碼錯(cuò)誤,即將退去!"
Sheets("sheet2").Select
End If
End Sub
2.在單元格里面輸值以及清除單元格格式以及內(nèi)容
Sub inserte()
Dim Color As Integer
Range("A1:A2").Select
Selection.EntireRow.Insert
ActiveCell.FormulaR1C1 = "text"
ActiveCell.FormulaR1C1 = "formulas"
Range("A1:A2").BorderAround Weight:=1
Range("A1:A6").Clear
End Sub
3.The properties of the cell selection
Sub Rngselect()
'Sheet1.Range("A3:F6, B1:C5").Select
Sheet2.Range("$A$3:F6 ,$C$5:$G$7").Select
'
End Sub
Sub Rngselect1()
'Sheet1.Range("A3:F6, B1:C5").Select
Sheet1.Range("A3:F6 ,C5:G7").Select
'Select the merge cell section
End Sub
Sub Rngselect2()
'Sheet1.Range("A3:F6, B1:C5").Select
Sheet1.Range("A3:F6? C5:G7").Select
'Select the intersecting cell section
End Sub
4.單元格序號(hào)填充
Sub cell()
Sub cell()
Dim Icell As Integer
For Icell = 1 To 100
Sheet2.Cells(Icell, 1).Value = Icell
'單元格序號(hào)填充
'? 英文注釋? ? ? ? ? The cell number is filled
Next
End Sub
5.for.......next帶循環(huán)語句
Sub cell()
Dim Icell, sum, j As Integer
For Icell = 1 To 100
sum = Icell + 1
j = j + sum
Sheet2.Cells(Icell, 1).Value = j
? ? ? ? ? ? ? ? ? ? ? ? ? ?'單元格序號(hào)填充
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?'? 英文注釋? ? ? ? ? The cell number is filled
Next
End Sub