_e) Evaluate Range
It is possible to get array type calculations in Excel. Nobody fully understands this topic, and a lot of things are found by chance to work in a way such as to do array type calculations, or rather , array type results can be obtained.
Evaluate Range techniques often allow a looping process to be replaced ba a single line of code. Broadly this arises due to two things:
_1) Excel frequently updates all cells in a spreadsheet by going across the columns in a row , then down a row, then across the columns in the next row … etc.
Usually a user "using" a single cell is like when it selected, and/ or the carriage Return key is used, and so it appears to us as if the cell is Updated and displayed at one time. There are various ways to display more than one cell in a single spreadsheet update.
_2) In VBA there is an Evaluate Method ( https://docs.microsoft.com/en-us/off...ation.evaluate ). In simplified terms, this allows calculation within VBA as if the calculations were written and done in a spreadsheet.
It is possible sometimes to get the Evaluate function to return an array representing the calculations across a range
There is no clear documentation on any of the array type things discussed in this post, and it is often suggested that getting array results in any form in Excel has occurred by chance and no one understands fully what is going on.
As an example, considering the last macro which looped to produce an array based on doing these calculations of this form, from down rows of 2 to Lr1
B2*(1.5/100)*56
B3*(1.5/100)*56
B4*(1.5/100)*56
_…. etc.
We find that Rem 3 from the last macro, Sub Vixer9d() , can be replaced by
Code:Rem 3 The Process ... using Evaluate Range Ws1.Range("D2:D" & Lr1 & "").Value = Ws1.Evaluate("=" & Range("B2:B" & Lr1 & "").Address & "*(1.5/100)*56")
The purpose of ("=" & Range("B2:B" & Lr1 & "") is to give us the formula form of like
=B2:B10
Hence the Range used does not need to be Qualified, such as by a worksheet, like in Ws1.Range
( There is an alternative form of Evaluate(" __ ") , which is often referred to as the "shorthand form" of Evaluate(" __ ") . It looks like this _ [ ___ ] _ . So you may now see what Mark L was suggesting here: https://www.excelforum.com/excel-pro...ml#post5190685 )
It is , however , important to qualify Evaluate. this is because we want to do an evaluation as if the formula within Evaluate(" ___ ") , was in the cell in worksheet, Ws1. If we omit the qualifying _ Ws1. _ , before the Evaluate , then we may do an evaluation of the formula in a different worksheet.
Code:Sub Vixer9e() ' demo for Evaluate Range Rem 1 Workbook and worksheets info '1a) Workbook info ' Dim Wbm As Workbook: Set Wbm = ThisWorkbook ' The workbook containing macro Dim Wb1 As Workbook ' This will be set later when the workbook is opened Dim MyPath As String: Let MyPath = "C:\Users\sk\Desktop" ' ".....The file will be located in C:\Users\sk\Desktop .... Dim strWb1 As String: Let strWb1 = "sample.xlsx" ' " ....and file name is sample.xlsx '1b) Worksheets info Dim Ws1 As Worksheet ' This will be set later when the workbook is opened) Dim Lr1 As Long ' Let Lr1 = 10 for sample file , but we will determine it dynamically after opening the file Rem 2 Open file "..... file is not opened so we have to open the file by vba ' Workbooks.Open Filename:="F:\Excel0202015Jan2016\ExcelFox\vixer\sample.xlsx" Workbooks.Open Filename:=ThisWorkbook.Path & "\" & strWb1 ' ...both files are located in same place ' Workbooks.Open Filename:=MyPath & "\" & strWb1 ' ...file will be located in C:\Users\sk\Desktop Set Wb1 = ActiveWorkbook ' The workbook just opened will now be the current active workbook Set Ws1 = Wb1.Worksheets.Item(1) ' make Lr1 dynamic .... http://www.excelfox.com/forum/showthread.php/2345-Appendix-Thread-(-Codes-for-other-Threads-HTML-Tables-etc-)?p=11474&viewfull=1#post11474 Let Lr1 = Ws1.Range("C" & Ws1.Rows.Count).End(xlUp).Row Rem 3 The Process ... using Evaluate Range Ws1.Range("D2:D" & Lr1 & "").Value = Ws1.Evaluate("=" & Range("B2:B" & Lr1 & "").Address & "*(1.5/100)*56") Rem 4 save it and close it Wb1.Save Wb1.Close End Sub
Using Evaluate often results in a much shorter coding.
For example, taking Sub Vixer9e() , and making a few other simplifications we can come up with a much shorter coding.
Code:Sub Vixer9f() ' simplified coding ( using Range Evaluate ) Workbooks.Open Filename:=ThisWorkbook.Path & "\" & "sample.xlsx" ' ...both files are located in same place Rem 3 The Process ... using Evaluate Range ActiveSheet.Range("D2:D" & ActiveSheet.Range("C" & ActiveSheet.Rows.Count).End(xlUp).Row & "").Value = ActiveSheet.Evaluate("=" & Range("B2:B" & ActiveSheet.Range("C" & ActiveSheet.Rows.Count).End(xlUp).Row & "").Address & "*(1.5/100)*56") Rem 4 save it and close it ActiveWorkbook.Close savechanges:=True End Sub
I personally do not like such coding because
_(i) They are more difficult to understand, especially at a later date,
_(ii) They are less flexible for adjustment.
_(iii) There may be some missing detail which might cause the coding to fail sometimes in certain circumstances




Reply With Quote
Bookmarks