Code from the book Excel 2016 VBA and Macros (MrExcel Library) not understood

Stan76

New Member
Joined
Aug 19, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Dear Excel users.
I have come across a code from the book Excel 2016 VBA and Macros (MrExcel Library). The code is written by Zack Barresse and is about Copying Data to Separate Worksheets Without using Filter.

The code is the following :
VBA Code:
Sub CriteriaRange_Copy()
Dim Table As ListObject
Dim SortColumn As ListColumn
Dim CriteriaColumn As ListColumn
Dim FoundRange As Range
Dim TargetSheet As Worksheet
Dim HeaderVisible As Boolean

Set Table = ActiveSheet.ListObjects(1) ' Set as desired
HeaderVisible = Table.ShowHeaders
Table.ShowHeaders = True
On Error GoTo RemoveColumns
Set SortColumn = Table.ListColumns.Add(Table.ListColumns.Count + 1)
Set CriteriaColumn = Table.ListColumns.Add(Table.ListColumns.Count + 1)
On Error GoTo 0

'Add a column to keep track of the original order of the records
SortColumn.Name = " Sort"
CriteriaColumn.Name = " Criteria"
SortColumn.DataBodyRange.Formula = "=ROW(A1)"
SortColumn.DataBodyRange.Value = SortColumn.DataBodyRange.Value


CriteriaColumn.DataBodyRange.Formula = "=1/(([@Units]<10)*([@Cost]<10))"
CriteriaColumn.DataBodyRange.Value = CriteriaColumn.DataBodyRange.Value

Table.Range.Sort Key1:=CriteriaColumn.Range(1, 1), Order1:=xlAscending, Header:=xlYes

On Error Resume Next
Set FoundRange = Intersect(Table.Range, CriteriaColumn.DataBodyRange.SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow)

On Error GoTo 0
If Not FoundRange Is Nothing Then
    Set TargetSheet = ThisWorkbook.Worksheets.Add(After:=ActiveSheet)
    FoundRange(1, 1).Offset(-1, 0).Resize(FoundRange.Rows.Count + 1, FoundRange.Columns.Count - 2).Copy
    TargetSheet.Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
    Application.CutCopyMode = False
End If

Table.Range.Sort Key1:=SortColumn.Range(1, 1), Order1:=xlAscending, Header:=xlYes

RemoveColumns:
If Not SortColumn Is Nothing Then SortColumn.Delete
If Not CriteriaColumn Is Nothing Then CriteriaColumn.Delete
Table.ShowHeaders = HeaderVisible
End Sub

There are two lines I do not understand :
SortColumn.DataBodyRange.Value = SortColumn.DataBodyRange.Value
CriteriaColumn.DataBodyRange.Value = CriteriaColumn.DataBodyRange.Value

What is the purpose of those lines ? I commented them and the code failed. I commented only "SortColumn.DataBodyRange.Value = SortColumn.DataBodyRange.Value" and the code worked. The line "CriteriaColumn.DataBodyRange.Value = CriteriaColumn.DataBodyRange.Value" seems to be critical. I do not understand why we set something like "A=A".
Thank you for your help
Best regards,
Stan
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Both lines change any Formulas to Values in the range, it is just a more efficient way of doing it than doing a Copy and PasteSpecial xlValues
 
Upvote 0
Solution
Thank you for your answer. Ok I just checked using F8 and when this line executes, it "converts" the formula into the value. thank you for your help !
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,853
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top