Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

excel - Working macro returns error when run from form control

I'm trying to move all charts from source sheet to destination sheet. The following code works fine when I run it from the VB Editor. My source had two charts and both were successfully moved.

But when I try to run it using a form button from a third sheet (neither source nor destination), it only works for the first chart and then returns an error. Infuriatingly it retains a copy of the 'moved' chart in the source as well.

Sub moveAllCharts(source As String, destination As String)
    Dim chartObject As Object
    
    ActiveWorkbook.Worksheets.Add(After:=Application.Worksheets(source)).Name = destination
    For Each chartObject In Worksheets(source).ChartObjects
        chartObject.Chart.Location xlLocationAsObject, destination
    Next chartObject
End Sub

The code gets stuck inside the loop and returns a The specified dimension is not valid for the current chart type error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like I said in comments, this is a very old problem which can be only sorted (I hope I am wrong) by activating the source sheet and selecting the chart. If you do not do that, you will end up with The specified dimension is not valid for the current chart type Chart. error

enter image description here

Is this what you are trying?

Sub moveAllCharts(source As String, destination As String)
    Dim chartObject As Object
    
    ActiveWorkbook.Worksheets.Add(After:=Application.Worksheets(source)).Name = destination
    
    For Each chartObject In Worksheets(source).ChartObjects
        '~~> Need both of these
        Worksheets(source).Activate
        chartObject.Select
        
        ActiveChart.Location xlLocationAsObject, destination
    Next chartObject
End Sub

BTW, since you are forced to use .Activate and .Select, the above code should also include error handling. For example

Sub moveAllCharts(source As String, destination As String)
    Dim chartObject As Object
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Worksheets(source)
        
    On Error GoTo Whoa
        
    wb.Worksheets.Add(After:=wb.Worksheets(source)).Name = destination
        
    For Each chartObject In ws.ChartObjects
        '~~> Need both of these
        ws.Activate
        chartObject.Select
            
        ActiveChart.Location xlLocationAsObject, destination
    Next chartObject
LetsContinue:
    Exit Sub
Whoa:
    MsgBox err.Description
    Resume LetsContinue
End Sub

In Action

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...