select cell in range inputs value into cell in another sheet - excel

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

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

How to clear cells when another cell becomes empty?

I'm making a list of items and totaling each item's quantity and weight.
The name of the item goes in A1, the weight of goes in C1, and the quantity goes in D1.
E1 is a formula to calculate the total weight by multiplying C1 and D1.
When I clear the contents of an item in A1, I want to automatically clear the cells of C1 and D1, which in turn will clear the weight from E1 and the total of all items later in the page?
I'd like something that would repeat for specified cells from A1-A31, and that would work automatically when I delete a cell's contents and hit enter.
You need the Worksheet.Change event to do this automatically, something like this:
Add the following code to that sheet's code module (right click on the sheet tab and hit View Code to bring it up):
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A1:A31")) Is Nothing Then Exit Sub
Dim rng As Range
For Each rng In Intersect(Target, Me.Range("A1:A31"))
If IsEmpty(rng) Then
On Error GoTo ErrorHandler
Application.EnableEvents = False
rng.Offset(, 2).Resize(, 2).ClearContents
End If
Next rng
ErrorHandler:
Application.EnableEvents = True
End Sub

Click on cells and add display sum in another cell

I have a range of values; I would like to click on a subset of this range and have the sum be displayed in another cell outside of the range.
Right now, I'm able to transfer the value of the clicked cell to another cell
but I'm unable to display the summation of the values when I click multiple cells.
This is my current code; I also have the clicked cell turn yellow to indicate that it's been selected.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B7:B8")) Is Nothing Then
Range("L7").Value = Selection.Value
Target.Interior.Color = vbYellow
End If
End If
End sub
I'm wondering if I can apply a sum function to
Range("L7").Value = Selection.Value
The status bar can be configured to show the Average, Count, Sum, etc of the selected cells. If you want to show it in a worksheet cell, try this
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1:B10")) Is Nothing Then
Range("L7").Value = WorksheetFunction.Sum(Target)
End If
End Sub

Excel and changing value between two drop down lists

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.

Resources