excel VBA double click go to cell value - vba

i have the code that copy/pastes a value from a cell in a range to a target cell
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Range("A2:A1000"), Target) Is Nothing Then
Application.EnableEvents = False
Range("B1048568") = Target
Cancel = True
Application.EnableEvents = True
End If
End Sub
What values do I need to change to do "goto" instead of copy/paste?
I have a list of values on column "A" and the same values as headers on cells "D:Z"
I want by double clicking on a cell in column "A" to go to the cell with the same value on the range "D:Z"

You need to find the cell and then use the Activate method of that cell.

Related

BeforeDoubleClick code placing data based on where cursor currently exists

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.

Move ActiveCell back to the cell whose value got changed

I have a macro that needs to run when a cell value gets changed.
This is what I have. This doesn't work because when I change the value of cell and then hit Enter or click on some other cell, the macro takes values from the cell that I have moved to and not from the cell whose value I changed
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("B3:V34")) Is Nothing Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error GoTo Finalize 'to re-enable the events
MsgBox (Target.Address)
'prints the cell address whose value I changed before I clicked on some other cell
MsgBox (ActiveCell.Address)
'prints the cell address of the new cell where I have moved to
ActiveCell.Range(Target.Address).Activate
'This is me trying to move the active cell to the cell whose value I changed
'But this doesn't work. The activeCell moves to some random cell
'I wanna run a macro here which takes activeCell address and value as input
Finalize:
Application.EnableEvents = True
End Sub

How to add a worksheet with the value of the cell I double clicked

I developed this code to select a worksheet named with the text of a cell on column B when I double click on it.
But I am trying to increase my code with one IF condition:
If there is no worksheet named with the cell I double clicked show a msgbox ("Hello")
and after that create the worksheet with the name of the cell I double clicked.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = 2 Then
If Sheets(Target.Text) = True Then
Sheets(Target.Text).Visible = xlSheetVisible
Sheets(Target.Text).Select
Sheets(Target.Text).Range("A2").Select
Cancel = True
Else
MsgBox ("hello")
End If
End If
End Sub
This condition does not work as I thought
If Sheets(Target.Text) = True Then
Sheets(Target.Text) is a Worksheet-object, you cannot compare it to a boolean. Furthermore, if the sheet doesn't exists, you will get a runtime error. What you want is a function to check if a worksheet exists. You can find many examples, for example on stackoverflow
Copy such a function into your code and change your event handler
If SheetExists(Target.Text) Then
Sheets(Target.Text).Visible = xlSheetVisible
Sheets(Target.Text).Select
Sheets(Target.Text).Range("A2").Select
Cancel = True
Else
MsgBox ("hello")
End If

Excel VBA: Get range of previous cell after calling LostFocus()

How can I get the range of a previous cell in Excel? I have a ComboBox and usually I can fill its value into the active cell (under the ComboBox) with ActiveCell.Value = box.Value. When I have selected a value of my ComboBox and click in any other cell, I want the value of the ComboBox to be written into the previous cell, but this code writes it to cell I clicked on:
Private Sub box_LostFocus()
ActiveCell.Value = box.Value
End Sub
Any ideas?
If you include this in your worksheet code, PreviousCell will always be the previous range you had selected.
Dim PreviousCell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Your code that uses PreviousCell should go in the if statement that makes sure PreviousCell has a value
If Not PreviousCell Is Nothing Then
Debug.Print PreviousCell.Address
End If
Set PreviousCell = Target ' This needs to be the last line of code.
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