please can I have a vba module to include into an existing Worksheet Activate event to…
- User
please can I have a vba module to include into an existing Worksheet Activate event to do the following:
Copy the columns A:B in sheet 'Incident' and paste into sheet 'Tracking' K:L but as values only
- Assistant
Certainly! Below is a VBA code snippet that achieves your requirements. You can insert this code into the
Worksheet_Activateevent of the sheet you are working with.Step-by-Step Instructions:
- Open Excel and press
ALT + F11to enter the Visual Basic for Applications (VBA) editor. - In the Project Explorer window, find the worksheet where you want to add the
Worksheet_Activateevent. - Double-click on that worksheet to open its code window.
- Copy and paste the following code into the code window:
- Open Excel and press
- User
the copy and paste did not stop. Is there a way of determining the last entry in column in sheet 'Incident' and only paste those values into column K:L
- Assistant
Certainly! You can modify the code to dynamically determine the last entry in columns A and B of the "Incident" sheet. This way, you will only copy the rows with data and paste them into columns K and L of the "Tracking" sheet.
Here’s the updated code that achieves this:
Private Sub Worksheet_Activate() ' Declare variables for the source and destination sheets Dim sourceSheet As Worksheet Dim destinationSheet As Worksheet Dim lastRow As Long ' Set the source and destination sheets Set sourceSheet = ThisWorkbook.Sheets("Incident") Set destinationSheet = ThisWorkbook.Sheets("Tracking") ' Clear the destination columns K:L before pasting destinationSheet.Range("K:L").ClearContents ' Determine the last row with data in column A of the 'Incident' sheet lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row ' Copy the range A:B up to the last row and paste as values into columns K:L in 'Tracking' sourceSheet.Range("A1:B" & lastRow).Copy destinationSheet.Range("K1").PasteSpecial Paste:=xlPasteValues ' Clear the clipboard to free memory Application.CutCopyMode = False End Sub - User
I only want the code below to activate if the value in column X of the current target row is "Open"
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Tracking") 'Application.EnableEvents = False Static previousRow As Range Dim currentRow As Range
' Check if the selected cell is within the desired range If Not Intersect(Target, Me.Range("A2:W3000")) Is Nothing Then ' Define the range of the current row from A to W Set currentRow = Me.Range("A" & Target.Row & ":W" & Target.Row) 'On Error Resume Next ' If previousRow is not empty, reset its interior color to white for A to W If Not previousRow Is Nothing Then previousRow.Interior.Color = RGB(255, 255, 255) End If ' Set the interior color of the current row to light gray (191, 191, 191) currentRow.Interior.Color = RGB(191, 191, 191) ' Store the current row in previousRow variable Set previousRow = currentRow 'On Error GoTo 0 Else ' If a different cell outside the range was selected, reset previousRow color to white for A to W If Not previousRow Is Nothing Then previousRow.Interior.Color = RGB(255, 255, 255) Set previousRow = Nothing End If End If 'Application.EnableEvents = TrueEnd Sub
- Assistant