Delete Blank Layer Section Rows

Started by OldSchool1948, January 07, 2017, 04:13:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

OldSchool1948

Is there any way to delete blank rows in the Page.Layers section.  This code identifies the blank row, but won't delete the row.


Public Sub DeleteBlankLayerRow()

    Dim vsoPage As Visio.Page
    Set vsoPage = ActivePage
   
    Dim vsoShape As Visio.Shape
    Set vsoShape = vsoPage.PageSheet
   
    Dim intRows As Integer
    intRows = vsoShape.RowCount(Visio.visSectionLayer)
   
    Dim i As Integer
    Dim LayerName As String
   
    For i = 1 To intRows
   
        LayerName = vsoShape.CellsSRC(visSectionLayer, i, visLayerName).Formula
       
        If LayerName = "" Then
            vsoShape.DeleteRow Visio.visSectionLayer, i
        End If
       
    Next

End Sub



Any help would be greatly appreciated!

Paul Herber

Just because a row has a blank name doesn't mean that the layer is empty or unused.
Layers are a property of the page, they have to be deleted from the page. Deleting a leyer that is used by a shape will also delete that shape.

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

wapperdude

The problem would appear to be your Dim declaration.  Below are 2 macros that successfully delete a layer row.  They both assume the desired row has been identified. 

Keep in mind Paul's warning.


Sub Macro1()
'This works by opening the Layer Properties window for the active page.
'Note, the Layer Properties window has option to remove ALL unreferenced layers.
'Item count below begins with "1", not "0".
'The order of rows does not necessarily correspond to the item number???

    Dim vsoLayer1 As Visio.Layer
   
    Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item(3)
    vsoLayer1.CellsC(visLayerLock).FormulaU = "0"
    vsoLayer1.Delete 1

End Sub

Sub Macro2()
'This is run with no shapes selected,and works with the active window.
    ActiveWindow.Page.PageSheet.OpenSheetWindow
    ActiveWindow.Shape.DeleteRow visSectionLayer, 2  'Row count starts with "0", not "1"; this is 3rd row.

End Sub


The macro recorder is your friend.
Wapperdude
Visio 2019 Pro