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
Related
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.
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
I have rows from 1-100.
I know how to target specific cells and get data from them, but how would I do this when any row from 1 to 100 can be changed?
Say you put anything into Row A3. How would you write "Updated" into row B3 via VBA?
I want this to apply to rows A1-A100.
Thanks
Place the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, Intersection As Range, Cell As Range
Set A = Range("A1:A100")
Set Intersection = Intersect(Target, A)
If Intersection Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each Cell In Intersection
Cell.Offset(0, 1).Value = "Updated"
Next Cell
Application.EnableEvents = True
End Sub
Open VBA Editor
Double click on the sheet you event take action (sheets appears in the left top box)
Select Worksheet on the left box above code box
Select change on the right box above code box
Paste the code
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With ThisWorkbook.Worksheets("Sheet1")
If Not Intersect(Target, .Range("A1:A100")) Is Nothing Then
Application.EnableEvents = False
.Range("B" & Target.Row).Value = "Updated"
Application.EnableEvents = True
End If
End With
End Sub
I have successfully used the code below to clear the contents of an indirect cell based on a change of a dropdown list in order to prevent mismatched data.
For example my first drop down cell is D2 and has 3 options, when an option is selected, my second cell F2 (based on named ranges) changes to show the choices specific to the selected option.
When a user changes the first option in D2, the code triggers and clears out the contents of F2 so a new selection can be made preventing mis-matched data.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
'Clear Adjustment Reasons if there is a change of Type'
If Target.Address = "$D$2" Then
If Target.Validation.Type = "$F$2" Then
Application.EnableEvents = False
Target.Offset(0, 2).ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
What i'm looking to do is now extend this so it can work on a sheet where the dropdown lists will be in each cell of a column (ranged from M7:M500), and the indirect dropdown in each cell of (ranged from N7:N500)
So, when the user changes the original option in any of the cells in the M range, then the corresponding N cell in the same row will clear.
Does anyone know any examples i could potentially look at to make this work?
I'm still fairly new to excel but I can just about work out some simple formulas. I'm looking to create a placeholder text effect. The way I want to achieve this is like so;
Cell A1 : has the place holder text and is hidden.
Below this, cell B1 containing the formula.
This formula checks to see if Cell C1 is blank if C1 is blank it returns a value to C1. That value being the value of A1.
This is what I have in my head but I'm unsure on how to code this. I have bounced around the web for a while but I can't find a specific answer. The closest I have gotten is;
=IF(C6<>"","",C4)
Just to clarify Im looking to oput the result of formula B1 into C1.
OK, so put this in the worksheet's code module. Make sure you put it in the module for the specific sheet you're monitoring.
First, fill your range C5 through C99 with the formula like =$C$1. You should only need to do this one time, the macro will take care of it later.
Private Sub Worksheet_Change(ByVal Target As Range)
'the formula reference
Dim defaultFormula As String
defaultFormula = "=$C$1"
'The default text cell:
Dim defaultText As Range
Set defaultText = Range("C1")
'The cells you want to monitor:
Dim rng As Range
Set rng = Range("C5:C999") '## Modify as needed
'Cell iterator
Dim cl As Range
If Intersect(Target, rng) Is Nothing Then Exit Sub
'Avoid infinite looping
Application.EnableEvents = False
'If the user has deleted the value in the cell, then replace it with the formula:
For Each cl In Intersect(Target, rng)
If Trim(cl.Value) = vbNullString Then
cl.Formula = defaultFormula
End If
Next
'Turn on Events:
Application.EnableEvents = True
End Sub
What this does is (hopefully) pretty self-explanatory from the comments in the above code The Change event is raised any time a cell on the worksheet is changed.