Excel VBA: Loop through headers and cells of a table not working properly

The following code is a WIP. Everything works, except of the two commented out blocks. Any ideas? Thanks in advance!

  1. Transferring Header Names
    After the first header was transferred from one sheet to another, the loop breaks for some reason

  2. Getting Cell Names
    Always getting the error subscript out of range without even entering the loop once. The test tables have only two rows. The first one has a value, the other one is empty.

Option Explicit

Sub GetCellNames(CatRange As String, ArtRange As String)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Dim ws As Worksheet
Dim tbl As ListObject
Dim cellNames As Object
Dim headerArr As Variant
Dim cellArr As Variant
Dim tblName As String
Dim headerRow As String
Dim selectRow As String
Dim selectCol As String
Dim header As Range
Dim cell As Range
Set ws = Worksheets(Range(CatRange).Value)
tblName = Worksheets(1).Range(ArtRange).Value
Set tbl = ws.ListObjects(tblName)
Set cellNames = CreateObject("System.Collections.ArrayList")
selectCol = Chr(Asc(Left(ArtRange, 1)) + 1)
selectRow = Right(ArtRange, 1)
headerRow = Chr(Asc(selectRow) - 1)
'Getting Header Names
For Each header In tbl.HeaderRowRange.Cells
If Not Right(header.Value, 1) = "2" Then
'1. Transferring Header Names
</code>
<code>Dim ws As Worksheet Dim tbl As ListObject Dim cellNames As Object Dim headerArr As Variant Dim cellArr As Variant Dim tblName As String Dim headerRow As String Dim selectRow As String Dim selectCol As String Dim header As Range Dim cell As Range Set ws = Worksheets(Range(CatRange).Value) tblName = Worksheets(1).Range(ArtRange).Value Set tbl = ws.ListObjects(tblName) Set cellNames = CreateObject("System.Collections.ArrayList") selectCol = Chr(Asc(Left(ArtRange, 1)) + 1) selectRow = Right(ArtRange, 1) headerRow = Chr(Asc(selectRow) - 1) 'Getting Header Names For Each header In tbl.HeaderRowRange.Cells If Not Right(header.Value, 1) = "2" Then '1. Transferring Header Names </code>
Dim ws As Worksheet
Dim tbl As ListObject
Dim cellNames As Object
Dim headerArr As Variant
Dim cellArr As Variant
Dim tblName As String
Dim headerRow As String
Dim selectRow As String
Dim selectCol As String
Dim header As Range
Dim cell As Range

Set ws = Worksheets(Range(CatRange).Value)
tblName = Worksheets(1).Range(ArtRange).Value
Set tbl = ws.ListObjects(tblName)
Set cellNames = CreateObject("System.Collections.ArrayList")

selectCol = Chr(Asc(Left(ArtRange, 1)) + 1)
selectRow = Right(ArtRange, 1)
headerRow = Chr(Asc(selectRow) - 1)

'Getting Header Names
For Each header In tbl.HeaderRowRange.Cells
    If Not Right(header.Value, 1) = "2" Then
    
        '1. Transferring Header Names

‘ Worksheets(1).Range(selectCol + headerRow).Value = header.Value

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> '2. Getting Cell Names
</code>
<code> '2. Getting Cell Names </code>
        '2. Getting Cell Names

‘ For Each cell In tbl.ListColumns(header).DataBodyRange
‘ If Not IsEmpty(cell) Then cellNames.Add cell.Value
‘ Next cell

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> ' Transform to Array
cellArr = cellNames.ToArray
' Create Drop-Down List
Call List.CreateDropDown(selectCol + selectRow, cellArr)
selectCol = Chr(Asc(selectCol) + 1)
End If
Next header
</code>
<code> ' Transform to Array cellArr = cellNames.ToArray ' Create Drop-Down List Call List.CreateDropDown(selectCol + selectRow, cellArr) selectCol = Chr(Asc(selectCol) + 1) End If Next header </code>
        ' Transform to Array
        cellArr = cellNames.ToArray
        ' Create Drop-Down List
        Call List.CreateDropDown(selectCol + selectRow, cellArr)
        
        selectCol = Chr(Asc(selectCol) + 1)
    End If
Next header

End Sub

  1. No clue what to try

  2. Tried already the following. Unfortunately, still the same error
    For Each cell In ws.Range(tblName).ListObject.ListColumns.Item(header).Range
    For Each cell In ws.Range(tblName + “[” + header + “]”)

New contributor

imerkow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật