Issues looping through excel data range when blank rows/cells are introduced.

Jeezer

New Member
Joined
Jun 3, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I am trying to accomplish the following with this code:
  1. Take entries from a spreadsheet and populate Visio Shapes in a specific x,y location
  2. Then connect the shapes with dynamic connectors based on info filled out by the user
The code that I have does accomplish this, however there is a specific scenario that seems to break it when I loop through the excel data.
After the shapes have been dropped into Visio, I then look at columns L (source connection) and B (destination connection) to see where I should glue the ends of the dynamic connector in Visio. This does work when the data is consistent (no gaps/blanks) between the start and end of the populated range. However, if there is a blank cell/row anywhere in the middle, I get an "Unexpected end of file" error. See the following example:

1685801505416.png


In this case, after the loop is past L19, I can see by adding watches...that it cannot determine the ID values in column L and B, which is how I connect the end points of the connector to the Visio shapes.

Below is the entire module:

VBA Code:
Sub VisioNetworkAutomation()
    Dim ws As Worksheet
    Dim visApp As Visio.Application
    Dim visDoc As Visio.Document
    Dim visPage As Visio.Page
    Dim visStencil As Visio.Document
    Dim visShape As Visio.Shape
    Dim visConnector As Visio.Shape
    Dim boundingBox As Object
    Dim numberOfPages As Long
    Dim i As Long
    
    Dim deviceCountColumn As Long
    Dim lastDeviceRow As Long
    Dim endCell As Range
    
    Set ws = ThisWorkbook.Worksheets("Network Architecture")    ' Set your worksheet and the column number you want to check
    
    numberOfPages = ws.Range("B9")  ' Determines number of pages entered by user
    
    ' Create a new Visio document
    Set visApp = New Visio.Application
    Set visDoc = visApp.Documents.Open("C:\_EAA Network Automation\EAA Network_Blank.vsdm")

    For i = 1 To numberOfPages
        ' Create Visio pages, then sets the name and background
        Set visPage = visDoc.Pages.Add
        visPage.Name = "Page-" & i
        visPage.BackPage = "Background-1"
    Next i
    
    Set visStencil = visApp.Documents.OpenEx("EAA.VSSX", visOpenDocked) ' Open the Basic Shapes stencil
    
    deviceCountColumn = 10  ' Column to check = J, Change this to the desired column number (1 for Column A, 2 for Column B, etc.)
    lastDeviceRow = ws.Cells(ws.Rows.Count, deviceCountColumn).End(xlUp).Row  ' Find the last non-empty row in the column
    Set endCell = ws.Cells(lastDeviceRow, deviceCountColumn)  ' Set end cell ranges

    Dim dataRange As Range
    Dim cell As Range
    Dim xCoord As Variant
    Dim yCoord As Variant
    Dim connectionTemplate
    Dim idMatch As Range


    Set dataRange = ws.Range(Range("J12"), Range(endCell.Address(0, 0)))    'Sets the range from J12 to last populated cell in J column

    ' Loop through the range and store the cell values in the array
    For Each cell In dataRange
        connectionTemplate = cell.Value     ' Grabs the Connection Template of the current cell row
        xCoord = cell.Offset(0, 4).Value    ' Grabs the x-coordinate of the current cell row
        yCoord = cell.Offset(0, 5).Value    ' Grabs the y-coordinate of the current cell row

        If IsEmpty(cell) = False Then
            Set visShape = visDoc.Pages("Page-" & cell.Offset(0, 3).Value).Drop(visStencil.Masters(connectionTemplate), xCoord, yCoord) ' Drops current shape on current page
            visShape.Name = cell.Offset(0, -8).Value
            
            ' Write into Shapesheet data of shape
            visShape.Cells("Prop.Name.Value").FormulaU = Chr(34) & cell.Offset(0, -6) & Chr(34) 'Writes C4 to shapedata
            visShape.Cells("Prop.Model.Value").FormulaU = Chr(34) & cell.Offset(0, -5) & Chr(34) 'Writes D4 to shapedata
            visShape.Cells("Prop.IP.Value").FormulaU = Chr(34) & cell.Offset(0, -4) & Chr(34) 'Writes E4 to shapedata
            visShape.Cells("Prop.Subnet.Value").FormulaU = Chr(34) & cell.Offset(0, -3) & Chr(34) 'Writes F4 to shapedata
            visShape.Cells("Prop.Gateway.Value").FormulaU = Chr(34) & cell.Offset(0, -2) & Chr(34) 'Writes G4 to shapedata
            
        End If
    Next cell
    
    Set idRange = Range("L12:L51")
    
    For Each cell In idRange.SpecialCells(Type:=xlCellTypeConstants, Value:=xlNumbers)

        Set visConnector = visDoc.Pages("Page-" & cell.Offset(0, 1).Value).Drop(visApp.ConnectorToolDataObject, 0, 0)

        ' Connect the shapes (ID to Source)
        visConnector.Cells("BeginX").GlueTo visDoc.Pages("Page-" & cell.Offset(0, 4).Value).Shapes(cell.Offset(0, -10).Value).Cells("Connections.Left")
        visConnector.Cells("EndX").GlueTo visDoc.Pages("Page-" & cell.Offset(0, 1).Value).Shapes(cell.Value).Cells("Connections.Right")

    Next cell
