如何在不在VBA中指定单元格引用的情况下更改Excel表格单元格中的值
例如,在下面的代码中,我从表中查找一个值,增加该值,然后将新值写回表中。现在,该值将基于单元格名称(即b5)写回表中。如果用户对表进行排序或添加/删除表及其上方的行,则所有这些都会崩溃。 我想做的是确定在查找时从中加载值的单元格的名称,以便可以将新值放回相同的位置如何在不在VBA中指定单元格引用的情况下更改Excel表格单元格中的值,excel,excel-2013,vba,Excel,Excel 2013,Vba,例如,在下面的代码中,我从表中查找一个值,增加该值,然后将新值写回表中。现在,该值将基于单元格名称(即b5)写回表中。如果用户对表进行排序或添加/删除表及其上方的行,则所有这些都会崩溃。 我想做的是确定在查找时从中加载值的单元格的名称,以便可以将新值放回相同的位置 '*** Lookup last DEWR number from Properties Page *** intDEWRNum = Application.WorksheetFunction.VLookup( _
'*** 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
这正是我需要的。谢谢
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....