I built a goal seek formula which works, however when you increase the target cell (G3) by too much of an increase, excel keeps reiterating into the negative and cant figure out a solution. Is there a way to add a condition to make the changing cell ("b2") have to be greater than negative, or make it so goal seek guesses is high enough that it will find the ideal positive goal seek before going negative. Here is the current code.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("g3")) Is Nothing Then
Range("g21").GoalSeek Goal:=1.3, ChangingCell:=Range("b2")
End If
End Sub
Figured it out. I set the changing range to be set to well above any potential answer, and then it will work every time.
> Private Sub Worksheet_Change(ByVal Target As Range)
> If Not Application.Intersect(Target, Range("g3")) Is Nothing Then
> Range("b2") = 1000000000
> Range("g21").GoalSeek Goal:=1.3, ChangingCell:=Range("b2")
> End If End Sub
Related
I want to change the formatting of a cell if the value changes. I just need it to be shaded in.
I'm trying to use the following:
Private Sub Worksheet_Change(ByVal Target As Range)
Target.Interior.ColorIndex = 45
End Sub
However this is far too sensitive. For example if i delete a row, the entire row changes colour as technically it's a change. Even if I go into a cell, make no change and come out of it, it changes the formatting.
Is there anyway to edit the above so it only formats the cell's when the value has changed?
Thanks
I think you could use below code as a base for your further logic.
The idea is: we need to remember value before it is changed, so best to read it when some range is selected (since you are talking about single cell, I didn't consider ranges consisting of more cells). The value would be stored in global variable ValueOnEnter.
When the change is finally made, we compare values "before" and after", and if they differ, change the color.
Public ValueOnEnter As String
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Value <> ValueOnEnter Then
Target.Interior.ColorIndex = 45
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
ValueOnEnter = Target.Value
End Sub
So I've set up this spreadsheet at work - I end up having to do a lot of fiddly Excel tasks, though I barely know any VBA - and I want a table to automatically filter itself after the worksheet has been edited. The problem is that the column which is being filtered is full of formulas, which change in response to edits made by the user, and the filter gets applied before the column has finished calculating, producing erratic results. So really what I want is to apply AutoFilter after the worksheet has been calculated, rather than after it has been edited.
The macro I have been using is:
Private Sub Worksheet_Change(ByVal Target As Range)
With ActiveWorkbook.Worksheets("Library").ListObjects("Library")
.AutoFilter.ApplyFilter
End With
End Sub
Naively changing the activating event to Worksheet_Calculate() doesn't work - it appears to be repeatedly applying the filter over and over again.
I'm sure this is a relatively simple one and I just don't know what it is I need to do. Can you guys advise?
(PS first time posting here - hope I have done everything right!)
Never mind, have solved it myself. For anyone else with the problem, I just set calculation to manual and then replaced my code with:
Private Sub Worksheet_Change(ByVal Target As Range)
Calculate
ActiveWorkbook.Worksheets("Library").ListObjects("Library").AutoFilter.ApplyFilter
End Sub
Is there a way prevent copy/paste for some columns in Excel?.
Here is a potential workaround although it may not be satisfactory; you can make a range essentially unselectable therefore no copying or pasting would work as you can't actually select the data.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim LockedRange As Range: Set LockedRange = Me.Range(Columns(1), Columns(3))
If Not Application.Intersect(Target, LockedRange) Is Nothing Then
Me.Range("D1").Select
End If
End Sub
I am trying to create a pop up message that will appear only when a certain word appears in a range of cells in a spreadsheet. Currently the macro I have written displays the pop up message whenever anything is entered. Here is the code I have:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G10:G40")) Is Nothing Then
MsgBox "Exact dimensions needed for ceramic pipe due to required shop fabrication. This can affect both pipe costs and leadtime."
End If
End Sub
Again, I only want the pop up to appear when the word DURA-CORE II appears in the range of cells. I will admit right now that I know next to nothing about VBA, so I'm sure the fix is fairly simple.
Any help is appreciated.
Something like this?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G10:G40")) Is Nothing Then
If Target = "DURA-CORE II" Then
MsgBox "Exact dimensions needed for ceramic pipe due to required shop fabrication. This can affect both pipe costs and leadtime."
End If
End If
End Sub
you could use
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Range("G10:G40").Find(what:="DURA-CORE II", LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True) Is Nothing Then
MsgBox "Exact dimensions needed for ceramic pipe due to required shop fabrication. This can affect both pipe costs and leadtime."
End If
End Sub
in this way the user would always be informed as long as any cell in Range("G10:G40") has content "DURA-CORE II"
I have been trying to work this out myself for the last few days and caught myself in a bit of a one step forward three steps back cycle. I've been reluctant to bother you thinking this would have been answered somewhere else before now.
The idea is that I have a spreadsheet that has criteria in rows with separate entries in rows; in row 6 it is the status of each column entry, which when changed to "Completed" I would like the column to be hidden.
I've been floundering around with Worksheet_Change and been able to hide specific columns, but not the active column.
Any help offered would be much appreciated and I'm sorry if this has been covered elsewhere, but I've not been able to successfully apply any examples out there.
Thanks.
Whenever you have to work with worksheet_change events, you have to consider a cycle for it, due to user may delete multiple data at the same time or do a copy paste, if you only consider "Target" It would give a debugger error.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ItemMultipleData As Range
For Each ItemMultipleData In Target 'handles multiple cells, paste, del, etc
'your code (instead of using "Target" change to ItemMultipleData. IE:
'If ItemMultipleData.Value = "Completed" Then
Next ItemMultipleData
End Sub
Here is a starting point. It only checks row # 6:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("6:6")
If Intersect(Target, rng) Is Nothing Then Exit Sub
If Target.Count <> 1 Then Exit Sub
If Target.Value = "Completed" Then
Application.EnableEvents = False
Target.EntireColumn.Hidden = True
Application.EnableEvents = True
End If
End Sub
EDIT#1:
This approach assumes that only one cell at a time is being changed........that makes it easy to "find the active cell"