Executing the VBA code on opening the excel instead of Changing value - excel

I have a VBA code for a Rota Sheet that is activated on change of any value in the row.
I want the code to be activated upon opening the excel.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("B2:V11")
If Not Intersect(Target, rng) Is Nothing Then
'scan each row (month)
Dim countRow As Long
Dim i As Long
For i = 1 To rng.Rows.count
If Not Intersect(Target, rng.Rows(i)) Is Nothing Then
If WorksheetFunction.CountIf(rng.Rows(i), "V") > 0 Then
countRow = 0
Dim cel As Range
For Each cel In rng.Rows(i).Cells
If cel.Value2 = "V" Then
countRow = countRow + 1
VacationChange cel, countRow
Else
VacationChange cel, 0
End If
Next cel
End If
End If
Next i
'scan each column (day)
Dim j As Long
For j = 1 To rng.Columns.count
If Not Intersect(Target, rng.Columns(j)) Is Nothing Then
If WorksheetFunction.CountIf(rng.Columns(j), "V") > 5 Then
VacationChange rng.Columns(j).Cells(0, 1), 6
Else
VacationChange rng.Columns(j).Cells(0, 1), 0
End If
End If
Next j
End If
End Sub
Private Function VacationChange(ByVal rng As Range, ByVal count As Long)
With rng.Interior
Select Case count
Case 0
'clear cell colors
.Pattern = xlNone
.Color = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
Case 1 To 3
'blue
.Pattern = xlSolid
.Color = 15773696
.TintAndShade = 0
.PatternTintAndShade = 0
Case 4 To 5
'yellow
.Pattern = xlSolid
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0
Case Else
'red
.Pattern = xlSolid
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End Select
End With
End Function
I spent efforts by trying:
1. Using below code in Workbook: which is throwing 424 error
Private Sub Workbook_Open()
Sheet1.Activate
Call Worksheet_Change(Target)
End Sub
Pasting the entire code under Workbook_Open() function which is not working
Can anyone suggest what i am missing in the code ?
Sample Output image is attached
enter image description here

The problem is that Target is an undeclared Variant in your Workbook_Open implementation. That means when it gets passed as a parameter that needs to be a Range, the implicit cast fails and results in an error 424 (Object required).
If you want to "simulate" every cell in your target range changing, you can simply loop over B2:V11 and pass it each individual cell (untested with your data, but should give the gist):
Private Sub Workbook_Open()
Sheet1.Activate
Dim cell As Range
For Each cell In Sheet1.Range("B2:V11")
'Worksheet_Change needs to be Public
Sheet1.Worksheet_Change cell
Next
End Sub
Note that this is by no means the ideal solution to what you are trying to do and is a sign that you need to refactor your code a little bit to extract the functionality that you currently have in Worksheet_Change into a free-standing procedure. If you need to run the same code from the Worksheet_Change handler, you can call that procedure.

Related

VBA Excel Highlighting cells based on cell input

I'm trying to create a VBA script to highlight a particular range of cells when a user inputs any value in the cell. For example my cell range will be a1:a5, if a user enters any value in any cells within the range, cells a1 till a5 will be highlighted in the desired color. I'm a new user with VBA and after searching for a while found the below code that might be useful. Looking for advice. Thanks.
Private Sub Highlight_Condition(ByVal Target As Range)
Dim lastRow As Long
Dim cell As Range
Dim i As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Application.EnableEvents = False
For i = lastRow To 1 Step -1
If .Range("C" & i).Value = "" Then
Debug.Print "Checking Row: " & i
.Range("A" & i).Interior.ColorIndex = 39
.Range("F" & i & ":AW" & i).Interior.ColorIndex = 39
Next i
Application.EnableEvents = True
End With
End Sub
Edit: Trying to edit the code given by teylyn to be able to remove highlight from cells if cell value is removed however I can't seem to find the solution. (The original code will highlight the cells when there is input in cells however if you remove the cell value the highlight remains there.)
If Not Intersect(Target, Range("A12:F12")) Is Nothing Then
With Range("A12:F12").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ElseIf IsEmpty(Range("A12:F12").Value) = True Then
With Range("A12:F12").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65536
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
This code does what you describe, i.e. set a fill color for range A1 to A5 when any cell in that range is edited.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
With Range("A1:A5").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
End Sub
This code needs to be put in the sheet module.
Edit: If you want the highlight to disappear if none of the five cells have a value, then you can try out this variant:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim valCount As Long
If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
' a cell in Range A1 to A5 has been edited
' we don't know if that edit was adding or deleting a cell, so ...
' ... we count how many cells in that range contain values
valCount = WorksheetFunction.CountA(Range("A1:A5"))
If valCount > 0 Then
' the range has values, so highlight
With Range("A1:A5").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Else
' the range has no values, so remove the highlight
With Range("A1:A5").Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
End If
End Sub

Conditional Formating by groups in excel

Was wondering if there was a way to set conditional formatting separately for different groups across the same column. Something like this:
The idea is that the color scale should be done independently for the groups. In Group 1: 2 is the smallest value and therefore would be red and 50 is the highest and therefore would be green (even though there are values like 114 or 1467, it shouldn't affect this range as it belongs to a different group).
Thanks in advance!
If colour scaling within groups something like:
Public Sub FormatRanges()
Dim rng1 As Range, rng2 As Range, rng3 As Range, rng As Long
Application.ScreenUpdating = False
With ActiveSheet
Set rng1 = .Range("B1:B5")
Set rng2 = .Range("B6:B10")
Set rng3 = .Range("B11:B15")
Dim myRanges()
myRanges = Array(rng1, rng2, rng3)
For rng = LBound(myRanges) To UBound(myRanges)
ApplyFormatting myRanges(rng)
Next rng
End With
Application.ScreenUpdating = True
End Sub
Public Sub ApplyFormatting(ByRef rng As Variant)
rng.FormatConditions.Delete
rng.FormatConditions.AddColorScale ColorScaleType:=3
rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
rng.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
With rng.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 7039480
.TintAndShade = 0
End With
rng.FormatConditions(1).ColorScaleCriteria(2).Type = _
xlConditionValuePercentile
rng.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With rng.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 8711167
.TintAndShade = 0
End With
rng.FormatConditions(1).ColorScaleCriteria(3).Type = _
xlConditionValueHighestValue
With rng.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 8109667
.TintAndShade = 0
End With
End Sub
Example data:
Code goes in a standard module by pressing Alt + F11 to open VBE and then right click in project and add standard module.

Protect and format specified cells based on change in a cell

I have a cellrange (U4:U50) that allows you to choose between "yes" and "no". I want, for each row, format and protect the cells on the right (V4:AL4, V4:AL4, etc until V50:AL50) when relevant cell in column A changes value.
I am able to put together only a few pieces of the code based on my little knowledge: I managed to make the desired changes happen for the row 4, based on the code below.
The protect and UNprotect sub are in ThisWorkbook and they do exactly that.
Sub Worksheet_Change(ByVal Target As Range)
Set checkRange = Application.Intersect(Target, Range("U4:U50"))
' If the change wasn't in this range then we're done
If checkRange Is Nothing Then Exit Sub
If Range("U4").Value = "Yes" Then
Range("V4:AL4").Select
Call ActiveWorkbook.UNprotect_all_sheets
With Selection
.Locked = True
End With
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark2
.TintAndShade = -9.99786370433668E-02
.PatternTintAndShade = 1
End With
Range("U4").Select
ElseIf Range("U4").Value <> "Yes" Then
Call ActiveWorkbook.UNprotect_all_sheets
Range("V4:AL4").Select
With Selection
.Locked = False
End With
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
Call ActiveWorkbook.Protect_all_sheets
End Sub
Next step is to make the code work for all the rows depending from the target range, so I started with this
Dim r As Long
Dim c As Long
' 21 targets column U
c = 21
For r = 4 To 50
If Cells(r, c).Value = "Yes" Then
'here I think the process would be to unprotect the sheet, then select from (r,c+1) to (r,c+17), apply the formatting (shade and protection), go to next r and at the end protect the sheet again
But my problem is that I do now know how to:
Select the range of cells from Cells(r,c+1) to Cells(r,c+17);
Make the instruction relative to the right row.
Any comment on that is more than welcome!!
Thanks to all of you in advance, I hope you can understand from my explication what I need to do.
I have been looking for the answer around, maybe I have not been able to look for the right wording..
You can do it this way. Generally there is no need to Select anything but I have left it in as it's not clear whether your other subs are working off a selection. You could use Resize but I can't be bothered to work out how many columns it is from V to AL.
On reflection, it's probably safe to reconfigure the first block as I have done in the second (and perhaps the unprotect should be called before the selecting in any case).
Strictly speaking the code should cater for multiple cells being changed. For this, you can change instances of Target to Target(1).
Sub Worksheet_Change(ByVal Target As Range)
Set checkRange = Application.Intersect(Target, Range("U4:U50"))
' If the change wasn't in this range then we're done
If checkRange Is Nothing Then Exit Sub
If Target.Value = "Yes" Then
Range(Cells(Target.Row, "V"), Cells(Target.Row, "AL")).Select
Call ActiveWorkbook.UNprotect_all_sheets
With Selection
.Locked = True
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark2
.TintAndShade = -9.99786370433668E-02
.PatternTintAndShade = 1
End With
End With
Else
Call ActiveWorkbook.UNprotect_all_sheets
With Range(Cells(Target.Row, "V"), Cells(Target.Row, "AL"))
.Locked = False
With .Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
End If
Call ActiveWorkbook.Protect_all_sheets
End Sub

Highlight cell rows on selection error

I have this code for my Excel worksheet. It highlights the table rows by the row you have selected but problems arise if you highlight cells in the table to the outside or you put a slicer in the table. Here it the Module I use:
Option Explicit
Public Sub HighlightTableRow(Target As Excel.Range)
Dim t As ListObject
Dim lngInTable As Long
Dim c As Long
Const COLOR_SELECT = xlThemeColorAccent1
Const COLOR_LIGHTER = 0.4
On Error Resume Next
If Target.Interior.Pattern = xlPatternSolid Then Exit Sub
For Each t In Target.Parent.ListObjects
c = c + 1
If Not Intersect(Target, t.DataBodyRange) Is Nothing Then
lngInTable = c
End If
t.Range.Interior.Pattern = xlNone
Next
If lngInTable = 0 Then Exit Sub
With Target.Parent.ListObjects(lngInTable)
With .Range.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
With .DataBodyRange
With .Resize(Target.Rows.Count).Offset(Target.Row - .Row).Interior
.ThemeColor = COLOR_SELECT
.TintAndShade = 1 - COLOR_LIGHTER
End With
End With
End With
End Sub
And I put this code in each Excel sheet so the code works:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
HighlightTableRow Target
End Sub
I have no idea how to fix this error. The formatting gets weird. Any ideas?

Excel VBA - How do I select a range corresponding to values in previous cells?

I have a very large set of data that includes start and stop times for aircraft in the NAS. I want to create a macro to make a visual representation of this data in excel, like the following:
(note: this image uses fake data)
As you can see I've done the first 7 rows by hand, but there are several data files with as many as 2500+ rows each which makes the process tedious. I've tried to create a macro but I'm confused how to search for and select the appropriate range to highlight.
Here's what I have so far:
Sub autofill()
Dim rng As Range
Dim row As Range
Dim cell As Range
'set the range of the whole search area
Set rng = Range("A2:HJ121")
For Each row In rng.Rows
Dim callsign As Variant
Set callsign = cell("contents", "A" & row)
Dim valstart As Variant
Set valstart = cell("contents", "E" & row)
Dim valstop As Variant
Set valstop = cell("contents", "F" & row)
'now select the range beginning from the column whose header matches the
'time in valstart and ends at the time which matches the time in valstop
Selection.Merge
Selection.Style = "Highlight"
Selection.Value = callsign
Next row
End Sub
What's the easiest way of selecting the rows I need?
I'm not a programmer by profession; apologies in advance if my code demonstrates sloppy technique or violates some holy programming principles. :P
Thanks!
Here's my go at VBA for this.
Option Explicit
Public Sub fillSchedule()
Dim startCol As Long
Dim endCol As Long
Dim i As Long
Dim j As Long
Dim ws As Excel.Worksheet
Dim entryTime As Single
Dim exitTime As Single
Dim formatRange As Excel.Range
Set ws = ActiveSheet
startCol = ws.Range("H:H").Column
endCol = ws.Range("HJ:HJ").Column
Call clearFormats
For i = 2 To ws.Cells(1, 1).End(xlDown).Row
entryTime = ws.Cells(i, 5).Value
exitTime = ws.Cells(i, 6).Value
Set formatRange = Nothing
For j = startCol To endCol
If (ws.Cells(1, j).Value > exitTime) Then
Exit For
End If
If ((entryTime < ws.Cells(1, j).Value) And (ws.Cells(1, j).Value < exitTime)) Then
If (formatRange Is Nothing) Then
Set formatRange = ws.Cells(i, j)
Else
Set formatRange = formatRange.Resize(, formatRange.Columns.Count + 1)
End If
End If
Next j
If (Not formatRange Is Nothing) Then
Call formatTheRange(formatRange, ws.Cells(i, "A").Value)
End If
Next i
End Sub
Private Sub clearFormats()
With ActiveSheet.Range("H2:HJ121")
.clearFormats
.ClearContents
End With
End Sub
Private Sub formatTheRange(ByRef r As Excel.Range, ByRef callsign As String)
r.HorizontalAlignment = xlCenter
r.Merge
r.Value = callsign
' Apply color
With r.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
' Apply borders
With r.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With r.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With r.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With r.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
End Sub
How about a conditional formatting solution?
Highlight all the cells from H2 to (last bottom right cell).
Use this formula:
=IF(AND((H$1>$E2),(H$1<$F2)),TRUE)
Then apply a fill. And if you're willing to give up the border and the name inside the filled range, it will work for you :).
Also, you may want to Freeze Panes from G2 so you can scroll all the way to the HJ column and still see the Callsign column.
Hope this helps

Resources