如何在不指定VBA中的单元格引用的情况下更改Excel表格单元格中的值

时间:2014-06-05 21:33:17

标签: excel-vba excel-2013 vba excel

例如,在下面的代码中,我从表中查找一个值,递增值,将新值写回表中。现在,根据单元名称(即“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

1 个答案:

答案 0 :(得分:0)

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....