excel xlToRight但不包括前几列

bqucvtff  于 2022-12-27  发布在  其他
关注(0)|答案(3)|浏览(200)
Worksheets("front").Cells(5, Columns.Count).End(xlToRight).Column + 1

已经有这个代码了,工作很好-问题是..我不想让它填充列A,这恰好是空白的,所以代码是按预期工作的,我需要它跳过,并开始采取列H后的下一个可用列...我尝试了一些抵消工作,但仍然无法让它做我需要的。
任何帮助都非常感谢!看起来xlright上的资源并不像xlup那么常见:)

Worksheets("front").Cells(5, Columns.Count).End(xlToRight).Column + 1
h5qlskok

h5qlskok1#

行中第一个“可用”单元格

...即与最右侧非空单元格的右侧相邻的单元格

使用End属性

  • 请记住,如果“最后一个”单元格所在的列被隐藏,则此操作将失败(给予测试,使其下沉)。如果存在这种可能性,则应使用Find方法版本。
Sub AddTodayEnd()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Front")
    Dim fCell As Range: Set fCell = ws.Range("H5")
    Dim lCell As Range
    Set lCell = ws.Cells(fCell.Row, ws.Columns.Count).End(xlToLeft)
    If lCell.Column < fCell.Column Then Set lCell = fCell
    lCell.Offset(, 1).Value = Date
End Sub

使用查找方法(推荐)

Sub AddTodayFind()
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Front")
    Dim fCell As Range: Set fCell = ws.Range("H5")
    Dim lCell As Range: Set lCell = fCell.Resize(, ws.Columns.Count _
        - fCell.Column + 1).Find("*", , xlFormulas, , , xlPrevious)
    If lCell Is Nothing Then Set lCell = fCell
    lCell.Offset(, 1).Value = Date
End Sub
kh212irz

kh212irz2#

你可能会在这之后

Option Explicit

Function GetFirstAvailableColumnIndexAfterColumnH(sh As Worksheet) As Long

    With sh               
        GetFirstAvailableColumnIndexAfterColumnH = WorksheetFunction.Max(8, .Cells(5, .Columns.Count).End(xlToLeft).Column) + 1
    End With

End Function

测试:

Sub test()
    Dim sh As Worksheet
    Set sh = Worksheets("front")

    sh.Cells(5, GetFirstAvailableColumnIndexAfterColumnH(sh)).Value = Date
End Sub
vx6bjr1n

vx6bjr1n3#

这将解决您的问题:

'col is the column index of the first column avaible to you
Dim col As Long
col = Worksheets("front").Cells(5, Columns.Count).End(xlToLeft).Column
If (col < 8) Then
    col = 8
Else
    col = col + 1
End If
Worksheets("front").Cells(5, col).Value = "Your Data"

相关问题