Excel and changing value between two drop down lists - excel

I have two drop down lists -- one is dependent on the other -- meaning if I select a particular value from list in A1, a specific list appears in A2. This works just fine. However, when I change the value in A1, A2 stays in the cell until I click on the list -- then my value in A2 will change based on my selection.
For example, if list 1 is ['Yes','No'] and list to is Yes: [1,2,3] No: [4,5,6]. First I select 'Yes' for A1 and then select 2 for A2. Then, if I select 'No' for A1, "2" stays in A2 until I actually click on A2 to select a new value (4,5,6). Is there any way to "clear" A2 once I've changed the A1 selection?
Thanks!

put this in the VBA code for your worksheet:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = Range("A1").Address Then
Dim dependentCell As Range
Set dependentCell = Target.Offset(1, 0) 'Cell A2
If dependentCell.Validation.Value = False Then dependentCell.Clear
End If
End Sub

Put this code in the code page for your Worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address Then
Range("A2").ClearContents
End If
End Sub

You could insert code for refreshing A2 list in worksheet_change procedure for your worksheet. Every time a cell value is changed in that worksheet, your updating code will run.

Related

Put first cell's value depend on which row is selected, into another cell

i need to write a cell value in other cell by clicking a range of cells as below:
my range is between b1:x30 and i want every time i click on these selection range then the value of b1 to b30 (depending which row selected) will write in cell z1
i tried to write a code as below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B1:X30")) Is Nothing Then
If Intersect(Target, Range("B1:X30")) Then
Worksheets("Order Sheet").Range("Z1").Value = .Range("B1:B30").Value
End If
End If
End If
End Sub
i know this code is not complete yet and i need to help to complete.

How to display value based on highlighted cell?

Is there a way for a cell in Excel to show a value based on just selecting another cell? Example:
Column A has a list of car manufacturers. Column B has a list of vehicle models. When the user puts the cursor in cell A4 then cell D2 would display the content of cell B4. If the user clicks cell A3 then cell D2 now displays the content of cell B3. Is there a way to put a formula in cell D2 for this purpose without any macros? It would change value based on selecting a different cell in column A.
The code given below should work. It will be triggered everytime you select a value in Column "Make".
Paste the below code in the VB editor :
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim currentRow As Long
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A:A")) Is Nothing Then
currentRow = Target.Row
Range("D2").Value = Cells(currentRow, 2).Value
End If
End If
End Sub
Go to Excel Developer Mode (Alt + F11)
Doube Click The Sheet1(Sheet1) in Left Side
Paste The code And Save
Sub test()
Worksheets("Sheet1").Activate
Set selectedCell = Application.ActiveCell
Range("D2").Value = selectedCell.Row
End Sub
D2 is a your highlighted Cell
And Goto Excel Sheet and Open Macro
Select Cell & use ctrl+q

select cell in range inputs value into cell in another sheet

In the code below that i got working, when i select a cell in a range, an input box pops up and my input is sent to the same cell i clicked but in sheet 2. I want to take this one step further. i want to bypass the input box completely and just send the value F, and i only want to do this after i click cell b2. so cell b2 would have to work as some kind of toggle (maybe put an invisible shape over it to act as a button?)
Example 1: sheet 1, select cell B2 turns on macro, select cell in range example: D10 inputs the letter F into cell D10 on sheet 2, select cell B2 turns off macro so if i select cell D10 or any cell in that range nothing will happen anymore. it would also need to remove the value F from D10 if the cell is clicked again while the macro is on.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
xRtn = Application.InputBox("Insert your value please")
Sheets("2020").Range(Target.Address).Value = xRtn
End If
End If
End Sub
Untested. I'm not sure if I understood all of your objectives.
I think if you add a checkbox to your worksheet (resize and store it wherever you want; maybe in cell B2) called "Check Box 1" then the below code should work.
One way of adding a check box might be: Excel > Developer > Insert > Check Box (Form Control) (depending on your Excel version). If the Developer tab is not visible, you may need to get it to show first.
Option Explicit
Private Function GetCheckBox() As Shape
Set GetCheckBox = Me.Shapes("Check Box 1")
End Function
Private Function IsCheckBoxTicked() As Boolean
IsCheckBoxTicked = (GetCheckBox.OLEFormat.Object.Value = 1)
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge <> 1 Then Exit Sub
If Intersect(Target, Me.Range("D9:AS20")) Is Nothing Then Exit Sub
If IsCheckBoxTicked() Then
With ThisWorkbook.Worksheets("2020").Range(Target.Address)
.Value = IIf(.Value = "F", Empty, "F")
End With
End If
End Sub

Need to be able to double click cells on sheet1 to creat a list on sheet2

So I would like to be able to double click multiples cells on sheet1 to creat a list of sorts on sheet2.
The problem with the code I have now is that it just replaces content in one cell on sheet2 instead of seeing it already has content then transferring content to the next cell below it.
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("E:P")) Is Nothing Then
If Target.Cells.Count = 1 then
Cancel = True
Sheets("Sheet1").Range("S13") = Target.Value
End If
End If
End Sub
So for example I need to be able to double click on E12 sheet1 it fills S13 on sheet2 with its content. Then I double click on E15 on sheet1 and it sees that S13 on sheet2 has content already so it fills S14 on sheet2 with its content.

Update value in Excel Field based on Value in another

I am trying to update the value of Cell A1 to reflect the value in B1, but without using formulas in Cell A1. For Example, if the value in B1=5, then By default the value in A1 should be updated to 5. The user needs to be able to type in a number in A1 if B1 is blank, but once B1 has a value, then A1 should always reflect that value. I have tried VBA but it doesn't work. I am hoping there is a way to do this without VBA. Any help is appreciated.
It isn't possible without using VBA
To copy B1 to A1 if B1 isn't empty using VBA (every time a cell is changed)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not IsEmpty(Range("B1").Value) Then
Range("A1").Value = Range("B1").Value
End If
End Sub
Same thing but if B1 becomes empty, it clears A1
Private clear As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not IsEmpty(Range("B1").Value) Then
clear = True
Range("A1").Value = Range("B1").Value
ElseIf clear = true Then
clear = False
Range("A1").Value = ""
End If
End Sub

Resources