Implement a macro (increase cell +1 by right click) for Merge Cells

itommy14

New Member
Joined
Jul 17, 2015
Messages
9
Hi, I'm new on this forum
I've got a problem with a macro used for increasing cells by 1 with the right click of the mouse

This is the macro:

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

If Target.Cells.Count > 1 Then Exit Sub

If Not Intersect(Target, Range("A1:Z100")) Is Nothing Then
Cancel = True
Target = Target + 1
End If

End Sub

Unfortunately it doesn't work with merge cells. How can I do?
Thank you
 

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,151
Office Version
365
Platform
Windows
Hi itommy14,
it took some messing around, but I found some help on Stackoverflow and this seems to work. The code is commented so you know what's happening.
Cheers,
Koen

Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

'http://stackoverflow.com/questions/14167481/check-if-merged-cells-are-blank
If Target.Cells.Count > 1 Then
    If IsNull(Target.MergeCells) Then
        'Changed Range contains one or more Merged cells and other cells
        Exit Sub
    Else
        If Target.MergeCells Then
            'Changed Range is a single Merged cell
        Else
            'Changed Range is Unmerged cells only
            Exit Sub
        End If
    End If
Else
    'Continue, one cell selected
End If

If Not Intersect(Target(1, 1), Range("A1:Z100")) Is Nothing Then
    Cancel = True
    Target(1, 1) = Target(1, 1) + 1
End If

End Sub
 

itommy14

New Member
Joined
Jul 17, 2015
Messages
9
Hello how can i do to increase cell value by one in another sheet?
For example if A1 in sheet1 is selected and I press a button I want that cell B2 in sheet 2 in increased by 1
Thank you
 

Rijnsent

Well-known Member
Joined
Oct 17, 2005
Messages
1,151
Office Version
365
Platform
Windows
Hi itommy,
squeezing into your previous code:
Code:
If Not Intersect(Target(1, 1), Range("A1:Z100")) Is Nothing Then
    Cancel = True
    Target(1, 1) = Target(1, 1) + 1
    rw = Target(1,1).Row
    col = Target(1,1).Column
    Worksheets("Sheet2").Cells(rw+1,col).value = Worksheets("Sheet2").Cells(rw+1,col).value + 1

End If
The last bit is the one finding the row and column of your selected cell and add one to the same cell one row down on the Sheet2 worksheet.

Cheers,

Koen
 

Forum statistics

Threads
1,081,983
Messages
5,362,548
Members
400,679
Latest member
alecalec202

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top