【问题标题】:How do I change the value in an Excel table cell without specifying a cell reference in VBA如何更改 Excel 表格单元格中的值而不在 VBA 中指定单元格引用
【发布时间】:2014-06-05 21:33:17
【问题描述】:

例如,在下面的代码中,我从表中查找一个值,递增该值,然后将新值写回表中。现在,根据单元格名称(即“b5”)将该值写回表中。如果用户对表格进行排序或在表格上方添加/删除和行,这一切都会崩溃。 我想做的是确定在查找时从中加载值的单元格的名称,以便可以将新值放回原处。

 '*** Lookup last DEWR number from Properties Page ***
    intDEWRNum = Application.WorksheetFunction.VLookup( _
    "Current DEWR", Worksheets("Properties").ListObjects("propertiesTable").Range, 2, False)

    If intDEWRNum < 1 Then
        intDEWRNum = 1      ' If DEWR field is blank then this is the first DEWR.
    Else
        intDEWRNum = intDEWRNum + 1 ' Increment DEWR field
    End If

    Worksheets("Properties").Range("b5").Value = intDEWRNum
                               '^ This is what I want to avoid.***********


    newSheet.Name = "DEWR " & Str(intDEWRNum)   ' Name new worksheet with new DEWR number

    ActiveSheet.Range("j6").Value = Str(intDEWRNum)   ' Insert current DEWR number into sheet

【问题讨论】:

    标签: excel excel-2013 vba


    【解决方案1】:
    Dim f as Range
    
    Set f = Worksheets("Properties").ListObjects("propertiesTable"). _
            Range.Columns(1).Find("Current DEWR",lookin:=xlvalues, lookat:=xlwhole)
    
    If not f is nothing then
        intDEWRNum = f.offset(0,1).Value
    
        If intDEWRNum < 1 Then
            intDEWRNum = 1      ' If DEWR field is blank then this is the first DEWR.
        Else
            intDEWRNum = intDEWRNum + 1 ' Increment DEWR field
        End If
    
        f.offset(0,1).Value= intDEWRNum
    
        'etc....
    

    【讨论】:

    • 这正是我所需要的。谢谢!
    猜你喜欢
    • 2020-12-14
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2023-02-02
    • 1970-01-01
    相关资源
    最近更新 更多