My problem is I'm trying to make a sheet that update the date on the row where data is being edit.
But I have around 600 rows, and the limit for the one code (Worksheet_change) is by far exceeded before I reach the bottom.
My code is like this
Private Sub Worksheet_change(ByVal target As Range)
If Not Application.Intersect(Range("E7:AR7"), Range(target.Address)) Is Nothing Then
Range("AS7") = Range("A1")
End If
If Not Application.Intersect(Range("E8:AR8"), Range(target.Address)) Is Nothing Then
Range("AS8") = Range("A1")
End If
Just continues up to 600+. (I have a spreadsheet to write the code for me, so not that much work)
But it is when I didn't thought about the limit.
Is there a workaround or some similar code that gets the job done?
Just to make it Clear what I'm trying to do, if it isnt from the code itself.
When a user edit some data between E7:AR7 then AS7 = today()
And down the rows.
E8:AR8 then AS8 = today()
E9:AR9 then AS9 = today()
E10:AR10 then AS10 = today()
-##-
Hope you guys have some ideas :)
You don't need a separate If statement for each range.
The code below will check for changes in E7:AR700 and put the value from A1 in column AS of the row that's been changed.
You can adjust E7:AR700 to suit.
Private Sub Worksheet_change(ByVal Target As Range)
Dim rng As Range
If Not Application.Intersect(Range("E7:AR700"), Target) Is Nothing Then
Application.EnableEvents = False
For Each rng In Target.Cells
Range("AS" & rng.Row) = Range("A1")
Next rng
Application.EnableEvents = True
End If
End Sub
Related
The code below works for an individual cell, but I get type mismatch when i want to implement this for a range. Would appreciate it if someone can make the below work. Thanks.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("A159:A1034").Value = "SC" Then
Range("J159:J1034").Value = "SC:NA"
Else
Range("J159:J1034").Value = Null
End If
End Sub
Even if Range("A159:A1034").Value would work – which it doesn't – it would have to return the value of 875 cells at once. Most likely not what you want.
The easiest way to loop through the range and make the adjustments, would be something like:
Dim area As Range, c As Range
Set area = Range("A159:A1034")
For Each c In area
If c.Value = "SC" Then
c.Value = "SC:NA"
Else
c.Value = Null
End If
Next c
But I do not recommend to use that in a Worksheet_SelectionChange event for multiple reasons.
First, it will be a pretty heavy operation, which will run each time a change in selection is made. Making the whole worksheet very slow to work with.
Secondly, any cells that does contain "SC" will be changed to "SC:NA", but on next change in selection, they will all disappear. Since they will fail the check and be turned to "Null". This seems like very odd behavior.
IF you want to run it in a worksheet event, you could maybe run it in a Worksheet_Change event instead, and limit it to only run as a change in this range is made.
Something like:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim area As Range, c As Range
Set area = Range("A159:A1034")
If Not Intersect(Target, area) Is Nothing Then
Application.EnableEvents = False
If Target.Value = "SC" Then
Target.Value = "SC:NA"
Else
Target.Value = Null
End If
Application.EnableEvents = True
End If
End Sub
And if you want to trigger a check on the entire range – like the first example, make that a separate sub, that you call as a macro.
I tried searching for a solution to my XLookup via VBA problem but I couldn't find one. I have this below data set:
In the Data Set, If any cell in the range C2:C6 is blank, I want to use this formula =IF(ISBLANK(B2),"",XLOOKUP(B2,A:A,IF(ISBLANK(D:D),"",D:D))) in those cells. Where row number of B2 is variable depending upon the row we are putting this formula via VBA.
If any cell in the range C2:C6 has value, I want to use that value without any formula. And if someone deletes the value and the cell becomes blank, VBA will add above formula to that cell.
Currently in the screenshot above, all the cells in range C2:C6 has above formula.
I hope I made sense. If this is not doable, it's okay. I can always use a helper column. But I think VBA would be a more cleaner way for my Dashboard.
Many Thanks in Advance.
In the sheet's class module, put this code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
For Each rCell In Me.Range("C2:C6").Cells
If IsEmpty(rCell.Value) Then
Application.EnableEvents = False
rCell.FormulaR1C1 = "=IF(ISBLANK(RC[-1]),"""",xlookup(RC[-1],C[-2],IF(ISBLANK(C[1]),"""",C[1])))"
Application.EnableEvents = True
End If
Next rCell
End Sub
This will run every time something on the sheet changes. That can't slow things down so you don't want to try to do too much in the Change event. It does not fire on calculate, though.
This one seems to be working for any set of data. Thanks to everyone for the help:
Private Sub InsertFormula()
Dim mwRng As Range
Set mwRng = Range("C2:C250")
Dim d As Range
For Each d In mwRng
If d.Value = "" Then
d.Formula = "=IF(RC[-1]="""",""-"",INDEX(C[1],MATCH(RC[-1],C[-2],0)))"
End If
Next d
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("C2:C250")) Is Nothing Then
Application.EnableEvents = False
Call InsertFormula
Application.EnableEvents = True
End If
End Sub
I need help automatically changing cells containing a certain value whenever a specific cell on same row changes value.
E.g whenever a cell in B column changes = change TRUE to FALSE on that specific row.
My VBA knowledge is pretty much nonexistent and Im certainly a beginner.
Im fairly sure that Worksheet.Change is what Im looking for and I've been trying out some code I've found here on SO, such as:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Target.Parent.Range("B:B")) Is Nothing Then Exit Sub
For Each x In Target
Cells(x.Row, 3).Value = "False"
Next
End Sub
I know though that this doesn't replace specific values in whatever column the cells are.
I've been trying out silly things like:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Target.Parent.Range("B:B")) Is Nothing Then Exit Sub
For Each x In Target
If Cells(x.Row, x.Column).Value = "TRUE" Then Value = "FALSE"
Next
End Sub
But of course it doesnt work.
Think you could point me out a direction of what I should be researching?
Replace the change event sub on the sheet where you have your data with the code below. I think that should do the trick
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oRng As Range
Dim oCell As Range
' Check if change was in column B
If Intersect(Target, Target.Parent.Range("B:B")) Is Nothing Then Exit Sub
' Turn off events so that when we make a change on the sheet, this event is not triggered again
Application.EnableEvents = False
' Set the range to include all column in Target row
Set oRng = Target.Parent.Range("C" & Target.Row & ":" & Target.Parent.Cells(Target.Row, Target.Parent.UsedRange.Columns.Count).Address)
' Loop through all cells to change the value
For Each oCell In oRng
If Trim(LCase(oCell.Value)) = "true" Then
oCell.Value = "FALSE"
End If
Next
' Enable events again
Application.EnableEvents = True
End Sub
I need to highlight and entire row if a cell string contains the word "risk". But I need to make it using vba since the person using it will write on it after using the macro.
I have something like:
The reason will be written afterwards and I need to highlight the row if someone writes the word risk anywhere in this column. Anything can be written there.
I use this to highlite a row when I want a full match:
lastReg= Cells(Rows.Count, 1).End(xlUp).Row
Set Rng = Range("A1:J" & lastReg)
Rng.FormatConditions.Add Type:=xlExpression, Formula1:="=$J1=""Plat"""
...
so I tried:
Rng.FormatConditions.Add Type:=xlExpression, Formula1:="=FIND(""risk"",$J1)>0"
But it doesn't work.
Edit: it gives me an execution error so the code itself doesn't run.
Edit2: Someone else uses this macro, and he can't do it by himself so I wanted the code to do it for him.
Also, the code is stored in the personal.xlsb because he runs the code in a different worksheet everyday, so I can't pre config the formatConditions for the worksheet.
I would use a worksheet change event. place this sub in your worksheet. Whenever any cell in column 5 changes and the value is "risk", it will color the row.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range
Set Rng = Columns(5)
If Not Intersect(Rng, Target) Is Nothing And Target.Value = "risk" Then
Target.Offset(, -4).Resize(, 5).Interior.Color = vbYellow
End If
End Sub
Try:
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim cell As Range
If Not Intersect(Target, Sh.UsedRange) Is Nothing Then
For Each cell In Target.Cells
With cell
If UCase(.Value) = "RISK" Then
.Font.Color = vbRed
Else
.Font.Color = vbBlack
End If
End With
Next cell
End If
End Sub
I have code that inserts rows based on a cell value in cell J17 using Target.Address. I'd like to use the similar code on the line below it; however, the location of the line below it is dependent on the number of rows that are added.
Does anyone know of a method to add the number of iterations to the rows of an address?
I know how to do this using Cells function, but I like the automatic running of the Target.Address
Here is my code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$J$17" Then
Dim i As Integer
For i = 1 To Target.Value
Cells(18, 9).EntireRow.Insert
Cells(18, 9).Value = i
Next i
End If
End Sub
Does anyone know of a method to add the number of iterations to the rows of an address?
Sure. That part is elementary. You could compute the range's new address using the Offset method. But then you need to store it somewhere. You could create a CustomDocumentProperty, a hidden worksheet or a named range variable. All of these would work just fine.
But probably the best solution is just to define that cell initially as a named range. Named ranges are neat because they are dynamic. If you insert rows/columns or delete rows/columns, the Name moves with the cell. Then, in your code, just compare versus that range's .Address property, instead of hard-coding in a value like "$J$18".
Here's how:
First, define a named range for cell J18. I named it "insert", but you can name it (almost) anything you want. Just make sure to update the reference to it in the code.
Use something like this if you are going to do different operations depending on which Target is changing.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'This handles your original J17
If Target.Address = "$J$17" Then
For i = 1 To Target.Value
Cells(18, 9).EntireRow.Insert
Cells(18, 9).Value = i
Next i
End If
'This handles the new named range:
If Target.Address = Range("insert").Address Then
'Do something else, here:
MsgBox "Success!!"
End If
Application.EnableEvents = True
End Sub
If you are doing the same insertion and numbering, then I would fancy it up like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
With Target
If .Address = "$J$17" Or .Address = Range("insert").Address Then
For i = 1 To .Value
.Offset(1, -1).EntireRow.Insert
.Offset(1, -1).Value = i
Next i
End If
End With
Application.EnableEvents = True
End Sub