使用F8或F5键时出现不同的功能行为 [英] I get different function behavior when using F8 or F5 key

查看:108
本文介绍了使用F8或F5键时出现不同的功能行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个特定的函数来格式化我的图表。主要Sub和功能代码:

I created a specific function to format my charts. The main Sub and the Function code:

Private Sub UserForm_Initialize()

Dim mychart         As Chart
Dim ChartData       As Range
Dim ChartName       As String
Dim thiswb          As Workbook
Dim imageName       As String
Dim nColunas        As Long
Dim i, j, k         As Integer

Set thiswb = ThisWorkbook

k = 0

With thiswb.Sheets(4)
   MultiPage1.Pages.Add
   MultiPage1.Pages(k - 1).Controls.Copy
   MultiPage1.Pages(k).Paste
   Set ChartData = .Range("B2:B97")
   Set mychart = .Shapes.AddChart(xlXYScatterLines).Chart
     For j = mychart.SeriesCollection.Count To 1 Step -1
        If j = 1 Then
            Exit For
        End If
        mychart.SeriesCollection(j).Delete
     Next j
   mychart.SeriesCollection(1).Values = ChartData
   mychart.SeriesCollection(1).XValues = .Range(.Cells(2, 1), .Cells(97, 1))

   formatchart mychart

   With .Shapes(1).Chart.Axes(xlCategory)
     .MinimumScale = 0
     .MaximumScale = 1
   End With
   imageName = Application.DefaultFilePath & Application.PathSeparator & "GraficoTemp.gif"
   mychart.Export Filename:=imageName, FilterName:="GIF"
   .Shapes(1).Delete
   UserForm2.MultiPage1.Pages(k).Caption = "Total"
   UserForm2.Controls("Image" & k + 1).Picture = LoadPicture(imageName)

   Kill imageName

   With Controls("Listbox" & k + 1)
     .RowSource = "Total!B2:B97"
   End With
End With
End Sub

Function formatchart(mychart As Chart)
 With mychart
    .HasTitle = False
    .Legend.LegendEntries(1).Delete
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Caption = "Horas"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Potência (W)"
 End With
 With mychart.Parent
    .Height = 295 
    .Width = 470 
    .Top = 100    
    .Left = 100  
 End With
End Function

当您可以看到, formatchart功能要做的第一件事就是删除图表标题。发生的是,当我按F5键运行完整的Sub时,标题未删除。但是,当我按F8调试它时,我将按照Sub的所有步骤进行操作,直到最后,标题最终都被删除了!

As you can see, the first thing the "formatchart" function does is remove the chart title. What happens is, when I run the complete Sub by pressing F5, the title is not removed. But when I debug it by pressing F8, I follow all the steps of the Sub till the end and the title ends up being removed!

会发生什么? p>

What could be happening?

推荐答案

已经使用完整的代码在我的计算机上重建了方案,以下两件事使您的代码对我有用:

Having reconstructed the scenario on my computer with the complete code the following two things made your code work for me:

(1)将函数更改为子项,然后
(2)将 ChartObject 传递给子项,而不是图表

(1) Change the function into a sub and (2) pass the ChartObject to the sub instead of Chart.

因此,在此之后,您将不得不

So, following this you'll have to

Dim mychart as ChartObject

,初始设置会略有不同更改为

and the initial setup will slightly change to

   Set mychart = .ChartObjects.Add(295, 470, 100, 100)
   mychart.Chart.ChartType = xlXYScatterLines

而不是

formatchart mychart

您必须像这样调用该程序

you'll have to call the procedure like so

Call formatchart(mychart)

同时将函数更改为过程

Sub formatchart(mychart As ChartObject)

With mychart.Chart
   .HasTitle = False
   .Legend.LegendEntries(1).Delete
   .Axes(xlCategory, xlPrimary).HasTitle = True
   .Axes(xlCategory, xlPrimary).AxisTitle.Caption = "Horas"
   .Axes(xlValue, xlPrimary).HasTitle = True
   .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Potência (W)"
End With

With mychart
   .Height = 295
   .Width = 470
   .Top = 100
   .Left = 100
End With

End Sub

我相信问题是您根据变量声明添加了图表,但是在上面的原始代码中查看形状

I believe the problem is that you are adding a Chart according to your variable declaration but a Shape when looking at your original code above:

Set mychart = .Shapes.AddChart(xlXYScatterLines).Chart

我不确定为什么函数无效,而必须是一个过程。因此,由于我还没有完全理解问题,所以我只能提供在这里可行的方法。如果这没有帮助,那么我们希望其他人提出更确定的解决方案。

I am not sure yet why the function didn't work and had to be a procedure instead. So, since I don't fully comprehend the problem yet I can only offer what seems to work here. If this doesn't help then let's hope that someone else comes up with a more conclusive solution.

这篇关于使用F8或F5键时出现不同的功能行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