I am trying to create a macro in MS Excel using VB 6.0 but I keep getting the error message "Invalid Redim" with the entire line "ReDim Preserve intArrEvenVlans(UBound(intArrEvenVlans) +1)" highlighted. Could anyone help me in resolving the problem:
Public Sub GenerateConfig()
' This subroutine will accumulate data from the various worksheets contained within the request
' And use these to create a configuration script for the Core Switches.
' Two configuration scripts will be generated, one for each core switch.
' Variables
Dim strSwitchName As String
Dim intRowCounter As Integer
Dim strTargetRange As String
Dim intFrontEndVlan As Integer
Dim intBackEndVlan As Integer
Dim intManagementVlan As Integer
Dim intArrOddVlans(0) As Integer
Dim intArrEvenVlans(0) As Integer
' Switch Name
' Collect from B47 on Worksheet 5
Worksheets(5).Activate
Range("B47").Activate
strSwitchName = ActiveCell.Value
' Add switch Name to config
Worksheets(6).Activate
intRowCounter = 1
strTargetRange = "A" & intRowCounter
Range(strTargetRange).Activate
ActiveCell.Value = strSwitchName
'Move counter
intRowCounter = intRowCounter + 3
' Spanning Trees
' This should collect the front end vlan, management and the back end vlan, then create a spanning tree with the priority
' based on whether the VLAN is odd or Even, Odd VLANs will have a spanning tree priority of 8192 and Odd will be 16384.
' Collect VLANs
Worksheets(4).Activate
Range("E5").Activate
intFrontEndVlan = ActiveCell.Value
ActiveCell.Offset(1, 0).Activate
intBackEndVlan = ActiveCell.Value
Range("G5").Activate
intManagementVlan = ActiveCell.Value
' For each of the collected vlans, establish if odd or even and then add to correct array...
'Front End
AddToVlanArray (intFrontEndVlan)
'Back End
AddToVlanArray (intBackEndVlan)
'Management
AddToVlanArray (intManagementVlan)
' Create spanning tree lines
Dim strSpanTreeOdd As String
strSpanTreeOdd = "spanning-tree vlan "
For i = 0 To UBound(intArrOddVlans)
If intArrOddVlans(i) <> 0 Then
strSpanTreeOdd = strSpanTreeOdd & CStr(intArrOddVlans(i)) & ","
End If
Next i
strSpanTreeOdd = Left(strSpanTreeOdd, (Len(strSpanTreeOdd) - 1))
strSpanTreeOdd = strSpanTreeOdd & " priority 8192"
MsgBox (strSpanTreeOdd)
End Sub
Public Function OddOrEven(ByVal NumToCheck As Integer) As Integer
If NumToCheck Mod 2 <> 0 Then
OddOrEven = 1
Else
OddOrEven = 0
End If
End Function
Public Sub AddToVlanArray(ByVal Vlan As Integer)
If OddOrEven(Vlan) = 0 Then
intArrEvenVlans(UBound(intArrEvenVlans, 1)) = Vlan
ReDim Preserve intArrEvenVlans(UBound(intArrEvenVlans) + 1) As Integer
Else
intArrOddVlans(UBound(intArrOddVlans, 1)) = Vlan
ReDim Preserve intArrOddVlans(UBound(intArrOddVlans) + 1) As Integer
End If
End Sub
Public Sub GenerateConfig()
' This subroutine will accumulate data from the various worksheets contained within the request
' And use these to create a configuration script for the Core Switches.
' Two configuration scripts will be generated, one for each core switch.
' Variables
Dim strSwitchName As String
Dim intRowCounter As Integer
Dim strTargetRange As String
Dim intFrontEndVlan As Integer
Dim intBackEndVlan As Integer
Dim intManagementVlan As Integer
Dim intArrOddVlans(0) As Integer
Dim intArrEvenVlans(0) As Integer
' Switch Name
' Collect from B47 on Worksheet 5
Worksheets(5).Activate
Range("B47").Activate
strSwitchName = ActiveCell.Value
' Add switch Name to config
Worksheets(6).Activate
intRowCounter = 1
strTargetRange = "A" & intRowCounter
Range(strTargetRange).Activate
ActiveCell.Value = strSwitchName
'Move counter
intRowCounter = intRowCounter + 3
' Spanning Trees
' This should collect the front end vlan, management and the back end vlan, then create a spanning tree with the priority
' based on whether the VLAN is odd or Even, Odd VLANs will have a spanning tree priority of 8192 and Odd will be 16384.
' Collect VLANs
Worksheets(4).Activate
Range("E5").Activate
intFrontEndVlan = ActiveCell.Value
ActiveCell.Offset(1, 0).Activate
intBackEndVlan = ActiveCell.Value
Range("G5").Activate
intManagementVlan = ActiveCell.Value
' For each of the collected vlans, establish if odd or even and then add to correct array...
'Front End
AddToVlanArray (intFrontEndVlan)
'Back End
AddToVlanArray (intBackEndVlan)
'Management
AddToVlanArray (intManagementVlan)
' Create spanning tree lines
Dim strSpanTreeOdd As String
strSpanTreeOdd = "spanning-tree vlan "
For i = 0 To UBound(intArrOddVlans)
If intArrOddVlans(i) <> 0 Then
strSpanTreeOdd = strSpanTreeOdd & CStr(intArrOddVlans(i)) & ","
End If
Next i
strSpanTreeOdd = Left(strSpanTreeOdd, (Len(strSpanTreeOdd) - 1))
strSpanTreeOdd = strSpanTreeOdd & " priority 8192"
MsgBox (strSpanTreeOdd)
End Sub
Public Function OddOrEven(ByVal NumToCheck As Integer) As Integer
If NumToCheck Mod 2 <> 0 Then
OddOrEven = 1
Else
OddOrEven = 0
End If
End Function
Public Sub AddToVlanArray(ByVal Vlan As Integer)
If OddOrEven(Vlan) = 0 Then
intArrEvenVlans(UBound(intArrEvenVlans, 1)) = Vlan
ReDim Preserve intArrEvenVlans(UBound(intArrEvenVlans) + 1) As Integer
Else
intArrOddVlans(UBound(intArrOddVlans, 1)) = Vlan
ReDim Preserve intArrOddVlans(UBound(intArrOddVlans) + 1) As Integer
End If
End Sub
Comment