Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6575469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:23:26+00:00 2026-05-25T15:23:26+00:00

I try to write a macro that on double click of a cell, inserts

  • 0

I try to write a macro that on double click of a cell, inserts a new row below that cell with some formulas. The important thing for me is that if I double click the cell again, then the formulas of the previously inserted line are updated with the right indexes.

For example, in the code below, double click A1 will insert the formula =B2+1 in line 2. Double clicking again should insert the same in line 2. But now line 2 shifter to line 3, so the formula in A3 should be =B3+1.

Here is the code I have so far:

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 solves the issue. Leaving the question open for explanations on what is Offset and how it differs from Rows (The property EntireRow does not exist for Rows(2))

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:23:26+00:00Added an answer on May 25, 2026 at 3:23 pm

    You can reduce this code by four lines for the same outcome, pls see below

    Note though that your code is updating cells in your target row and below, ie it won’t be updating any cell formulae outside column A that reside above your target. Which is probably not an issue but worth mentioning. If you wanted a full update then you would always insert at row2 rather than at target

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write a macro in clojure that sets up a namespace and
I try to write the following in latex: \begin{itemize} \item \textbf{insert(element|text)} inserts the element
I am having trouble with Lisp's backquote read macro. Whenever I try to write
I try to write a script that counts connected components of a graph and
I'm trying to write a macro in Lisp that re-implements let using itself. This
I'm trying to write some metaprogramming code such that: Inheriting from some class foo<c1,
I'm trying to write a clojure macro that lets me call a function and
I try to write KSH script for processing a file consisting of name-value pairs,
I try to write to a large file, but it seems like it does
I try to write a simple client/server application (all application is a bluetooth service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.