简体   繁体   English

如何在格式化表中找到上次使用的列?

[英]How can I find last used column in formatted table?

I am trying to get the last used column in my formatted table and insert a value.我正在尝试获取格式化表中最后使用的列并插入一个值。 But it's always returning the very last (=empty) column inside the table.但它总是返回表内的最后一个(=空)列。 See picture below for understanding:看下图理解: 在此处输入图像描述

For some reason, the text "- seit -" is placed into the column "10.Besitzer" in column 16 whereas it should have been put into "1.Besitzer" in column 7.出于某种原因,文本“-seit-”被放置在第 16 列的“10.Besitzer”列中,而它应该被放入第 7 列的“1.Besitzer”中。

My code looks as following:我的代码如下所示:

LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1 '+1 to go to next empty row 

'here it adds values in empty row, starting from column 1(ID) until column 6(IT). 
'I left out this part of code.
 
LastCol = ws.Cells(LastRow, Columns.Count).End(xlToLeft).Column 'Search for last column
        
ws.Cells(LastRow, LastCol).Value = "- seit -" 'place text into last column

If have a feeling its causing problems because its a formatted table but I am not sure about that.如果感觉它会导致问题,因为它是一个格式化的表格,但我不确定。 I am very thankful for any help.我非常感谢任何帮助。

Yes it stops because its a table.是的,它停止了,因为它是一张桌子。 Check if value exist and if no again check for last column same way only not Columns.Count but from the LastColumn.检查值是否存在,如果不存在,则再次以相同的方式检查最后一列,但不是 Columns.Count,而是从 LastColumn。

Read the comments and adjust it to fit your needs阅读评论并根据您的需要进行调整

Public Sub InsertColumnValue()
    
    ' Set a reference to the table
    Dim targetTable As ListObject
    Set targetTable = Range("TableName").ListObject
    
    ' Find last row in column A
    Dim lastRow As Long
    lastRow = targetTable.DataBodyRange.Cells(targetTable.DataBodyRange.Rows.Count, "A").End(xlUp).Row - targetTable.HeaderRowRange.Row

    ' Add data to next empty row
    ' Do something
    
    ' Check if last column in last row is empty
    Dim lastColumn As Long
    If targetTable.DataBodyRange.Cells(lastRow, targetTable.DataBodyRange.Columns.Count).Value = vbNullString Then
        lastColumn = targetTable.DataBodyRange.Cells(lastRow, targetTable.DataBodyRange.Columns.Count).End(xlToLeft).Column
    Else
        lastColumn = targetTable.DataBodyRange.Columns.Count
    End If
    
    ' Add value to next empty column in next empty row
    targetTable.DataBodyRange.Cells(lastRow + 1, lastColumn + 1).Value = "- seit -"

End Sub

Use the next function, please:使用下function,请:

Function LastUsedColInTable(tbl As ListObject, iRow As Long) As Long
 Dim C As Range

 With tbl.Range.rows(iRow)
    Set C = .Find(what:="*", After:=.cells(1), LookIn:=xlValues, _
             searchorder:=xlByColumns, SearchDirection:=xlPrevious)
    If Not C Is Nothing Then
        LastUsedColInTable = C.Column
    Else
        LastUsedColInTable = 1
    End If
 End With
End Function

It can be tested in this way:可以这样测试:

Sub testLastColInTable()
 Dim tbl As ListObject
 Set tbl = ActiveSheet.ListObjects(1)
  Debug.Print LastUsedColInTable(tbl, 2)
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM