Although the Subnets macro is written in Visual Basic for Applications (VBA), its logic and code
are similar to the logic and code you’d see in VBScript scripts. As Listing 1 shows, the macro
begins by declaring a few variables, defining their types, and assigning them some initial values.
The macro then instructs Microsoft Excel to focus its attention on Sheet1. As callout A in Listing
1 shows, the Cells. Value property fetches the contents of the cell at “row, column”, which in this
case, is the first IP address in the list. The property passes this IP address to the GetSubnet
function, which is the heart of the macro.
In VBA, a Function procedure can be used on the right side of an expression and therefore can
return a value. The function’s name is used as a variable within the function; the value that this
variable contains when the function exits is assigned to the variable on the left side of the
expression back in the main routine. In this case, the GetSubnet function’s name acts as a
variable within the GetSubnet function, as callout C in Listing 1, in the main article shows. When the function
completes, this variable contains the desired subnet string. The desired subnet string is assigned
to the Subnet1 variable at callout A. . . .