Learning Loops and List Objects

CascadeDiver

Board Regular
Joined
Apr 10, 2020
Messages
62
Office Version
  1. 2016
Platform
  1. Windows
Hi all.

I'm trying to learn some basics on looping and List Objects.
I've created a simple loop that displays data from a selected range into a new field. It works just fine however I think there is a way I can replace the .Offset using ListRow but I'm not sure how. The reason for wanting to do away with the .Offset is for the ability to move the tables elsewhere.

VBA Code:
Option Explicit
Sub Load_Data()
Dim MainTable As ListObject
Set MainTable = Sheet1.ListObjects("Main_Table")
Dim RefTable As Range
Set RefTable = Sheet1.ListObjects("Review_Table").ListRows(1).Range
Dim SelectedRow As Long
SelectedRow = ActiveCell.Row

Dim EntryCol As Integer
For EntryCol = 1 To 3
    RefTable.Cells(1, EntryCol).Value = MainTable.Range(SelectedRow, EntryCol).Offset(-6, 0).Value
Next EntryCol

End Sub
Learning Loops.PNG


As the photo shows. When a cell in the MainTable is selected the data repopulates in the RefTable
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Rather than looping through the 3 columns, you can copy the whole table row in one statement.

VBA Code:
Public Sub Load_Data2()

    Dim MainTable As ListObject
    Dim RefTable As ListObject
    Dim MainTableRow As Range
    
    Set MainTable = Sheet1.ListObjects("Main_Table")
    Set RefTable = Sheet1.ListObjects("Review_Table")
    
    If Not Intersect(ActiveCell, MainTable.DataBodyRange) Is Nothing Then
        Set MainTableRow = MainTable.ListRows(ActiveCell.Row - MainTable.DataBodyRange.Row + 1).Range
        MainTableRow.Copy RefTable.DataBodyRange
    Else
        MsgBox "Active cell is not in Main_Table data"
    End If
    
End Sub
 
Upvote 0
Attempting to use a formula in one of the columns in the MainTable. The RefTable shows the formula and not the cell value.

I tried substituting with
VBA Code:
    MainTableRow.Copy
    RefTable.DataBodyRange.PasteSpecial xlPasteValues

However the active selection immediately highlights the RefTable yielding anything in the MainTable unselectable.

Is there a way around this?
 
Upvote 0
For just values, you could use a straight assignment like this:

Code:
Sub Load_Data()
Dim MainTable As ListObject
Set MainTable = Sheet1.ListObjects("Main_Table")
Dim RefTable As Range
Set RefTable = Sheet1.ListObjects("Review_Table").ListRows(1).Range
Dim SelectedRow As Range
On Error Resume Next
Set SelectedRow = Intersect(ActiveCell.EntireRow, MainTable.DataBodyRange)
On Error GoTo 0
If Not SelectedRow Is Nothing Then RefTable.Cells(1).Resize(1, SelectedRow.Columns.Count).Value = SelectedRow.Value
End Sub
 
Upvote 0
For just values, you could use a straight assignment like this:

That worked indeed. Thanks. Now I'll do some reading on what .Resize represents so I'll have a sound footing going forward.
 
Upvote 0

Forum statistics

Threads
1,215,640
Messages
6,125,972
Members
449,276
Latest member
surendra75

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