Through Rows
Tips
- Use
Option Explicit
for VBA to detect errors.
- Use constants at the beginning of the code to quickly be able to change them in one place.
- Declare all variables (e.g.
Dim i As Integer
)
The Code
Option Explicit
Sub RectangleRoundedCorners11_Click()
Const cFirst As Integer = 11 ' First Row
Const cLast As Integer = 60 ' Last Row
Const cRequest As String = "new request" ' Request Text
Const cMsg As String = "Please fill all mandatory fields" ' MsgBox Text
Dim i As Integer
For i = cFirst To cLast
If Range("A" & i).Value = cRequest Then
If Range("D" & i).Value = "" Or Range("E" & i).Value = "" _
Or Range("G" & i).Value = "" Or Range("H" & i).Value = "" Then
MsgBox cMsg
End If
End If
Next
End Sub
- A one cell range can be created using Range or Cells e.g. for
A1
:
Range("A1")
or Cells(1, "A")
or Cells(1, 1)
.
- The
If
statement has a few versions. In this case two of them are
equally valid, simplified as follows:
If x=y Then
x=5
End If
' or
If x=y Then x=5
A More Advanced Version
Sub RectangleRoundedCorners11_Click()
Const cFirst As Integer = 11 ' First Row
Const cLast As Integer = 60 ' Last Row
Const cRequest As String = "new request" ' Request Text
Const cMsg As String = "Please fill all mandatory fields" ' MsgBox Text
Const cColumns As String = "A,D,E,G,H" ' Columns List
Dim vnt As Variant ' Columns Array
Dim i As Integer ' Row Counter
vnt = Split(cColumns, ",") ' An array created with Split is 0-based.
For i = cFirst To cLast
If Cells(i, vnt(0)).Value = cRequest Then
If Cells(i, vnt(1)).Value = "" Or Cells(i, vnt(2)).Value = "" _
Or Cells(i, vnt(3)).Value = "" _
Or Cells(i, vnt(4)).Value = "" Then MsgBox cMsg
End If
Next
End Sub