Excel Sheet too Large - Need to redefine Range

CCSlice

New Member
Joined
Feb 11, 2022
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
Hi Mr. Excel Nation!

I am working with a large spreadsheet and the truth is that all the columns are not required for what the client wants to be done. I have attached a snip of the spreadsheet here but know that in reality this spreadsheet is about 30 columns and over 10,000 rows. I have highlighted the columns in red that I need in order to carry out the opening and updating of the reports located in SharePoint. The easy answer is to reduced the number of rows that is brought into Excel using Power Query, BUT the client, needs every column to be visible. Can I create an array of the columns I need rather than the whole sheet?

Report_Snip.xlsx
ABCDEFGHIJKLMNOPQRSTUVW
1LocationReport NameBusiness UnitLinkStatusPriorityCommentsSupportTeam LeadRefresh CycleChoice 1Choice 2Date AppliedOlinkElinkAlinkUpdatedUpdated ByAudienceIDCountPathChecked
2North Buildingabc.xlsxMarketinghttps://sharepoint.com/Marketing/abc.xlsxActiveLowStaffTom FieldingWeekly4/21/2022CLASSIFIEDCLASSIFIEDCLASSIFIED04/21/2022 11:45 AMTom Fielding145321CLASSIFIEDYes
3Central BuildingEmployee_Asset.xlsxAdminhttps://sharepoint.com/Admin/Employee_Asset.xlsxActiveMedDeptSuzanne DurrantWeekly4/24/2022CLASSIFIEDCLASSIFIEDCLASSIFIED04/24/2022 9:30 AMMichelle Fennings194521CLASSIFIEDYes
4DMZEnd Point Mappings.xlsxIThttps://sharepoint.com/IT/End Point Mappings.xlsxActiveHighDeptHenry NguyenBi-Weekly4/11/2022CLASSIFIEDCLASSIFIEDCLASSIFIED04/11/2022 3:30 PMLazar Popovic124491CLASSIFIEDYes
5
Sheet1
Cell Formulas
RangeFormula
D2:D4D2=HYPERLINK("https://sharepoint.com/"&C2&"/"&B2)
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Can I create an array of the columns I need rather than the whole sheet?

Certainly!

As an example of what you posted, and assuming the B Column to be the longest column, You could use something like the following to load just certain columns of the sheet into a 2D 1based array:

VBA Code:
Sub LoadNoncontiguousColumnsToArray()
'
    Dim DataLastRow             As Long, DataStartRow   As Long
    Dim DesiredInputColumns     As String
    Dim LastRowColumn           As String
    Dim InputArray              As Variant
'
    DataStartRow = 2                                                                    ' <--- Set this to the start row of data
    DesiredInputColumns = "2,4,5,10,11,14,15,16"                                        ' <--- Set the column #s from source sheet to be loaded into array
    LastRowColumn = "B"                                                                 ' <--- Set this to the column letter to use to determine last row
'
    DataLastRow = Range(LastRowColumn & Rows.Count).End(xlUp).Row                       ' Get last row used in Column
'
    InputArray = Application.Index(Cells, Evaluate("ROW(" & DataStartRow & ":" & _
            DataLastRow & ")"), Split(DesiredInputColumns, ","))                        ' Load the designated column #s into 2D 1 based InputArray
End Sub
 
Upvote 0
Solution
Certainly!

As an example of what you posted, and assuming the B Column to be the longest column, You could use something like the following to load just certain columns of the sheet into a 2D 1based array:

VBA Code:
Sub LoadNoncontiguousColumnsToArray()
'
    Dim DataLastRow             As Long, DataStartRow   As Long
    Dim DesiredInputColumns     As String
    Dim LastRowColumn           As String
    Dim InputArray              As Variant
'
    DataStartRow = 2                                                                    ' <--- Set this to the start row of data
    DesiredInputColumns = "2,4,5,10,11,14,15,16"                                        ' <--- Set the column #s from source sheet to be loaded into array
    LastRowColumn = "B"                                                                 ' <--- Set this to the column letter to use to determine last row
'
    DataLastRow = Range(LastRowColumn & Rows.Count).End(xlUp).Row                       ' Get last row used in Column
'
    InputArray = Application.Index(Cells, Evaluate("ROW(" & DataStartRow & ":" & _
            DataLastRow & ")"), Split(DesiredInputColumns, ","))                        ' Load the designated column #s into 2D 1 based InputArray
End Sub
This is good. Can the array be filled upon a condition? For example, column E is filled with Inactive and Active reports. No sense in loading inactive reports into the array. So could the array be populated when the report shows as active?
 
Upvote 0
It would take longer to look through Column E for 'Active' status as opposed to just loading that entire column into the array and then check the status of that column later in your code.

Each time you try to read/write from/to a sheet takes time. It is way way faster to look at the values when they are in an array than when they are in a sheet. ;)
 
Upvote 0
Why not using UNION function?
VBA Code:
Dim U as range
lastrow = ... ' last row of usedrange
U = UNION(range("B2:B" & lastrow), range("D2:D" & lastrow), range("E2:E" & lastrow), range("J2:K" & lastrow, range("N2:P" & lastrow)
'Then using For ... Next loop through each element in U only
 
Upvote 0
It would take longer to look through Column E for 'Active' status as opposed to just loading that entire column into the array and then check the status of that column later in your code.

Each time you try to read/write from/to a sheet takes time. It is way way faster to look at the values when they are in an array than when they are in a sheet. ;)
The reason why I asked is that the spreadsheet represents several reports from SharePoint locations. We don't have the Power Automate suite of tools, so we are looking to update our reports by opening and refreshing. But I rather have reports that are active to undergo that update than to have all reports that are not even required anymore to be opened and refreshed.
 
Upvote 0
Why not using UNION function?
VBA Code:
Dim U as range
lastrow = ... ' last row of usedrange
U = UNION(range("B2:B" & lastrow), range("D2:D" & lastrow), range("E2:E" & lastrow), range("J2:K" & lastrow, range("N2:P" & lastrow)
'Then using For ... Next loop through each element in U only
This is good. But to get the used range it's LastRow = ActiveSheet("Sheet1").UsedRange.Rows.Count?
 
Upvote 0
The reason why I asked is that the spreadsheet represents several reports from SharePoint locations. We don't have the Power Automate suite of tools, so we are looking to update our reports by opening and refreshing. But I rather have reports that are active to undergo that update than to have all reports that are not even required anymore to be opened and refreshed.
I mentioned you can check the "Active" status later in the code. ;)

Here is the code that includes that:

VBA Code:
Sub LoadNoncontiguousColumnsToArrayV2()
'
    Dim ArrayColumn             As Long, ArrayRow       As Long
    Dim DataLastRow             As Long, DataStartRow   As Long
    Dim RowCount                As Long
    Dim DesiredInputColumns     As String
    Dim LastRowColumn           As String
    Dim ActiveInputArray        As Variant, InputArray  As Variant
'
    DataStartRow = 2                                                                    ' <--- Set this to the start row of data
    DesiredInputColumns = "2,4,5,10,11,14,15,16"                                        ' <--- Set the column #s from source sheet to be loaded into array
    LastRowColumn = "B"                                                                 ' <--- Set this to the column letter to use to determine last row
'
    DataLastRow = Range(LastRowColumn & Rows.Count).End(xlUp).Row                       ' Get last row used in Column
'
    InputArray = Application.Index(Cells, Evaluate("ROW(" & DataStartRow & ":" & _
            DataLastRow & ")"), Split(DesiredInputColumns, ","))                        ' Load the designated column #s into 2D 1 based InputArray
'
    ReDim ActiveInputArray(1 To UBound(InputArray, 1), 1 To UBound(InputArray, 2))      ' Set # of rows & Columns of ActiveInputArray same as InputArray
'
    RowCount = 0                                                                        ' Initialize RowCount
'
    For ArrayRow = 1 To UBound(InputArray, 1)                                           ' Loop through the rows of InputArray
        If Trim(InputArray(ArrayRow, 3)) = "Active" Then                                '   If 3rd column in InputArray = "Active" Then ...
            RowCount = RowCount + 1                                                     '       Increment RowCount
'
            For ArrayColumn = 1 To UBound(InputArray, 2)                                '       Loop through columns of InputArray
                ActiveInputArray(RowCount, ArrayColumn) = InputArray(ArrayRow, ArrayColumn) '       Save row data from InputArray to ActiveInputArray
            Next                                                                        '       Loop back
        End If
    Next                                                                                ' Loop back
'
' *** ActiveInputArray now contains only the 'Active' rows of data ***
'
End Sub

It is done in memory to be be faster than continuously looking at the cells in the sheet.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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