简体   繁体   English

如何使公式的一部分变化?

[英]How can I make a part of a formula to vary?

I am trying to make just a part of the code change while the worksheet change, but it does not work. 我试图在工作表更改时仅对部分代码进行更改,但是它不起作用。 Changing from one worksheet to other is OK, but the changing integer do not work in the code. 从一个工作表更改为另一个工作表是可以的,但是更改的整数在代码中不起作用。 I create the J Integer to grow in one unit as the worksheet change. 我创建J Integer,使其随着工作表的更改而在一个单元中增长。 Therefore, the graph and the shapes are based on another Sheet (Dados) which has the data in different lines to provide the information to each worksheet. 因此,图形和形状基于另一个工作表(Dados),该工作表的数据位于不同的行中,以向每个工作表提供信息。

Sub relatorio()
Dim ws As Worksheet, GI As Integer, GF As Integer, J As Integer
J = 0
For Each ws In ActiveWorkbook.Worksheets
    With ws
        J = J + 1
        If .Name = "Brasil" Then
            i = (8 * J) + 4   'the 8 is the amount of lines below I need to catch and the 4 is because the data starts on line 4. 
            GI = (13 * J) + 271
            Gf = (13 * J) + 283    
        End If
        If .Name <> "Dados" Then
            With .Shapes("TRI")
                .Formula = "=Dados!a2"
                .ShapeRange.TextFrame2.TextRange.Font.Name = "Calibri"
                .ShapeRange.TextFrame2.TextRange.Font.Size = 9
            End With

It does not work from the following step on. 从下面的步骤开始,它将不起作用。 Would you please help me to solve this problem? 您能帮我解决这个问题吗?

        With .Shapes("PIT")
            .Formula = "=Dados!E(i)"
            .ShapeRange.TextFrame2.TextRange.Font.Name = "Calibri"
            .ShapeRange.TextFrame2.TextRange.Font.Size = 9
        End With
        With .ChartObjects("Gráfico 8")
            ActiveChart.PlotArea.Select
            ActiveChart.FullSeriesCollection(1).Values = "=Dados!$e$(GI):$e$(GF)"
            ActiveChart.FullSeriesCollection(1).XValues = "=Dados!$b$(GI):$c$(GF)"
            ActiveChart.SetElement (msoElementDataLabelTop)
            ActiveChart.FullSeriesCollection(1).DataLabels.Select
            Selection.NumberFormat = "#.##0,0"
        End with
    End if
Next
end sub

I am thinking the line that is causing the problem is .Formula `= "=Dados!E(i)" , is I intended to be a variable ? 我在想引起问题的那一行是.Formula`=“ = Dados!E(i)”,我打算成为一个变量吗? as it is written it is not. 正如它写的那样。 I know you assign it in the previous sheet, but for this sheet it is not. 我知道您在上一张纸中分配了它,但对于这张纸却没有。

Perhaps try: .Formula = "=Dados!E(" & i & ")" 也许尝试: .Formula = "=Dados!E(" & i & ")"

First off I would get the variable declarations in line with the scope of a modern worksheet. 首先,我将使变量声明与现代工作表的范围一致。 It may not matter now but it is a good practise to get into. 现在可能并不重要,但这是一个很好的习惯。

Dim ws As Worksheet, GI As Long, GF As Long, J As Long

Next, the construction of cell ranges from string constants and numerical variables does not look correct. 接下来,从字符串常量和数字变量构成的单元格范围看起来不正确。 You are trying to create something like =Dados!E99 not =Dados!E(i) when i has the value of 99 . 的值为99时,您正在尝试创建=Dados!E99而不是=Dados!E(i)之类的东西。

    With .Shapes("PIT")
        .Formula = "=Dados!E" & i
        ...
    End With
    With .ChartObjects("Gráfico 8")
        ...
        ActiveChart.FullSeriesCollection(1).Values = "=Dados!$e$" & GI & ":$e$" & GF
        ActiveChart.FullSeriesCollection(1).XValues = "=Dados!$b$" & GI & ":$c$" & GF
        ...
    End with

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM