I have the following code below. Instead of using stagnant ranges (i.e., Range("AF9:AF50") & Range(AK9:BI50")) I'm looking to implement a dynamic range that runs the code starting at row 9 through the last row of data for those columns. I've been reading on how to set dynamic ranges but I can't get it to work. Any advice/assistance is greatly appreciated.
Private Sub Worksheet_Change(ByVal target As Range)
Dim cell As Range
Dim controlRng As Range, nRng As Range
Set cell = Range("AK9:BI50")
Set controlRng = Range("AF9:AF50")
Set nRng = Intersect(controlRng, target)
Application.EnableEvents = False
If Not nRng Is Nothing Then
Select Case target.Value
Case "No Promotion"
target.Offset(, 1).Value = Range("M" & target.Row).Value
target.Offset(, 4).Value = Range("P" & target.Row).Value
target.Offset(, 9).Value = ""
Case "Promotion"
target.Offset(, 1).Value = ""
target.Offset(, 4).Value = ""
target.Offset(, 9).Value = 0.07
Case "Demotion", "Partner", ""
target.Offset(, 1).Value = ""
target.Offset(, 4).Value = ""
target.Offset(, 9).Value = ""
End Select
End If
If Not Application.Intersect(cell, target) Is Nothing Then
Select Case target.Column
Case 37, 39, 43
target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
Case 38, 40, 44
target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
Case 41, 60
target.Offset(, 1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
Case 42, 61
target.Offset(, -1).Value = target.Value / Range("V" & target.Row).Value
End Select
End If
Application.EnableEvents = True
End Sub
I'm assuming your problem is dealing with the letter aspect of ranges and trying to make that dynamic? If so the syntax you may be looking for is.
Set MyRange = MySheet.Range(MySheet.Cells(TopRow, LeftCol), MySheet.Cells(LastRow, RightCol))
So For example, these two ranges will be equivalent but the first can be generated dynamically.
TopRow = 1
LeftCol = 2
LastRow = 100
RightCol = 3
Set MyRange = MySheet.Range(MySheet.Cells(TopRow, LeftCol), MySheet.Cells(LastRow, RightCol))
Set MyOtherRange = mysheet.range("B1:C100")
There are several methods to find the last row of a column, some work better in other circumstances then others. Look here https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba
I usually use this
LastRow = sht.Cells(sht.Rows.Count, ColumnNumber).End(xlUp).Row
If I understand the question correctly, the columns and starting rows are static and only a dynamic last row reference is needed.
In Worksheet_Change event:
Set cell = Range("AK9:" & LastRow("BI"))
Set controlRng = Range("AF9:" & LastRow("AF"))
Choose a row number larger than any number of rows your data will likely have (e.g.1000). Then, in the same module:
function LastRow(strColumn as string) as long
LastRow=range(strColumn & 1000).end(xlup).row
end function
VBA will re-calculate the LastRow every time the Worksheet_Change event is raised,
thus making your ranges dynamic.
Related
I have this simple bit of code that automates some dates and stuff when adding line items to a sheet. It works well, but when I insert a line in to the spreadsheet [right-click the line name > insert] an error occurs.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim plusWeek
plusWeek = Now() + 7
For Each cell In Target
'========adds closed date, deleted date if status degenerates=========
If cell.Column = 13 And cell = "Closed" Then
Target.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
End If
If cell.Column = 13 And cell = "In-Progress" Then
Target.Offset(0, -2) = ""
End If
If cell.Column = 13 And cell = "Open" Then
Target.Offset(0, -2) = ""
End If
'========adds date added if date is embty and description is not empty========
If cell.Column = 8 And IsEmpty(Target.Offset(0, 1)) And Not IsEmpty(Target.Offset(0, 0)) Then
Target.Offset(0, 1) = Format(Now(), "yyyy-mm-dd")
Target.Offset(0, 2) = Format(plusWeek, "yyyy-mm-dd")
Target.Offset(0, 5) = "Open"
End If
'========deletes date added if description is empty========
'If cell.Column = 8 And IsEmpty(Target.Offset(0, 0)) Then
' Target.Offset(0, 1) = ""
'End If
Next cell
End Sub
if I paste a line, add a line or delete a line, error 1004 occurs. The debugger highlights this line, but I can't understand where the error comes from.
If cell.Column = 8 And IsEmpty(Target.Offset(0, 1)) And Not
IsEmpty(Target.Offset(0, 0)) Then
Something like this should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range, rng As Range
Dim plusWeek
plusWeek = Now() + 7
Set rng = Application.Intersect(Target, Me.Range("H:H,M:M"))
If rng Is Nothing Then Exit Sub
On Error GoTo haveError '<< make sure events don't get left turned off
Application.EnableEvents = False '<< turn events off
For Each cell In rng.Cells
'========adds closed date, deleted date if status degenerates=========
If cell.Column = 13 Then
Select Case cell.Value
Case "Closed": cell.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
Case "In-Progress", "Open": cell.Offset(0, -2) = ""
End Select
End If
'========adds date added if date is embty and description is not empty========
If cell.Column = 8 And IsEmpty(cell.Offset(0, 1)) And Not IsEmpty(cell) Then
cell.Offset(0, 1) = Format(Now(), "yyyy-mm-dd")
cell.Offset(0, 2) = Format(plusWeek, "yyyy-mm-dd")
cell.Offset(0, 5) = "Open"
End If
'========deletes date added if description is empty========
'If cell.Column = 8 And IsEmpty(Target.Offset(0, 0)) Then
' Target.Offset(0, 1) = ""
'End If
Next cell
haveError:
Application.EnableEvents = True
End Sub
I am trying to create a nonvolatile date stamp in Column A cells as entries are made in B, C and D cells in the same row.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 2 To 10000
If Cells(i, “B”).Value <> “” And _
Cells(i, “C”).Value <> “” And _
Cells(i, “D”).Value <> “” And _
Cells(i, “A”).Value = “” Then
Cells(i, "A").Value = Date & " " & Time
Cells(i, "A").NumberFormat = "m/d/yyyy h:mm AM/PM"
End If
Next
Range("A:A").EntireColumn.AutoFit
End Sub
I made it go to 10000 for the simple fact I do not know how to tell it to go as long as entries are entered.
It appears that you want to receive a datestamp once columns B:D are filled and column A is still empty.
If you write values back to the worksheet, disable event handling and provide error control.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("B:D"), Target) Is Nothing Then
On Error GoTo exit_handler
Application.EnableEvents = False
Dim r As Range
For Each r In Intersect(Range("B:D"), Target).Rows
If Cells(r.Row, "B").Value <> vbNullString And Cells(r.Row, "C").Value <> vbNullString And _
Cells(r.Row, "D").Value <> vbNullString And Cells(r.Row, "A").Value = vbNullString Then
Cells(i, "A").Value = Now
Cells(i, "A").NumberFormat = "mm/dd/yyyy h:mm AM/PM"
End If
Next t
End If
exit_handler:
Application.EnableEvents = True
End Sub
Try this to get rid of the loop:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
If Target.Count = 1 And Target.Column > 1 And Target.Column < 5 Then
If Cells(Target.Row, "B").Value <> "" And Cells(Target.Row, "C").Value <> "" And Cells(Target.Row, "D").Value <> "" And Cells(Target.Row, "A").Value = "" Then
Cells(Target.Row, 1).Value = Now
Cells(Target.Row, 1).NumberFormat = "m/d/yyyy h:mm AM/PM"
Range("A:A").EntireColumn.AutoFit
End If
End If
End Sub
In short, when you make a change on column B C or D, it will check if All 3 for that Row are filled and then put the time stamp if it doesnt have one. Skipping the loop. If you are pasting data instead of typing it, it will not work, instead use the loop from Pawel's answer.
I have created a macro to update certain values and after these values are entered they are used to create a text file for import into our system.
Below is a screen shot of the data entry screen:
Below is the code, I have written on the worksheet:
Option Explicit
Public Rec_Cnt As Integer
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Dim Rng4 As Range
Rec_Cnt = Sheets("MD").Cells(3, 7)
Set Rng1 = Range("G2:G" & Rec_Cnt + 1)
Set Rng2 = Range("M2:M" & Rec_Cnt + 1)
Set Rng3 = Range("S2:S" & Rec_Cnt + 1)
Set Rng4 = Range("D2:E" & Rec_Cnt + 1)
If Not Application.Intersect(Target, Rng1) Is Nothing Then
If Len(Target) > 10 Then
Call Original_Ticket_Greater_Error
ElseIf Len(Target) < 10 Then
Call Original_Ticket_Lesser_Error
ElseIf Len(Target) = 10 Then
Cells(Target.Row, 8).Value = 9
Cells(Target.Row, 9).Value = "|"
Cells(Target.Row, 10).Value = "|"
Cells(Target.Row, 11).Value = "|"
Cells(Target.Row, 12).Value = "|"
Exit Sub
End If
ElseIf Not Application.Intersect(Target, Rng2) Is Nothing Then
If Len(Target) > 10 Then
Call Original_Cnj_Ticket_Greater_Error
ElseIf Len(Target) < 10 Then
Call Original_Cnj_Ticket_Lesser_Error
ElseIf Len(Target) = 10 Then
Cells(Target.Row, 14).Value = 9
Cells(Target.Row, 15).Value = "|"
Cells(Target.Row, 16).Value = "|"
Cells(Target.Row, 17).Value = "|"
Cells(Target.Row, 18).Value = "|"
Exit Sub
End If
ElseIf Not Application.Intersect(Target, Rng3) Is Nothing Then
If Len(Target) > 10 Then
Call Original_Ticket_Greater_Error
Exit Sub
ElseIf Len(Target) < 10 Then
Call Original_Ticket_Lesser_Error
Exit Sub
End If
ElseIf Not Application.Intersect(Target, Rng4) Is Nothing Then
If Cells(Target.Row, 3).Value = "Sales" Or Cells(Target.Row, 3).Value = "Sales Conjunction" Then
Cells(Target.Row, 6).Value = Application.Sum((Cells(Target.Row, 4).Value), (Cells(Target.Row, 5).Value))
Cells(Target.Row, 6).Value = Int(Cells(Target.Row, 6).Value * 100)
End If
End If
End Sub
Sub Original_Ticket_Greater_Error()
MsgBox "Original Ticket Number is more than 10 characters"
End Sub
Sub Original_Cnj_Ticket_Greater_Error()
MsgBox "Original Conj. Ticket Number is more than 10 characters"
End Sub
Sub Original_Ticket_Lesser_Error()
MsgBox "Original Ticket Number is less than 10 characters"
End Sub
Sub Original_Cnj_Ticket_Lesser_Error()
MsgBox "Original Conj. Ticket Number is less than 10 characters"
End Sub
Based on the code you can notice that I am updating certain cells only when the Target = 10 and otherwise not.
I wanted to change the font to RED when the Target is >10 or <10 and have tried couple of options but the font color doesn't change.
I have used Target.Font.Color and similar options.
Any help is much appreciated.
Thanks,
Sachin
You can achieve this with some conditional formatting.
From the Home ribbon click on Conditional Formatting and Manage Rules. Then select New Rule.
In the formula textbox, enter =INDIRECT("G"&ROW())<>10
In the applies to textbox, enter the column minus the header =$G$2:$G$1048576
Example Results:
What I am trying to do is get my macro to search the data in Column "E". If the cell value contains "string", then I would like to offset by one column to the left, verify if, in the new selected cell, cell value contains "". If the new selected cell value is "" then background color is 19, if it contains "*" then background color is -4142.
Here is the code I have so far:
Sub Set_Background_Color ()
lRow = Range("E" & Rows.Count).End(xlUp).Row
Set MR = Range("E2:E" & lRow)
For Each cell In MR
If cell.Value = "X" Then cell.Offset(, -1).Interior.ColorIndex = 19
Next
End Sub
I can't seem to figure out how to embed a new If statement after the Offset and before the .Interior.ColorIndex
I have tried this mess but you will see immediately that it does not work.
If cell.Value = "X" Then
ElseIf cell.Offset(, -1).Value = "" Then cell.Interior.ColorIndex = 19
Else: cell.Interior.ColorIndex = -4142
Any help is greatly apreciated!
So close!
Sub Set_Background_Color ()
Dim lRow As Long
Dim MR As Range
Dim cel As Range
lRow = Range("E" & Rows.Count).End(xlUp).Row
Set MR = Range("E2:E" & lRow)
For Each cel In MR
If cel.Value = "string" Then
If cel.Offset(, -1).Value = "" Then
cel.Offset(, -1).Interior.ColorIndex = 19
ElseIf cel.Offset(, -1).Value = "*" Then
cel.Offset(, -1).Interior.ColorIndex = -4142
End If
End If
Next
End Sub
If by contains "*" you mean "has any content" then:
If cell.Value = "X" Then
cell.Interior.ColorIndex = IIf(Len(cell.Offset(0, -1).Value) = 0, 19, xlNone)
End If
I know that you can have only one private sub worksheet change event however I am struggling to combine code for the events I need. I am new to VBA so any assistance or recommendations are appreciated. Would it be more efficient to use select case?
First code needed:
Private Sub Worksheet_Change(ByVal target As Range)
Dim cell As Range
Set cell = Range("AK9:AR50")
Application.EnableEvents = False
If Not Application.Intersect(cell, target) Is Nothing Then
If target.Column = 37 Then
target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 38 Then
target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
If target.Column = 39 Then
target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 40 Then
target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
If target.Column = 41 Then
target.Offset(, 1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
ElseIf target.Column = 42 Then
target.Offset(, -1).Value = target.Value / Range("V" & target.Row).Value
End If
If target.Column = 43 Then
target.Offset(, 1).Value = target.Value / Range("V" & target.Row).Value
ElseIf target.Column = 44 Then
target.Offset(, -1).Value = WorksheetFunction.RoundUp((target.Value * Range("V" & target.Row).Value), -2)
End If
End If
Application.EnableEvents = True
End Sub
Second code needed:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim controlRng, nRng As Range
Set controlRng = Range("AF9:AF1000")
Set nRng = Intersect(controlRng, Target)
If nRng Is Nothing Then Exit Sub
If Target.Value = "No Promotion" Then
Target.Offset(0, 1) = Range("M" & Target.Row).Value
ElseIf Target.Value = "Promotion" Then
Target.Offset(0, 1) = ""
ElseIf Target.Value = "Demotion" Then
Target.Offset(0, 1) = ""
ElseIf Target.Value = "Partner" Then
Target.Offset(0, 1) = ""
ElseIf Target.Value = "" Then
Target.Offset(0, 1) = ""
End If
End Sub
Select Case will certainly tidy up your code. You might also want to build in a check that Target is not more than a single cell.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim controlRng As Range, nRng As Range
Set cell = Range("AK9:AR50")
Set controlRng = Range("AF9:AF1000")
Set nRng = Intersect(controlRng, Target)
Application.EnableEvents = False
If Not Application.Intersect(cell, Target) Is Nothing Then
Select Case Target.Column
Case 37, 39, 41, 43
Target.Offset(, 1).Value = Target.Value / Range("V" & Target.Row).Value
Case 38, 40, 42, 44
Target.Offset(, -1).Value = WorksheetFunction.RoundUp((Target.Value * Range("V" & Target.Row).Value), -2)
End Select
End If
If Not nRng Is Nothing Then
Select Case Target.Value
Case "No Promotion"
Target.Offset(0, 1) = Range("M" & Target.Row).Value
Case "Promotion", "Demotion", "Partner", ""
Target.Offset(0, 1).ClearContents
End Select
End If
Application.EnableEvents = True
End Sub