Macro assistance

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,055
Office Version
  1. 365
Platform
  1. Windows
This is what I'm trying to acheive;

When someone double clicks on a cell in row 4 (columns J to AM), the worksheet unlocks, copies the contents of the column to the left, pastes it into that column and re-locks the worksheet.

eg - double click on J4, (unlocks) copies contents of column I, pastes it into column J (re-locks)

Is this possible?

Thanks in advance!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Right click the tab of the worksheet you want this to work in and choose 'view code', then paste the following

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("J4:AM4")) Is Nothing Then
        Target.Value = Target.Offset(0, -1).Value
    End If
End Sub

note, this only copies values. If you need to copy formulas, formatting etc, it will need to be modified accordingly.
 
Upvote 0
I do need it to be a complete copy of the column, so formulae, formats etc.
 
Upvote 0
try this:

You'll need to type alt+F11, insert a new module, paste this code in then close and reopen the file for it to work. Delete the original code I posted if you ever used it.

Youll also need to change the password to suit.

Hope this helps.

Code:
Sub Auto_Open()
    On Error Resume Next
    Application.OnDoubleClick = "updateFromLeft"
 
End Sub
Sub updateFromLeft()
    Dim WasProtected As Boolean
    On Error GoTo myhandler
 
    If ActiveCell.Row = 4 And ActiveCell.Column > 9 And ActiveCell.Column < 40 Then
        If ThisWorkbook.ActiveSheet.ProtectContents = True Then
            ThisWorkbook.ActiveSheet.Unprotect Password:="test"
            WasProtected = True
        Else
            WasProtected = False
        End If
            ActiveCell.Offset(, -1).Copy Destination:=ActiveCell
        If WasProtected Then ThisWorkbook.ActiveSheet.Protect Password:="test"
        Exit Sub
    Else
        Application.SendKeys "{F2}"
        Exit Sub
    End If
 
myhandler:
    If Application.ScreenUpdating <> True Then Application.ScreenUpdating = True
    Application.SendKeys "{F2}"
End Sub
 
Upvote 0
Hi

Thanks for that - very nice!

One issue - all it is copying over is the contents of row 4 of the relevant column, not the whole column.
 
Upvote 0
Just got back from lunch.

Quick question, should any of the data above row 4 be copied over? Also, you could copy the whole column whether there's data or not, or copy just the rows with data? Any preference?

If it's the whole column, then this should work (just replace the 1 line - you should be able to tell which one)

Code:
ActiveCell.Offset(, -1).EntireColumn.Copy Destination:=ActiveCell.EntireColumn

If there's a lot of data put the following in after the on error command

Code:
application.screenupdating = false
 
Last edited:
Upvote 0
Option 1 works perfectly!

Thanks for your help on this, much appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,392
Messages
6,119,254
Members
448,879
Latest member
oksanana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top