VBA计算Excel中列之间的距离

发布于 2024-10-19 10:19:29 字数 873 浏览 3 评论 0原文

我决定通过例子来问这个问题。我试图解释整个项目,但如果您尚未从事同一个项目,那么看起来会很混乱,所以我们开始吧。

作为我正在编写的代码的用户,您需要决定希望程序实际抓取哪些列以及将所有计算写入何处。用户可以使用的总共九列,不过我将重点关注前四列,这样它就不会变得太复杂。

前两列是我的程序进行计算所需的列(稍后解释),并且实际上是整个项目运行的基础。我们将它们称为 Data_Before 和 Data_After。

接下来的两列是根据 Data_Before 和 Data_After 中的信息完成的计算。这两列是差异和百分比。就像标题所示,Diff 是 Data_After - Data_Before,Percent 是 Diff / Data_Before。

截至目前,这些列必须彼此相邻。因此,如果 Data_Before 位于“A”列中,则 Data_After 必须出现在“B”中,Diff 出现在“C”中,Percent 出现在“D”中。这是因为我的计算来自记录宏函数。填充 Diff 和 Percent 单元格的代码如下。

' Diff column
ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]" 
' Percent column
ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-3]" 

抱歉代码中的颜色很奇怪,“Diff 列”和“Percent 列”是注释。无论如何,这里有 FormulaR1C1,这使得程序不灵活。问题是,如何找到 Diff 列与 Data_Before 或 Data_After 列之间的距离,以便用户可以将 Data_Before 放在“A”列中,将 Data_After 放在“B”列中,而将 Diff 和 Percent 列放在任何他们想要的地方?

抱歉问题太长了。

I've decided to ask this question through examples. I tried to explain the entire project but it seemed confusing if you weren't already working on the same project so here we go.

As a user of the code I'm writing you need to decide which columns you want the program to actually grab and where to write all of the calculations to. There are nine columns total that the user can play with, though I'll focus on the first four so it doesn't get too complex.

The first two are the columns that my program needs in order to do it's calculations (explained later) and is really the basis for the entire project to run. Let's call them Data_Before and Data_After.

The next two columns are the calculations done from the information in Data_Before and Data_After. These two columns are Diff and Percent. Like the headers suggest Diff is Data_After - Data_Before and Percent is Diff / Data_Before.

As of now the columns have to be right next to each other. So if Data_Before is in column "A" then Data_After has to appear in "B" with Diff in "C" and Percent in "D". This is because of my calculations coming from the record macro function. The code that populates a cell in Diff and Percent are below

' Diff column
ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]" 
' Percent column
ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-3]" 

Sorry for the weird coloring in the code, the "Diff column" and "Percent column" are comments. Anyway, here we have the FormulaR1C1 which makes the program inflexible. The question being, how to do find the distance between the Diff column and the Data_Before or Data_After column so that the user can have the Data_Before in column "A" and Data_After in column "B" but the Diff and Percent columns wherever they want?

Sorry for the length of the question.

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

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

发布评论

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

