BeforeDoubleClick code placing data based on where cursor currently exists - vba

I may or may not have an easy one here... For some reason I can't properly think of the solution.
Spreadsheet has numbers 1 to 14 in column A. 14 ends on row 15 as there is a header in cell A1. What I'm trying to do is double click one of these numbers and transfer that number to a specific cell on the same sheet (named "Sheet1"). I was able to put the code together to make it work for transferring a chosen number to a specific cell. Code below works well. However, I don't want to add a bunch of areas to double click for different cells. For example: The destination cell for the chosen data is E6, H6, and G6. I'd like to place the cursor starting in E6, double click a number in range A2 to A15, and have that number that I choose between A2 and A15 to appear in E6 since that is where the cursor was when I double clicked a cell in A2 to A15. Then I would move the cursor by click H6 and then going back to same selection between A2 to A15, placing whatever number I choose in that range in H6 since that is where the cursor currently exists.
Hopefully this makes sense and is even possible.
example screenshot
Working code that allows the double click to put data in a certain cell
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A2:A15")) Is Nothing Then
Cancel = True
Target.Copy Destination:=Cells(6, "E")
End If
End Sub

I've added the solution I would try, with comments in the code. The reason for two sheet variables, is because the first click in the double-click registers as a SelectionChange event. So to get the correct cell location, you need it from two selections back, instead of just one.
Public selectedCell As String 'Sheet Variable
Public lastCell As String
' This updates the Sheet variable with the most recent selection
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
selectedCell = lastCell
lastCell = Target.Address
End Sub
' Added a check for having a previously selected cell
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A2:A15")) Is Nothing Then
If selectedCell = vbNullString Then
Cancel = True
MsgBox "Please select a destination cell for the data."
selectedCell = vbNullString
lastCell = vbNullString 'Prevents overwriting same cell by accident
Else
Cancel = True
Target.Copy Destination:=Range(selectedCell)
selectedCell = vbNullString
lastCell = vbNullString 'Prevents overwriting same cell by accident
End If
End If
End Sub

Here is a tiny tool that you may be able to adapt to your needs. If you double-click on an empty cell, it becomes the FinalDestination. If you then double-click on another cell that is not empty, its contents will be copied to FinalDestination:
In the worksheet code area:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Application.EnableEvents = False
If Target.Value = "" Then
Set FinalDestination = Target
Else
Target.Copy FinalDestination
End If
Application.EnableEvents = True
End Sub
In a standard module:
Public FinalDestination As Range
NOTE:
In this simple demo code there are no restrictions on source/destination.

Related

VBA is not updating automatically after input

I have created a Excel workbook for my work where I collect information. In this workbook I have a sheet where details in various currencies can be filled in. Based on the selection by the preparer the value in cell B5 will change to either USD or LC. In case the value in cell B5 will be USD, columns C and E should be hidden. The issue in here is that this code will not immediately unhide the columns. After clicking on a random cell, the columns are hidden. Please let me know if there is a solution for this issue whereby the columns are hidden without clicking on a random cell each time. Thank you.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("B5").Value = "USD" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = True
ElseIf Range("B5").Value = "LC" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = False
End If
End Sub
Thank you all for your comments. Let me further elaborate on my issue. In principle my VBA code works. The only thing is that after cell B5 is changed to USD, initially nothing happens. After I click on a random cell in this sheet, the VBA code works and hide the columns. The same is applicable in case the value in cell B5 is LC. Then again the VBA code does not work immediately. After clicking on a random cell in the sheet the columns are unhided.
A small update with respect to cell B5. So cell B5 contains a formula that is linked to listed cell in another sheet. After a value is selected from the list in another sheet, cell B5 will determine through the IF functions if the value in B5 will be LC or USD.
I am now afraid that after the preparer select in the listed cell a value, it will not click on a random cell in designated sheet resulting that he or she will see the wrong information.
Please let me know if you require further information. Thank you.
PS. I am not very strong in creating VBA codes.
You're using the wrong event.
SelectionChange fires when you select a different cell, etc.
Change fires immediately after a cell's contents change.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B5").Value = "USD" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = True
ElseIf Range("B5").Value = "LC" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = False
End If
End Sub
Alternate Solution:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Range("B5") Then Union(Columns("C"), Columns("E")).EntireColumn.Hidden = (Target.Value = "USD")
End Sub
(adapted from #Vityata's comment below)
More Information:
MSDN : Worksheet.SelectionChange Event (Excel)
MSDN : Worksheet.Change Event (Excel)
A few changes to your code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$5" Then
If Range("B5").Value = "USD" Then
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = True
Else
Union(Columns("C"), Columns("E")).EntireColumn.Hidden = False
End If
Application.Calculate
End If
End Sub
1: the right event is Change (when a value changes in the sheet), not SelectionChange (when the selection changes).
2: Your code works on my side, so I guess it's not working on your side because you have some calculation off. I've added the line Application.Calculate which will refresh the spreadsheet even if your calculations are set to manual.
3: I've added an If Target.Address condition to check only when the cell that changed is the right one.
4: I've added just Else instead of ElseIf cell = "LC", because it's faster and because I think it's cleaner (if the user removes completely the value of the currency and before it was USD, nobody will unhide the columns C and E).
Keep it simple,
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Set rng = Range("B5")
If rng.Value = "USD" Then Columns("C:E").EntireColumn.Hidden = True
If rng.Value = "LC" Then Columns("C:E").EntireColumn.Hidden = False
End Sub

Capture the final change in a Worksheet Change Event

In the worksheet(1), I have multiple plan-to-change cells, A1 and other cells, and others cells value depends on the the value of A1 as I set up the function(eg.,B1=A1+1). In this way, if I change the value of A1, other cells would change value as well, and the total changed cells would be the A1 plus others changed cells. However, as the quantity of "other cells" is quite large, when I run the following function in VBA:
Private Sub Worksheet_Change(ByVal Target As Range)
Cells(Target.Cells.Count, 21) = "ok"
End Sub
The column 21 would appear 3 "ok"s as I suppose because the large cells change happen in order, which actives three times the Worksheet_Change function. (the first ok for I change A1, the second for the time delay of updated function, the third for the final cell) However, I only want the Worksheet_Change to capture the final cells change after I change the value of A1, what should I do to avoid the previous capturing of Worksheet_Change function?
Modify your code to run only when A1 is changed.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address Then
Cells(1, 21).Value = "ok"
End If
End Sub
Target is often more than a single cell. Determine if one of the changed cells is A1 with Intersect and then only deal with A1.
If you plan to change a value on the same worksheet, shut down event handling temporarily so the Worksheet_Change does not try to run on top of itself.
Private Sub Worksheet_Change(ByVal Target As Range)
if not intersect(target, range("a1")) is nothing then
on error goto safe_exit
application.enableevents = false
dim trgt as range
for each trgt in intersect(target, range("a1"))
Cells(trgt.row, 21) = "ok"
next trgt
end if
safe_exit:
application.enableevents = true
End Sub
Since I don't believe that a1 is the actual target of your Target in your real world, I've made the code a little more verbose than it probably needs to be.

How to run macro if based on other cells which automatically changes by formula

As per subject, what I need is to run macro based on other cells.
Here is the case :
cells G3 until the end of row contains data used formula =IF(B3="";"";(SUMIF('Incoming Goods'!$F$3:$F$1048576;'Current Stock'!B3;'Incoming Goods'!$M$3:$M$1048576)-(SUMIF('Outgoing Goods'!$D$4:$D$1048576;'Current Stock'!B3;'Outgoing Goods'!$J$4:$J$1048576))))--> i need to convert this formula to VBA
cells H3 should contain : If G3.value = 0 then "Out of Stock", else " "
And this sheet must be calculate every time data in G3 change automatically or any additional data on this sheet.
Already tried this code :
Private Sub Worksheet_Calculate()
Dim Current As Worksheet
Dim Rng1 As Range
Dim Target As Range
Set Current = Worksheets("Current Stock")
Set Rng1 = Current.Range("G:G")
Set Target = Range("H:H")
For Each Rng1 In Target
If Rng1.Value2 = "0" Then
Target.Value2 = "Out Of Stock"
Else
Exit Sub
End If
Next
End Sub
However, above code is Not working. Already try using Private Sub Selection Change() and Private Sub Selection Change() but still not working.
Any suggestion?
Thanks in advance
the answer to the first part is below:
ActiveCell.FormulaR1C1 = _
"=IF(R[2]C[1]="""","""",(SUMIF('Incoming Goods'!R3C6:R1048576C6,'Current Stock'!R[2]C[1],'Incoming Goods'!R3C13:R1048576C13)-(SUMIF('Outgoing Goods'!R4C4:R1048576C4,'Current Stock'!R[2]C[1],'Outgoing Goods'!R4C10:R1048576C10))))"
handy tip: to convert any excel formula to code, hit the record macro button, then click on the cell, press F2 key, then press enter, and stop recording macro. The code will now be in its own module in the vba editor.
This should do what you want.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("A1"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value > 200 Then
Call YourMacroName
End If
End If
End Sub

Prefill a certain cell with a number when data (a letter) is entered in one cell

I'm trying to figure out a VBA code that will allow me to prefill a certain cell with a number when I type in "X" in a cell right next to it. I can't figure out if I should use Range, or Insert, or what.
I cannot use a button and assign a macro to it because I need to see which cells I have put an "X" into.
This is what I have so far, but it's using a button with macro assigned to it:
490 is being entered into E9 and tabs over to F9 after the macro button is clicked:
Sub eightNineSpring()
Range("E9").Select
ActiveCell.FormulaR1C1 = "490"
Range("F9").Select
End Sub
as automation put in the worksheet you need it:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 And Target.Count = 1 Then
If Target.Value = "x" Then Target.Offset(0, -1).Value = 490
End If
End Sub
or as formula in E1 then copy down
=IF(F1="x",490,"")
But keep in mind when deleting the "x" (or replace it with something different):
The function will empty the 490 again while the change event will not
When using a Change Events that makes a change, Application.Events should be turned off to avoid the code calling itself recursively.
The code below caters for one or more cells in E1:E10 being updated.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Intersect(Target, Range("F1:F10"))
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each rng2 In Range
If rng2.Value = "x" Then rng2.Offset(0, -1).Value = 490
Next
Application.EnableEvents = True
End Sub

Getting the selected cell's range from a different worksheet in Excel

I'm trying to set up Excel so that the cell's value that is selected in the first worksheet is set to the value of a cell that's double clicked in a different worksheet. So far my code looks like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim c As Range
For Each c In Sheet1.Range("M11:M24")
If IsEmpty(c) Then
c.Value = Target.Value
Exit For
End If
Next c
End Sub
What this does is sets the first empty cell in the range m11:m24 to the contents of the double clicked cell in the other worksheet. What I want though is not a static "M11:M24" range, but instead have the user select a cell in the first worksheet by clicking on it, move to the other worksheet, double click a cell in that worksheet and have the value appear in the selected cell on the first worksheet. I think I could have it so that there is a variable set up to save which cell is selected in the first worksheet and then just access that from the other worksheet. But I'd prefer if there was away built in to Excel to just choose the selected cell.
Is there a way to get the selected cell/range in Excel?
I solved this easily. The code is:
Sheet1.Activate
ActiveCell.Value = Target.Value
If you want to do a whole selection, try
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Sheet1.Activate
Dim r As Range
Set r = Selection
r.Value = Target.Value
End Sub