English 中文(简体)
专栏:插入行更新公式
原标题:excel: insert row updating formulas
  • 时间:2011-09-18 18:22:10
  •  标签:
  • excel
  • vba

我试图写一个宏观,在两点点点点点击时,用一些公式在囚室后面插入一个新行。 对我来说,重要的是,如果我再次点击牢房,那么前面插入的线路的公式就会得到正确的指数的更新。

例如,在以下代码中,将双点击A1插入“公式=B2+1”一行。 2. 重复点击应插入一行。 2. 结 论 但现在第2行改为第3行,因此A3中的公式应为=B3+1。

这里是我迄今为止制定的法典:

Option Explicit

Const MYRANGE As String = "A:A"

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
     If Sh.Name <> "Sheet1" Then Exit Sub
    If Target.Cells.Count > 1 Then Exit Sub
    If Intersect(Target, Sh.Range(MYRANGE)) Is Nothing Then Exit Sub
    Cancel = True
    Target.Rows(2).Insert
    Dim newRow As Range
    Set newRow = Target.Rows(2)
    Dim rowIndex As Long
    rowIndex = newRow.row
    newRow.Cells(1, 1).Formula = "=B" & rowIndex & "+1"
End Sub

UPDATE: Changing Target.Rows(2).Insert to Target.Offset(1).EntireRow.Insert 解决这个问题。 提出问题,可以解释谁是谁是谁,它如何不同于罗(Ros(2)没有财产)。

最佳回答

您可以将这一法典削减四行,以取得相同结果。

请注意,你的法典正在更新你的目标行文和以下的囚室,因此它所赢得的只是更新一栏外的任何囚房配方。 生活在你的目标之上。 这个问题可能不是一个问题,值得一提。 如果您希望得到全面更新,那么你总是会插入第二行而不是目标。

Option Explicit

Const MYRANGE As String = "A:A"

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    If Target.Cells.Count > 1 Then Exit Sub
    If Intersect(Target, Sh.Range(MYRANGE)) Is Nothing Then Exit Sub
    Cancel = True
    Target.Offset(1).EntireRow.Insert
    Target.Offset(1).Formula = "=B" & Target.Row + 1 & "+1"
End Sub
问题回答

暂无回答




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签