简体   繁体   中英

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. For instance:

在此处输入图像描述

Before I press the code, it was like this. After pressing it with the laptop with excel 2010, it gives this output:

在此处输入图像描述

However when I used the laptop that has excel 2016 it gives me the ideal output which is something like this:

在此处输入图像描述

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.

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.

Adding the INDEX is necessary for older versions of Excel to evaluate the ABS of each cell in the range.

Added this comment in the original question as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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