End Sub


The piece in question is the last For loop which checks all populated cells in idRange (column L):


VBA Code:
Set idRange = Range("L12:L51")
    
    For Each cell In idRange.SpecialCells(Type:=xlCellTypeConstants, Value:=xlNumbers)

        Set visConnector = visDoc.Pages("Page-" & cell.Offset(0, 1).Value).Drop(visApp.ConnectorToolDataObject, 0, 0)

        ' Connect the shapes (ID to Source)
        visConnector.Cells("BeginX").GlueTo visDoc.Pages("Page-" & cell.Offset(0, 4).Value).Shapes(cell.Offset(0, -10).Value).Cells("Connections.Left")
        visConnector.Cells("EndX").GlueTo visDoc.Pages("Page-" & cell.Offset(0, 1).Value).Shapes(cell.Value).Cells("Connections.Right")

    Next cell



You can see from the watches, when the loop is currently is on L18, it can still properly determine the ID values in L18/B18.
1685801886539.png



For whatever reason I cannot graps, after I loop past a blank in column L, it does not seem to be able to execute this to what I would expect.
1685802019699.png

At this point, it can identify L20 = 7, but then grabs the "dynamic connector" for B20?

Again, when there are no breaks in data from cell to cell, the code runs fine.
The files can be found here: Excel to Visio - Google Drive
The current state of the files will error out, as I have left row 19 blank. To see the file work, simply copy the contents of row 18 or 20, to row 19. Thanks for any assistance here.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Reddit
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Reddit
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
Not seeing what the issue is here. When you post on one forum with 0 responses...can you really blame someone?
 
Upvote 0
I am trying to accomplish the following with this code:
  1. Take entries from a spreadsheet and populate Visio Shapes in a specific x,y location
  2. Then connect the shapes with dynamic connectors based on info filled out by the user
The code that I have does accomplish this, however there is a specific scenario that seems to break it when I loop through the excel data.
After the shapes have been dropped into Visio, I then look at columns L (source connection) and B (destination connection) to see where I should glue the ends of the dynamic connector in Visio. This does work when the data is consistent (no gaps/blanks) between the start and end of the populated range. However, if there is a blank cell/row anywhere in the middle, I get an "Unexpected end of file" error. See the following example:

View attachment 92843

In this case, after the loop is past L19, I can see by adding watches...that it cannot determine the ID values in column L and B, which is how I connect the end points of the connector to the Visio shapes.

Below is the entire module:

VBA Code:
Sub VisioNetworkAutomation()
    Dim ws As Worksheet
    Dim visApp As Visio.Application
    Dim visDoc As Visio.Document
    Dim visPage As Visio.Page
    Dim visStencil As Visio.Document
    Dim visShape As Visio.Shape
    Dim visConnector As Visio.Shape
    Dim boundingBox As Object
    Dim numberOfPages As Long
    Dim i As Long
   
    Dim deviceCountColumn As Long
    Dim lastDeviceRow As Long
    Dim endCell As Range
   
    Set ws = ThisWorkbook.Worksheets("Network Architecture")    ' Set your worksheet and the column number you want to check
   
    numberOfPages = ws.Range("B9")  ' Determines number of pages entered by user
   
    ' Create a new Visio document
    Set visApp = New Visio.Application
    Set visDoc = visApp.Documents.Open("C:\_EAA Network Automation\EAA Network_Blank.vsdm")

    For i = 1 To numberOfPages
        ' Create Visio pages, then sets the name and background
        Set visPage = visDoc.Pages.Add
        visPage.Name = "Page-" & i
        visPage.BackPage = "Background-1"
    Next i
   
    Set visStencil = visApp.Documents.OpenEx("EAA.VSSX", visOpenDocked) ' Open the Basic Shapes stencil
   
    deviceCountColumn = 10  ' Column to check = J, Change this to the desired column number (1 for Column A, 2 for Column B, etc.)
    lastDeviceRow = ws.Cells(ws.Rows.Count, deviceCountColumn).End(xlUp).Row  ' Find the last non-empty row in the column
    Set endCell = ws.Cells(lastDeviceRow, deviceCountColumn)  ' Set end cell ranges

    Dim dataRange As Range
    Dim cell As Range
    Dim xCoord As Variant
    Dim yCoord As Variant
    Dim connectionTemplate
    Dim idMatch As Range


    Set dataRange = ws.Range(Range("J12"), Range(endCell.Address(0, 0)))    'Sets the range from J12 to last populated cell in J column

    ' Loop through the range and store the cell values in the array
    For Each cell In dataRange
        connectionTemplate = cell.Value     ' Grabs the Connection Template of the current cell row
        xCoord = cell.Offset(0, 4).Value    ' Grabs the x-coordinate of the current cell row
        yCoord = cell.Offset(0, 5).Value    ' Grabs the y-coordinate of the current cell row

        If IsEmpty(cell) = False Then
            Set visShape = visDoc.Pages("Page-" & cell.Offset(0, 3).Value).Drop(visStencil.Masters(connectionTemplate), xCoord, yCoord) ' Drops current shape on current page
            visShape.Name = cell.Offset(0, -8).Value
           
            ' Write into Shapesheet data of shape
            visShape.Cells("Prop.Name.Value").FormulaU = Chr(34) & cell.Offset(0, -6) & Chr(34) 'Writes C4 to shapedata
            visShape.Cells("Prop.Model.Value").FormulaU = Chr(34) & cell.Offset(0, -5) & Chr(34) 'Writes D4 to shapedata
            visShape.Cells("Prop.IP.Value").FormulaU = Chr(34) & cell.Offset(0, -4) & Chr(34) 'Writes E4 to shapedata
            visShape.Cells("Prop.Subnet.Value").FormulaU = Chr(34) & cell.Offset(0, -3) & Chr(34) 'Writes F4 to shapedata
            visShape.Cells("Prop.Gateway.Value").FormulaU = Chr(34) & cell.Offset(0, -2) & Chr(34) 'Writes G4 to shapedata
           
        End If
    Next cell
   
    Set idRange = Range("L12:L51")
   
    For Each cell In idRange.SpecialCells(Type:=xlCellTypeConstants, Value:=xlNumbers)

        Set visConnector = visDoc.Pages("Page-" & cell.Offset(0, 1).Value).Drop(visApp.ConnectorToolDataObject, 0, 0)

        ' Connect the shapes (ID to Source)
        visConnector.Cells("BeginX").GlueTo visDoc.Pages("Page-" & cell.Offset(0, 4).Value).Shapes(cell.Offset(0, -10).Value).Cells("Connections.Left")
        visConnector.Cells("EndX").GlueTo visDoc.Pages("Page-" & cell.Offset(0, 1).Value).Shapes(cell.Value).Cells("Connections.Right")

    Next cell
End Sub


The piece in question is the last For loop which checks all populated cells in idRange (column L):


VBA Code:
Set idRange = Range("L12:L51")
   
    For Each cell In idRange.SpecialCells(Type:=xlCellTypeConstants, Value:=xlNumbers)

        Set visConnector = visDoc.Pages("Page-" & cell.Offset(0, 1).Value).Drop(visApp.ConnectorToolDataObject, 0, 0)

        ' Connect the shapes (ID to Source)
        visConnector.Cells("BeginX").GlueTo visDoc.Pages("Page-" & cell.Offset(0, 4).Value).Shapes(cell.Offset(0, -10).Value).Cells("Connections.Left")
        visConnector.Cells("EndX").GlueTo visDoc.Pages("Page-" & cell.Offset(0, 1).Value).Shapes(cell.Value).Cells("Connections.Right")

    Next cell



You can see from the watches, when the loop is currently is on L18, it can still properly determine the ID values in L18/B18.
View attachment 92844


For whatever reason I cannot graps, after I loop past a blank in column L, it does not seem to be able to execute this to what I would expect.
View attachment 92845
At this point, it can identify L20 = 7, but then grabs the "dynamic connector" for B20?

Again, when there are no breaks in data from cell to cell, the code runs fine.
The files can be found here: Excel to Visio - Google Drive
The current state of the files will error out, as I have left row 19 blank. To see the file work, simply copy the contents of row 18 or 20, to row 19. Thanks for any assistance here.
Have you tried entering an If statement for an empty cell? Even if it only handles an error, it may work.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top