excel 是否有方法根据电子表格中的位置列出图表编号?(不按排序顺序?)

dfuffjeb  于 2023-01-27  发布在  其他
关注(0)|答案(2)|浏览(159)

下面的宏按工作表列出了所有的图表编号,但它对它们进行了排序,这不是图表在工作表中的显示方式。

Sub ListChartNames()
Dim Cht As ChartObject
Dim i As Integer
i = 1
For Each Cht In ActiveSheet.ChartObjects
Cells(i, 1) = Cht.Chart.Name
i = i + 1
Next Cht
End Sub

例如,我在E6:L17中有一个图表(我们称之为图表1),在N6:U17中有另一个图表(我们称之为图表11)。然后,我向下移动到E19:L30中的两个图表(我们称之为图表400)和N19:U30中的另一个图表(我们称之为图表2)。然后,我向下移动到E32:L43中的两个图表(我们称之为图表3)和N32:U43中的另一个图表(我们称之为图表12)。然后,我向下移动到E45:L56中的一个图表(我们称之为图表13)。然后,我返回到E58:L69中的两个图表和N58:U69中的另一个图表(我们称之为图表15和图表16)等等...
以上图表都在E列到U列。但是在Y列到AO列中有另一组相同的模式,在AS列到BI列中也有,等等。
我有像500图表,我想一个宏来列出他们开始在第一组列(E到L),但他们从上到下列出,让右。
因此,基于上述内容的结果将针对列F到U图表1图表11图表400图表2图表3图表12图表13图表15图表16
上面的宏以排序的顺序列出了图表,这不是我所需要的。
这也没有回答以下问题:Select chart object based on position in sheet (VBA)

92dk7w1h

92dk7w1h1#

这能给予你的需要吗

Sub list_charts_in_top_left_to_bottom_right()
    
    Dim ws As Worksheet, outputsh As Worksheet, last_cell As Range, oChartObj As Object
    
    Set ws = ThisWorkbook.Sheets("SheetWithChartsOnIt")
    Set outputsh = ThisWorkbook.Sheets("SheetToWriteTo")
    
    outputsh.Range("A:A").ClearContents
    
    outputsh.Range("A1") = "Output:"
    
    If ws.ChartObjects.Count = 0 Then
        outputsh.Range("A2") = "No charts found"
        Exit Sub
    End If
    
    Debug.Print "Charts found: " & ws.ChartObjects.Count
    
    Set last_cell = ws.Range("A1")
    
    'find bounds of range by expanding last_cell with each chart
    For Each oChartObj In ws.ChartObjects
        With oChartObj

            If .TopLeftCell.Row > last_cell.Row Then Set last_cell = ws.Cells(.TopLeftCell.Row, last_cell.Column)
            If .TopLeftCell.Column > last_cell.Column Then Set last_cell = ws.Cells(last_cell.Row, .TopLeftCell.Column)
        End With
    Next
    
    Debug.Print "Bounds of range: $A$1:" & last_cell.Address

    Dim area_to_examine As Range

    For col = 5 To last_cell.Column Step 21 'start with column 5 (E) and then jump 21 columns at a time
    
    Set area_to_examine = Range(Columns(col), Columns(col + 17))
    
    Debug.Print "Examining: " & area_to_examine.Address
    
        For Each rw In Intersect(area_to_examine, ws.Range("A1", last_cell.Address).Rows)
            For Each cl In rw.Cells
                For Each oChartObj In ws.ChartObjects
                    With oChartObj
                        If .TopLeftCell.Row = cl.Row And .TopLeftCell.Column = cl.Column Then
                        outputsh.Cells(outputsh.Rows.Count, "A").End(xlUp).Offset(1) = .Name
                        Debug.Print .Name
                        End If
                    End With
                Next
            Next
        Next
    Next
End Sub
jei2mxaa

jei2mxaa2#

这是一种替代方法,它仍然没有使用排序算法,而是使用了一种变通方法(确实浪费了一点时间,但是),这比扫描工作表中的每个单元格要快得多:

Sub list_charts_in_top_left_to_bottom_right_v2()
    
    Dim ws As Worksheet, outputsh As Worksheet, chartCount As Long, x As Long, y As Long, maxZ As Long
    
    Set ws = ThisWorkbook.Sheets("SheetWithChartsOnIt")
    Set outputsh = ThisWorkbook.Sheets("SheetToWriteTo")
    
    outputsh.Range("A:A").ClearContents
    
    outputsh.Range("A1").Value = "Chart"
    chartCount = ws.ChartObjects.Count
    
    ReDim arrChartlist(chartCount, 1)
    
    If chartCount = 0 Then
        outputsh.Range("A2") = "No charts found"
        Exit Sub
    End If
           
    maxZ = 0
    
    For x = 0 To chartCount - 1
        With ws.ChartObjects(x + 1)
            arrChartlist(x, 0) = .Name
            arrChartlist(x, 1) = (((.TopLeftCell.Column - 2) \ 19) * chartCount * chartCount) + (.TopLeftCell.Column * chartCount) + .TopLeftCell.Row
            If maxZ < arrChartlist(x, 1) Then maxZ = arrChartlist(x, 1)
        End With
    Next

    For x = 0 To maxZ
        For y = 0 To chartCount - 1
            If x = arrChartlist(y, 1) Then
                outputsh.Cells(outputsh.Rows.Count, "A").End(xlUp).Offset(1).Value = arrChartlist(y, 0)
            End If
        Next
    Next
      
End Sub

相关问题