In the following code, I am having difficulty restricting the macro to only activate…
- User
In the following code, I am having difficulty restricting the macro to only activate when clicking on K2:K2001 and J2:J2001; Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Row > 1 Then Cells.Interior.ColorIndex = 0 'Reset all cells to no color Target.EntireRow.Interior.ColorIndex = 6 'Highlight active row End If
selectedCellAddress = Target.Address
Dim cell As Range Dim Rng1 As Range Dim RowNum As Integer
RowNum = ActiveCell.Row
Set Rng1 = Range("A" & RowNum & ":H" & RowNum)
codeIf Target.Cells.CountLarge > 1 Then Exit Sub 'only one cell at a time If Not Intersect(ActiveCell, Range("K2:K2001")) And ActiveCell.Offset(0, -1).Value <> "" Is Nothing Then Rng1.CopyShell "notepad.exe", vbNormalFocus Application.Wait Now + TimeValue("00:00:01") For Each cell In Rng1 SendKeys cell.Value SendKeys vbCrLf Next cell
Application.Wait Now + TimeValue("00:00:02") ThisWorkbook.Sheets(ActiveCell.Value).Activate
codeEnd IfIf Target.Cells.CountLarge > 1 Then Exit Sub 'only one cell at a time If Not Intersect(Target, Range("J2:J2001")) Is Nothing Then
MsgBox ("Action") Dim wordApp As Object Dim doc As Object Dim WshShell As Object
Set wordApp = CreateObject("Word.Application") Set doc = wordApp.Documents.Open("G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zzzz ServProvDocs\POissued.docm")
wordApp.Visible = True doc.Activate
Set WshShell = CreateObject("WScript.Shell") WshShell.AppActivate wordApp.Caption
Application.Wait (Now + TimeValue("0:00:03"))
Dim wsjrw As Worksheet Dim wscfw As Worksheet Dim activeRow As Long
Set wsjrw = Worksheets("PORDER") activeRow = ActiveCell.Row Set wscfw = Worksheets(Cells(activeRow, 11).Value) Cells(activeRow, 12).Value = "Sent" Cells(activeRow, 10).Calculate
wscfw.Activate
MsgBox ("PO details copied to Word Documnet" & vbNewLine & "" & vbNewLine & "Please Add PO:123 number into Comment Cell" & vbNewLine & "" & vbNewLine & "Comments Can Also Be Added")
End If
End Sub
- Assistant
To restrict the macro to only activate when clicking on K2:K2001 and J2:J2001, you can add an additional check to the beginning of the Sub like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Target, Range("K2:K2001,J2:J2001")) Is Nothing Then 'Added check for K2:K2001 and J2:J2001 If Target.Row > 1 Then Cells.Interior.ColorIndex = 0 'Reset all cells to no color Target.EntireRow.Interior.ColorIndex = 6 'Highlight active row End If 'Rest of your code here End If selectedCellAddress = Target.Address '... End Sub
This will ensure that the rest of the macro is only executed when the selection is within the specified range.
- User
- Assistant
Yes, you can add separate restrictions for K2:K2001 and J2:J2001 by modifying the first If statement to include multiple Intersect checks like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Row > 1 And (Not Intersect(Target, Range("K2:K2001")) Is Nothing Or Not Intersect(Target, Range("J2:J2001")) Is Nothing) Then Cells.Interior.ColorIndex = 0 'Reset all cells to no color Target.EntireRow.Interior.ColorIndex = 6 'Highlight active row 'Rest of your code here End If selectedCellAddress = Target.Address '... End Sub
This will execute the rest of the code only when the user selects a cell within K2:K2001 or J2:J2001.