The following VBA code works, but is not very reactive. Can it be improved: Private Sub…
- User
The following VBA code works, but is not very reactive. Can it be improved: Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim targetRow As Long Static previousRow As Range
codeIf Target.CountLarge > 1 Then Exit Sub Me.Unprotect Password:="edit" Application.EnableEvents = False Application.ScreenUpdating = False If Not Intersect(Target, Me.Range("A2:K51")) Is Nothing Then If Not previousRow Is Nothing Then previousRow.Interior.Color = RGB(255, 255, 255) End If targetRow = Target.Row If Target <> "" Then Me.Range("A" & targetRow & ":K" & targetRow).Interior.Color = RGB(191, 191, 191) End If Set previousRow = Me.Range("A" & targetRow & ":K" & targetRow) Else If Not previousRow Is Nothing Then previousRow.Interior.Color = RGB(255, 255, 255) Set previousRow = Nothing End If End If Application.ScreenUpdating = True Application.EnableEvents = True Me.Protect Password:="edit"End Sub
- Assistant
- User
when I leave a sheet for another sheet or diactivate a sheet, is it possible to have a vba code that will select I1 before going to the other sheet
- Assistant
Yes, you can use the
Worksheet_Deactivateevent to select a specific cell (likeI1) before the user switches to another worksheet. However, it's important to note that the selection will not be visible to the user once they navigate away from the sheet unless you are reverting back to that sheet.Here's how you can implement this in VBA:
- Open the Visual Basic for Applications (VBA) editor by pressing
ALT + F11. - In the Project Explorer window, find and select the specific worksheet where you want to apply this (e.g.,
Sheet1). - Double-click on the worksheet to open its code window.
- Open the Visual Basic for Applications (VBA) editor by pressing
- User
error: Select method of Range class failed
- Assistant
- User
does this work: Me.Range("A2:K51").Interior.Color = None
- Assistant
In VBA, setting the interior color of a range to
Noneis not directly supported. If you want to remove the fill color from a range (essentially setting it to no fill), you should use thexlNoneconstant.Here’s how you can clear the fill color for the specified range:
vbaMe.Range("A2:K51").Interior.Pattern = xlNoneExplanation:
Interior.Pattern = xlNoneeffectively removes any fill from the specified range, making it transparent and reverting it to the default appearance (no color).
Full Example:
If you're looking to integrate this into a subroutine, here’s a complete example:
vbaPrivate Sub ClearInteriorColor() ' This will clear the fill color from the range A2:K51 Me.Range("A2:K51").Interior.Pattern = xlNone End SubYou can call this procedure whenever you need to reset the interior color of that range.
- User
I ma getting an error: Application-defined or objrct-defined error on the line: Me.Range("A2:K51").Interior.Color = RGB(255, 255, 255) in the following module Private Sub Worksheet_Activate() Me.Unprotect Password:="edit" Me.Range("I1").Select Me.Range("A2:K51").Interior.Color = RGB(255, 255, 255) Call Module3.SheetAutoWrap End Sub
- Assistant