简体   繁体   English

为什么当我对 vba 使用相同的代码、相同的数据但不同的笔记本电脑时得到不同的输出?

[英]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.

相关问题 如何为30种不同的Excel工作簿运行相同的宏,VBA代码? - How do I run the same macro, VBA-code for 30 different excel workbooks? 同一单元格中的多个vlookup,其值来自不同的下拉列表。 不使用其中一个下拉菜单时,如何获得空白? - Multiple vlookups in the same cell, with their values coming from different drop down lists. How do I get a blank when one of the dropdowns isnt used? 为什么运行同一段VBA代码会花费不同的时间? - Why running the same piece of VBA code takes different amount of time? 下面的VBA代码填充同一工作表中的文件名列表,但是我希望将结果数据集生成到另一个工作表中吗? - The below VBA code populates list of filenames in the same sheet, but I want the result dataset to be generated into a different worksheet? 在相同的不同工作表上复制 VBA 代码 - Duplicating a VBA code on different Sheets in same 如何编辑此 VBA 代码以接受不同的值? - How do i edit this VBA code to accept different values? VBA-Excel从不同位置的不同工作表获取相同的数据 - VBA - Excel Getting same data from different sheets at different positions 如何将我的VBA数据透视表行数据分成两列而不是同一列? - How do I get my vba pivot table row data into two columns instead of the same column? 如何让这个 VBA 代码输出为特定的数字格式? - How do I get this VBA code to output to a specific number format? 键入ALT +(数字)时,如何使用Excel VBA输出完全相同的字符? - How can I use Excel VBA to output the exact same characters I get when type ALT+(number)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM