[英]Why do i get different output when i used the same code, same data but different laptops for vba?
I am trying to make an entire columns of cells absolute when the cells are below the header "Vbd" and I faced this problem: when I use the same code and same data on different laptops (one is excel 2010 and the other one is excel 2016), it gives different output.当单元格位于标题“Vbd”下方时,我试图使一整列单元格成为绝对单元格,我遇到了这个问题:当我在不同的笔记本电脑上使用相同的代码和相同的数据时(一个是 excel 2010,另一个是 excel 2016),它给出了不同的输出。 For instance:
例如:
Before I press the code, it was like this.在我按下代码之前,它是这样的。 After pressing it with the laptop with excel 2010, it gives this output:
用带有 excel 2010 的笔记本电脑按下它后,它会给出以下输出:
However when I used the laptop that has excel 2016 it gives me the ideal output which is something like this:但是,当我使用具有 excel 2016 的笔记本电脑时,它为我提供了理想的输出,如下所示:
The thing is I used the same data and same code for these two laptops (I have checked several times that both codes and data are the same) and I am super confused why the output is different.问题是我对这两台笔记本电脑使用了相同的数据和相同的代码(我已经多次检查代码和数据是否相同)而且我非常困惑为什么输出不同。 Below is my code and the workbook can be found here.
下面是我的代码,工作簿可以在这里找到。(Dropbox)
(投递箱)
Option Explicit
Sub testing1()
Dim i As Long
Dim LastColumn As Long
Dim sht As Worksheet
Dim rngToAbs As Range
Dim lastrow As Long
Set sht = ThisWorkbook.Sheets("Sheet1")
LastColumn = sht.Cells(1, sht.Columns.Count).End(xlToLeft).Column
lastrow = sht.Cells(sht.Rows.Count, "D").End(xlUp).Row
For i = 1 To LastColumn
With sht
If sht.Cells(1, i).Value = "Vbd" Then
Set rngToAbs = .Range(sht.Cells(2, i), sht.Cells(lastrow, i))
rngToAbs.Value = .Evaluate("=abs(" & rngToAbs.Address & ")")
End If
End With
Next
End Sub
Answer provided by @ScottCraner but posting for future reference. @ScottCraner 提供的答案,但发布以供将来参考。
Change改变
.Evaluate("=abs(" & rngToAbs.Address & ")")
to到
.Evaluate("=INDEX(abs(" & rngToAbs.Address & "),)")
The current version works because the formula is evaluated as an array formula, if you have the latest version of Excel.当前版本有效,因为如果您有最新版本的 Excel,公式将作为数组公式求值。
Adding the INDEX
is necessary for older versions of Excel to evaluate the ABS of each cell in the range.旧版本的 Excel 需要添加
INDEX
来评估范围内每个单元格的 ABS。
Added this comment in the original question as well.在原始问题中也添加了此评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.