有没有办法找到并找到替换 Excel 2010 中的单元格引用值?编程语言?

发布于 2024-12-09 06:01:02 字数 432 浏览 0 评论 0原文

我有一个 Excel 工作表,其中包含一堆根据从工作簿中的另一个工作表中提取的数据创建的图表。每个月初,当我将上个月的数据添加到数据工作表时,我会将所有内容移到一列中,这样上个月的数据现在位于 O 列中,而现在的数据则位于 O 列中。 上个月总是在 P 列中。问题是,我希望带有图表的工作表始终绘制当前的“上个月”(始终在 P 列),但它足够“智能”,可以将图表重新配置为继续将数据绘制成图表图表(即之前的“上个月”),现在位于 O 列中。

具体来说,我想更改(例如)
=SERIES("访问次数",数据!$D$4:$O$4,数据!$D$4:$O$4,1)

$D$4:$P$4,1)

=SERIES("访问",数据!$D$4:$P$4,数据! Replace 似乎无法做到这一点。用VBA可以吗? (我是个十足的 VBA 菜鸟。)

I have an Excel worksheet that contains a bunch of charts created from data pulled from another worksheet in the workbook. At the beginning of each month, when I add last month's data to the data worksheet, I shift everything over one column such that what was last month is now in column O, and what is now last month is always in column P. Problem is, I want the worksheet with the charts to always chart the current "last month" (always column P), but it's "smart" enough to re-configure the charts to keep charting the data it had been charting (i.e., the previous "last month"), which is now in column O.

Specifically, I'd like to change (for example)
=SERIES("Visits",Data!$D$4:$O$4,Data!$D$4:$O$4,1)
to
=SERIES("Visits",Data!$D$4:$P$4,Data!$D$4:$P$4,1)

Find & Replace doesn't seem capable of doing that. Is it possible with VBA? (I'm a total VBA noob.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清音悠歌 2024-12-16 06:01:02

我认为这里的问题是你如何“转移”数据。如果您要删除/插入或移动数据,Excel 将更新其引用。您可以复制数据并将其粘贴到左侧 1 列,然后将当前的“上个月”数据粘贴到 P 列中。

简而言之,复制和粘贴不会强制 Excel 更新其指针,移动/删除/插入会。

如果这不是一个选项,请查看 OFFSET 函数 - 如果您可以将其合并到数据范围中,您也许可以做您想做的事情。

I think the problem here is how you're "shifting" the data. If you are deleting/inserting or moving data then Excel will update its references. You could copy the data and paste it 1 column to the left, and then paste your current 'last month' data into column P.

In short, copying and pasting will not force Excel to update its pointers, moving/deleting/inserting will.

If this isn't an option look into the OFFSET function - if you can incorporate that into the data-range you might be able to do what you want.

冷心人i 2024-12-16 06:01:02

我建议使用动态范围,它会在添加数据时自动扩展范围,请参阅 Jon Peltier 的此示例< /a> 首先

I would suggest using dynamic ranges which automatically extend your range as data is added, see this example from Jon Peltier for a start

棒棒糖 2024-12-16 06:01:02

没有内置方法可以对级数公式执行查找替换,但您可以编写 VBA 代码来执行此操作。我写了一个关于此的教程,更改系列公式 - 改进的例程。我展示了许多 VBA 例程,但最简单的是下面的。

以下子命令检查活动图表,然后提示用户输入“更改内容”和“更改内容”字符串(在您的情况下为 $O$ 和 $P$),然后更改活动图表中的所有系列公式。

Sub ChangeSeriesFormula()
    ''' Just do active chart
    If ActiveChart Is Nothing Then
        '' There is no active chart
        MsgBox "Please select a chart and try again.", vbExclamation, _
            "No Chart Selected"
        Exit Sub
    End If

    Dim OldString As String, NewString As String, strTemp As String
    Dim mySrs As Series

    OldString = InputBox("Enter the string to be replaced:", "Enter old string")

    If Len(OldString) > 1 Then
        NewString= InputBox("Enter the string to replace " & """" _
            & OldString & """:", "Enter new string")
        '' Loop through all series
        For Each mySrs In ActiveChart.SeriesCollection
            strTemp = WorksheetFunction.Substitute(mySrs.Formula, _
                OldString, NewString)
            mySrs.Formula = strTemp
        Next
    Else
        MsgBox "Nothing to be replaced.", vbInformation, "Nothing Entered"
    End If
End Sub

您可以提取该例程的核心,并从循环遍历工作表上的所有图表、工作簿中的所有图表等的其他例程中调用它。

There is no built-in way to perform find-replace on series formulas, but you can write VBA code to do it. I've written a tutorial about this, Change Series Formula – Improved Routines. I show a number of VBA routines, but the simplest is below.

The following sub checks for an active chart, then prompts the user for 'change what' and 'to what' strings, $O$ and $P$ in your case, and then changes all series formulas in the active chart.

Sub ChangeSeriesFormula()
    ''' Just do active chart
    If ActiveChart Is Nothing Then
        '' There is no active chart
        MsgBox "Please select a chart and try again.", vbExclamation, _
            "No Chart Selected"
        Exit Sub
    End If

    Dim OldString As String, NewString As String, strTemp As String
    Dim mySrs As Series

    OldString = InputBox("Enter the string to be replaced:", "Enter old string")

    If Len(OldString) > 1 Then
        NewString= InputBox("Enter the string to replace " & """" _
            & OldString & """:", "Enter new string")
        '' Loop through all series
        For Each mySrs In ActiveChart.SeriesCollection
            strTemp = WorksheetFunction.Substitute(mySrs.Formula, _
                OldString, NewString)
            mySrs.Formula = strTemp
        Next
    Else
        MsgBox "Nothing to be replaced.", vbInformation, "Nothing Entered"
    End If
End Sub

You can extract the core of this routine and call it from other routines that cycle through all charts on a worksheet, all charts in a workbook, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文