Looping through all rows in a table column, Excel-VBA

前端 未结 9 562
予麋鹿
予麋鹿 2020-12-14 19:12

I\'m currently working on a data set which is formatted as a table, with headers. What I need to do is cycle through all cells in a specific column and change the contents.

相关标签:
9条回答
  • 2020-12-14 19:49

    You can find the last column of table and then fill the cell by looping throught it.

    Sub test()
        Dim lastCol As Long, i As Integer
        lastCol = Range("AZ1").End(xlToLeft).Column
            For i = 1 To lastCol
                Cells(1, i).Value = "PHEV"
            Next
    End Sub
    
    0 讨论(0)
  • 2020-12-14 19:57

    Assuming that your table is called 'Table1' and the column you need is 'Column' you can try this:

    for i = 1 to Range("Table1").Rows.Count
       Range("Table1[Column]")(i)="PHEV"
    next i
    
    0 讨论(0)
  • 2020-12-14 20:01

    You can search column before assignments:

    Dim col_n as long
    for i = 1 to NumCols
        if Cells(1, i).Value = "column header you are looking for" Then col_n = i
    next
    
    for i = 1 to NumRows
        Cells(i, col_n).Value = "PHEV"
    next i
    
    0 讨论(0)
  • 2020-12-14 20:04

    Assuming your table is called "Table1" and your column is called "Column1" then:

    For i = 1 To ListObjects("Table1").ListRows.Count
        ListObjects("Table1").ListColumns("Column1").DataBodyRange(i) = "PHEV"
    Next i
    
    0 讨论(0)
  • 2020-12-14 20:06

    If this is in fact a ListObject table (Insert Table from the ribbon) then you can use the table's .DataBodyRange object to get the number of rows and columns. This ignores the header row.

    Sub TableTest()
    
    Dim tbl As ListObject
    Dim tRows As Long
    Dim tCols As Long
    
    Set tbl = ActiveSheet.ListObjects("Table1")  '## modify to your table name.
    
    With tbl.DataBodyRange
        tRows = .Rows.Count
        tCols = .Columns.Count
    End With
    
    MsgBox tbl.Name & " contains " & tRows & " rows and " & tCols & " columns.", vbInformation
    
    End Sub
    

    If you need to use the header row, instead of using tbl.DataBodyRange just use tbl.Range.

    0 讨论(0)
  • 2020-12-14 20:07

    Since none of the above answers helped me with my problem, here my solution to extract a certain (named) column from each row.

    I convert a table into text using the values of some named columns (Yes, No, Maybe) within the named Excel table myTable on the tab mySheet using the following (Excel) VBA snippet:

    Function Table2text()
        Dim NumRows, i As Integer
        Dim rngTab As Range
        Set rngTab = ThisWorkbook.Worksheets("mySheet").Range("myTable")
        ' For each row, convert the named columns into an enumeration
        For i = 1 To rngTab.Rows.Count
            Table2text= Table2text & "- Yes:"   & Range("myTable[Yes]")(i).Value & Chr(10)
            Table2text= Table2text & "- No: "   & Range("myTable[No]")(i).Value & Chr(10)
            Table2text= Table2text & "- Maybe: "& Range("myTable[Maybe]")(i).Value & Chr(10) & Chr(10)
        Next i
      ' Finalize return value 
      Table2text = Table2text & Chr(10)  
    End Function
    

    We define a range rngTab over which we loop. The trick is to use Range("myTable[col]")(i) to extract the entry of column col in row i.

    0 讨论(0)
提交回复
热议问题