Excel VBA notes

Specify Cells (S4)

' One Cell
(B5)
' Part of a Column
(B5:B9)
' Part of a Row
(B5:E5)
' Matrix of Cells
(B5:E9)
' Unrelated Cells
(B5, B9, E9)
' Entire Column
(B:B)
' Entire Row
(5:5)
' Multiple Columns
(B:C)
' Multiple Rows
(5:7)

(S5)

VBA (S13)

Sub Hello()
	MsgBox "This is body msg in msgbox", vbInformation, "Head title"
End Sub

vbInformation refers to icons, some icons include:
None: vbDefaultButton1
! :
VbExclamation X: vbCritical
i: vbInformation
? : VbQuestion

If / else (S15)

Dim Age
Age = InputBox("How old are you?")
if Age = 12 Then
	Range("B3").Value = "12 years old only"
End If

If Age >= 18 Then
	Range("B4").Value = _
		"Adult. "
Else
	Range("B4").Value = _
		"Kid. "
End If

Loop (S16)

method 1

While ActiveCell.Row <= 10
	' Put Row number into first cell
	Active.Value = "I am row " & ActiveCell.Row
	' Move the selection down one cell
	ActiveCell.Offset(1,0).Select
Wend

Method 2 (the effect is the same as above)

Do While ActiveCell.Row <= 10
	' Put Row number into first cell
	Active.Value = "I am row " & ActiveCell.Row
	' Move the selection down one cell
	ActiveCell.Offset(1,0).Select
Loop

Method 3 (Run the content first and then decide whether to Loop)

Do
	' Ask a question
	Answer = InputBox ( _
		" Are you above 18 years old ?")
Loop While LCase(Answer) <> "yes"

Guess you like

Origin blog.csdn.net/MikeW138/article/details/96480709
Recommended