评论(2

做个ˇ局外人 2024-10-26 10:19:29

我在完成以下内容后才看到您对程序设置的评论,希望它能以某种方式提供帮助。

Sub InsertDiffPercentCalc(ByVal rngDataBeforeColumn As Excel.Range, ByVal rngDataAfterColumn As Excel.Range, ByVal rngTargetDiffColumn As Excel.Range, ByVal rngTargetPercentColumn As Excel.Range)

' Diff column
rngTargetDiffColumn.FormulaR1C1 = "=RC" & CStr(rngDataAfterColumn.Column) & "-RC" & CStr(rngDataBeforeColumn.Column)
' Percent column
rngTargetPercentColumn.FormulaR1C1 = "=RC" & CStr(rngTargetDiffColumn.Column) & "/RC" & CStr(rngDataBeforeColumn.Column)

End Sub

根据您的评论,您可以创建一个无参数 Sub,但无论如何您仍然必须确定目标列(或至少是它们的地址) - 您是如何做到这一点的? 编辑刚刚看到您的其他评论。

Sub InsertDiffPercentCalc()

Dim rngDataBeforeColumn As Excel.Range
Dim rngDataAfterColumn As Excel.Range
Dim rngTargetDiffColumn As Excel.Range
Dim rngTargetPercentColumn As Excel.Range

Set wksSettings = ThisWorkbook.Sheets("<settings sheet name>")
Set wksTarget = Application.Workbooks("<target workbook name>").Sheets("<target sheet name>")

Set rngDataBeforeColumn = wksTarget.Range(wksSetting.range("A2").Value & ":" & wksSetting.range("A2").Value)
Set rngDataAfterColumn = wksTarget.Range(wksSetting.range("B2").Value & ":" & wksSetting.range("B2").Value)
Set rngTargetDiffColumn = wksTarget.Range(wksSetting.range("C2").Value & ":" & wksSetting.range("C2").Value)
Set rngTargetPercentColumn = wksTarget.Range(wksSetting.range("D2").Value & ":" & wksSetting.range("D2").Value)

' Diff column
rngTargetDiffColumn.FormulaR1C1 = "=RC" & CStr(rngDataAfterColumn.Column) & "-RC" & CStr(rngDataBeforeColumn.Column)
' Percent column
rngTargetPercentColumn.FormulaR1C1 = "=RC" & CStr(rngTargetDiffColumn.Column) & "/RC" & CStr(rngDataBeforeColumn.Column)

End Sub

其中 wksSetting 是设置表的名称,wksTarget 是目标(输出)表的名称。

I only just saw your comment on the set-up of your program after I'd finished the below, hope it can help in some way.

Sub InsertDiffPercentCalc(ByVal rngDataBeforeColumn As Excel.Range, ByVal rngDataAfterColumn As Excel.Range, ByVal rngTargetDiffColumn As Excel.Range, ByVal rngTargetPercentColumn As Excel.Range)

' Diff column
rngTargetDiffColumn.FormulaR1C1 = "=RC" & CStr(rngDataAfterColumn.Column) & "-RC" & CStr(rngDataBeforeColumn.Column)
' Percent column
rngTargetPercentColumn.FormulaR1C1 = "=RC" & CStr(rngTargetDiffColumn.Column) & "/RC" & CStr(rngDataBeforeColumn.Column)

End Sub

Based on your comment, you can create a parameterless Sub, but in any case you still have to determine the target columns (or at least their addresses) - how are you doing this? edit just seen your other comment.

Sub InsertDiffPercentCalc()

Dim rngDataBeforeColumn As Excel.Range
Dim rngDataAfterColumn As Excel.Range
Dim rngTargetDiffColumn As Excel.Range
Dim rngTargetPercentColumn As Excel.Range

Set wksSettings = ThisWorkbook.Sheets("<settings sheet name>")
Set wksTarget = Application.Workbooks("<target workbook name>").Sheets("<target sheet name>")

Set rngDataBeforeColumn = wksTarget.Range(wksSetting.range("A2").Value & ":" & wksSetting.range("A2").Value)
Set rngDataAfterColumn = wksTarget.Range(wksSetting.range("B2").Value & ":" & wksSetting.range("B2").Value)
Set rngTargetDiffColumn = wksTarget.Range(wksSetting.range("C2").Value & ":" & wksSetting.range("C2").Value)
Set rngTargetPercentColumn = wksTarget.Range(wksSetting.range("D2").Value & ":" & wksSetting.range("D2").Value)

' Diff column
rngTargetDiffColumn.FormulaR1C1 = "=RC" & CStr(rngDataAfterColumn.Column) & "-RC" & CStr(rngDataBeforeColumn.Column)
' Percent column
rngTargetPercentColumn.FormulaR1C1 = "=RC" & CStr(rngTargetDiffColumn.Column) & "/RC" & CStr(rngDataBeforeColumn.Column)

End Sub

where wksSetting is the name of your settings sheet and wksTarget is the name of your target (output) sheet.

给妤﹃绝世温柔 2024-10-26 10:19:29

根据您的评论,您似乎确切知道将输出放在哪里。正确的?因此,如果 B 列是之后的数据,A 列是之前的数据,并且用户想要 G 列中的 diff 列,您可以将其用于您的 diff 公式:

Range("G1").Formula = "=B1-A1"

还是我误解了?

编辑

您可能会遇到这样的事情:

sheet1.Columns(15).column - sheet1.Columns(13).column ' returns 2

您可以使用它来创建 R1C1 公式。

Per your comment, it seems you know exactly where to put the output. Right? So, if column B was data after and column A was data before and the user wanted the diff column in column G, you could use this for your diff formula:

Range("G1").Formula = "=B1-A1"

Or am I misunderstanding?

Edit

You're probably after something like this:

sheet1.Columns(15).column - sheet1.Columns(13).column ' returns 2

You could use that to create an R1C1 formula.

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