excel 有没有一种方法可以定义和使用单个细胞?

jq6vz3qz  于 2023-01-31  发布在  其他
关注(0)|答案(2)|浏览(135)

我试着挑出一个单独的细胞,而不是一个常数,因为它取决于手头的数据。

Dim lastrow As Integer
Dim sumcount As Integer

    'find last non-empty row in column C
     lastrow = Cells(Rows.Count, 3).End(xlUp).Row

    'add up the int values in the range in C
     sumcount = Application.WorksheetFunction.Sum(Range("C17", Cells(lastrow, 3)))
    
    'result goes into the next empty cell in C
     Cells(lastrow + 1, 3).Value = sumcount

现在,我尝试将单元格Cells(lastrow + 1, 3)放入一个变量中,这样就可以对其进行偏移,并为偏移单元格赋值一个字符串值。
据我所知,只有Range对象才能做到这一点,但我不知道实现这一点的语法。是否有我不知道的属性或方法?

qyzbxkaa

qyzbxkaa1#

Cells返回一个range对象,所以你应该能够简单地执行以下操作:

set myrange = cells(lastrow + 1, 3)
myrange.value = "string"
fquxozlt

fquxozlt2#

添加列总计并写入右侧相邻单元格

Option Explicit

Sub Test()
    
    Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
    
    Dim sfCell As Range: Set sfCell = ws.Range("C17")
    Dim slCell As Range: Set slCell = ws.Cells(ws.Rows.Count, "C").End(xlUp)
    
    Dim srg As Range: Set srg = ws.Range(sfCell, slCell)
    Dim sSum As Double: sSum = Application.Sum(srg)
    
    Dim dCell As Range: Set dCell = slCell.Offset(1)
    dCell.Value = sSum
    
    dCell.Offset(, 1) = "some string" ' adjacent to the right
    
End Sub

相关问题