Some further notes and information for this Thread:
http://www.excelfox.com/forum/showth...1476#post11476
http://www.excelfox.com/forum/showth...ulation-by-vba
https://www.mrexcel.com/forum/excel-...ation-vba.html
http://www.vbaexpress.com/forum/show...Formula-by-vba
https://www.excelforum.com/excel-pro...on-by-vba.html
There are many different was to achieve the same…
Some further notes on changes that can be made to Sub Vixer9a() from here: http://www.excelfox.com/forum/showth...ll=1#post11475
b) Rng.Value = Rng.Value
Excel VBA has been written such that applying .Value to a cell or cells has a similar effect to writing manually into a cell.
So if you do this:
Range("A1").Value = "X"
, it will write
X
in the first cell.
You will then see in the first cell this
X
If you do this:
Range("A1").Value = " = 1"
, it will write
= 1
in the first cell.
But, you will see in the first cell just the Value:
1
To explain:
The .Value Property of a range object , for example a single cell, is what we "see" in the cell. But if you apply .Value to a cell it will write into the cell as if you did it manually.
So, in your code you can replace this: …_
Code:
'3(iii) I need only result in the cell no formulas
Ws1.Range("D2:D" & Lr1 & "").Copy
Ws1.Range("D2:D" & Lr1 & "").PasteSpecial Paste:=xlPasteValues
Let Application.CutCopyMode = False
_.. with this:
Code:
'3(iii) I need only result in the cell no formulas
Let Ws1.Range("D2:D" & Lr1 & "").Value = Ws1.Range("D2:D" & Lr1 & "").Value
Code:
Sub Vixer9b() ' demo for rng.Value = rng.Value
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 ..."....
'3(i) ....Multiply the value of B2 by 1.5%, then multiply that result by 56 and then paste the result in D2.. formula will be added by me in the code, put that formula in
Ws1.Range("D2").Value = "=B2*(1.5/100)*56"
'3(ii) ....drag it
Ws1.Range("D2").AutoFill Destination:=Ws1.Range("D2:D" & Lr1 & ""), Type:=xlFillDefault
'3(iii) I need only result in the cell no formulas
Let Ws1.Range("D2:D" & Lr1 & "").Value = Ws1.Range("D2:D" & Lr1 & "").Value
Rem 4 save it and close it
Wb1.Save
Wb1.Close
End Sub
_c) Apply "fixed vector"** form across a range
I can apply the formula in its "fixed vector"** form across a range. In other words I can apply the same formula in its fixed vector form across a range. Applying the same fixed vector formula across a range will make any referred to cells change the shown formula appropriately to apply to the different cells
** In simplified terms, "fixed vector", means notation without the $. So..
A1 is "fixed vector"
$A$1 is absolute referencing
So we can replace this:
Code:
'3(i) ....Multiply the value of B2 by 1.5%, then multiply that result by 56 and then paste the result in D2.. formula will be added by me in the code, put that formula in
Ws1.Range("D2").Value = "=B2*(1.5/100)*56"
'3(ii) ....drag it
Ws1.Range("D2").AutoFill Destination:=Ws1.Range("D2:D" & Lr1 & ""), Type:=xlFillDefault
With this:
Code:
'3(i)(ii) ....Multiply the value of B2 by 1.5%, then multiply that result by 56 and then paste the result in D2.. ....drag it formula will be added by me in the code, put that formula in
Ws1.Range("D2:D" & Lr1 & "").Value = "=B2*(1.5/100)*56"
Code:
Sub Vixer9c() ' demo for fixed vector applied across a 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 ..."....
'3(i)(ii) ....Multiply the value of B2 by 1.5%, then multiply that result by 56 and then paste the result in D2.. ....drag it formula will be added by me in the code, put that formula in
Ws1.Range("D2:D" & Lr1 & "").Value = "=B2*(1.5/100)*56"
'3(iii) I need only result in the cell no formulas
Let Ws1.Range("D2:D" & Lr1 & "").Value = Ws1.Range("D2:D" & Lr1 & "").Value
Rem 4 save it and close it
Wb1.Save
Wb1.Close
End Sub
_d) Internal calculation with VBA arrays
We do not need to put formulas into any cells.
We can do the calculations internally, within coding, and then paste all the values out in one go.
Using VBA arrays is a convenient way to do this.
_a) First we bring all the data into an array.
_b) Then we do the calculations
_c) Finally we paste out all the calculated values in one go
We can replace all of Rem3 with new coding
Rem 3 The Process .. using VBA arrays
'3_a) First we bring all the data into an array.
'3_b) Now we do the calculations
'3_c) Finally we paste out all the calculated values in one go
Code:
Rem 3 The Process ...using VBA arrays
'3_a) First we bring all the data into an array. (We also take in the column D values, even if the column D is empty)
Dim arrDta() As Variant
Let arrDta() = Ws1.Range("A1:D" & Lr1 & "").Value
'3_b) Now we do the calculations looping through the row data held internally in the data array, arrDta()
Dim Cnt As Long
For Cnt = 2 To Lr1
Let arrDta(Cnt, 4) = arrDta(Cnt, 2) * (1.5 / 100) * 56 ' .. like.. column "D" = column "B" * (1.5/100) * 56
Next Cnt
'3_c) Finally we paste out all the calculated values ( and also the original data ) in one go
Ws1.Range("A1:D" & Lr1 & "").Value = arrDta()
Code:
Sub Vixer9d() ' demo using VBA arrays
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 VBA arrays
'3_a) First we bring all the data into an array. (We also take in the column D values, even if the column D is empty)
Dim arrDta() As Variant
Let arrDta() = Ws1.Range("A1:D" & Lr1 & "").Value
'3_b) Now we do the calculations looping through the row data held internally in the data array, arrDta()
Dim Cnt As Long
For Cnt = 2 To Lr1
Let arrDta(Cnt, 4) = arrDta(Cnt, 2) * (1.5 / 100) * 56 ' .. like.. column "D" = column "B" * (1.5/100) * 56
Next Cnt
'3_c) Finally we paste out all the calculated values ( and also the original data ) in one go
Ws1.Range("A1:D" & Lr1 & "").Value = arrDta()
Rem 4 save it and close it
Wb1.Save
Wb1.Close
End Sub
_e) Evaluate Range
see next post:
Bookmarks