VBA to Copy one cell left of active cell and paste in a different cell but just one time and then make previous active cell the active cell again

Ohyaisee

New Member
Joined
Jan 21, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi
I am having troubles finding the solution to what I want to do. I have a locked sheet where I have unlocked specific cells so the user can tab to the next cell to fill in. What I am looking to do is to copy the Field Name such as "Payee Name" and place in another cell that is blank.
1705354864785.png
1705355089757.png

I have tried this but still new to vba and know I am missing a bunch of stuff. It does it but is in a loop as it shows W12 as Active cell continuously. So my plan is to take the field name of whatever feild the user is at and populate it in W12 and then go back to the field that the user was at.

This is where I am at:
Private Sub CopyCells(ByVal Target As Range)
ActiveCell.Offset(0, -1).Select
Selection.Copy
Range("W12").PasteSpecial xlPasteAll
End Sub


Any help would be greatly appreciated!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
It is very rare with VBa that you have to use "Select" to do anything, in your case the easiest way to leave the active cell where it started is not to move , trythis really simple code:
VBA Code:
Sub CopyCells()
ActiveCell.Offset(0, -1).Copy Range("W12")
End Sub
 
Upvote 0
Best if you just explain what you want as a result and not worry about how it would be done. I say that because it seems that the W12 thing is your way of storing what cell a user was in, but I don't see that as being necessary. You probably should be using the SelectionChange event instead.
 
Upvote 0
It is very rare with VBa that you have to use "Select" to do anything, in your case the easiest way to leave the active cell where it started is not to move , trythis really simple code:
VBA Code:
Sub CopyCells()
ActiveCell.Offset(0, -1).Copy Range("W12")
End Sub
Thanks for the expedited response. I tried that and I must have a conflict somewhere as the Excel workbook shuts down on me. I did try in in another workbook and the result is what I want. My issue is that I have Private subs that change locked cells based on user input. My current macro I am sure is an eye sore but it is working fine until I hit this little issue. So for instance, the code for one if statement is in the image. My goal is for the macro to run each time the user gets a new active cell. Would it be better if I shared the whole macro that I have as a private sub on a sheet?
 

Attachments

  • Screenshot 2024-01-15 144340.png
    Screenshot 2024-01-15 144340.png
    28 KB · Views: 17
Upvote 0
Best if you just explain what you want as a result and not worry about how it would be done. I say that because it seems that the W12 thing is your way of storing what cell a user was in, but I don't see that as being necessary. You probably should be using the SelectionChange event instead.
I am trying to set up a macro that populates a specific cell (in this case W12) with the Field name of the cell that the user is on so that I can then add a lookup to show the definition for the used in the cell (in this case X12). The issue is that I want it to run each time the user tabs to a new cell.
 
Upvote 0
Best if you just explain what you want as a result and not worry about how it would be done. I say that because it seems that the W12 thing is your way of storing what cell a user was in, but I don't see that as being necessary. You probably should be using the SelectionChange event instead.
My idea is that I want an excel sheet for a user to fill in and I also want a Field Description on the screen for the field that they are at. Here is a screenshot of the workbook but I also have some other macros running when target fields are entered. The white space to the right is where I want to use a lookup to put definition of field so every time the user tabs definition area gives a new definition:
1705362607380.png
 
Upvote 0
I suspect that the reason the code I posted causes your workbook to shut down is because of the way you are calling the code, if you trigger the code automatically with the worksheet change event then it will crash the workbook , what you have to do is turn events on and off when writing to a cell using vba.
I think that all you actually need to do to meet your requirements is put this code in the worksheet code which uses the selection change event. ( I added the code to turn of events although it might not be necessary it is good practice:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False ' turn off events to prevent loops and crashes
Target.Offset(0, -1).Copy Range("W12")
Application.EnableEvents = True  ' turn events back on again to allow normal operation of excel
End Sub
 
Upvote 0
put this code in the worksheet code which uses the selection change event
Suggested in post 3, not addressed afterwards. I see this as determining which cell was chosen (Target.Address) and based on that, write a description to W12 or wherever it is wanted. Probably a Select Case block to provide the descriptor; something like
VBA Code:
Select Case Target.Address
   Case $A$10
        Range("W12") = "Payee Name"
   Case ...
End Select
although I have to wonder why any of this is necessary. The 'label' used for the data entry cell should tell you what you need to know. Perhaps this has been simplified for this thread, but it sure looks like "Payee name" in W12 should not be needed.

I agree that if there are other events being triggered by this (e.g. as a result of changing cell values) then I'd try preventing that as suggested. I advocate using an error handler lest a raised error leaves events turned off.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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