Update For loop from Cells references to dynamic Excel table references

Ben M

New Member
Joined
Aug 13, 2014
Messages
23
Hi,

Is there a way to update this loop so that the Cells (i,7) and Cells (i,4) refer to each cell in the range of a table column (perhaps using the column header to choose the row)?

VBA Code:
[
Sub CreateNewListEng()
    
    Application.ScreenUpdating = False
    
    Dim Theme                As String
    Dim i, FinalRow          As Integer
    Dim TestPage, Data       As Worksheet
    Dim TblData, TblTopic    As ListObject
    
    Set Test = Sheet1
    Set Data = Sheet2
    Set TblData = Worksheets("Data").ListObjects("T_VocabList")
    Set TblTopic = Worksheets("Test").ListObjects("T_Topic")
       
    Theme = Worksheets("Test").ListObjects("T_Topic").DataBodyRange(1, 1)
    FinalRow = TblData.Range.Rows.Count
    
    Sheet1.Range("C4:E1000").ClearContents
    
    For i = 1 To FinalRow
    
    Data.Select
        
        If Cells(i, 7) = Theme Then
            Cells(i, 4).Copy
            Test.Select
            Range("C1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
            Data.Select
            Cells(i, 6).Copy
            Test.Select
            Range("D1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
            Data.Select
        End If
        
    Next i
    
    Test.Select
    Range("E4").Select
    
End Sub
/CODE]

I think it can be done with something like this perhaps?

[I]iListRow In ActiveSheet.ListObjects("Tbl...........").ListRows[/I]

Thanks!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Instead of a numbered loop, you could loop through a range.
VBA Code:
For Each c In Range("TableName[ColumnHeader]")

It is possible that you don't even need to loop, what exactly is it that you're trying to do?
 
Upvote 0
I choose a topic from a drop down list and then press a button to select words from a vocab list that relate to that topic. The words are copied from a second sheet and pasted into a list on the first sheet. Does that make sense?
 
Upvote 0
It would probably make more sense looking at it than reading a description.

Is it simply a case of copying all of the cells that are still visible (in the table column) after the filter is applied?
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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