Private Sub WorksheetActivate() Application.Calculation = xlManual…
- User
Private Sub Worksheet_Activate() Application.Calculation = xlManual 'Application.EnableEvents = False 'Application.CalculateFullRebuild End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim wss As Worksheet 'On Click goes to sheet name in column D If Target.Column = [4] Then Set wss = ThisWorkbook.Sheets(Target.Value) 'Set To the sheet named in the clicked cell 'Sheets(Target.Value).Activate wss.Activate 'If the active sheet is named “ADH” If wss.Name = "ADH" Then 'Find the next available row in Column B of the new sheet Dim nextRow As Long nextRow = wss.Cells(Rows.Count, "B").End(xlUp).Row + 1 'Copy the value from Column B of Sheet Providers wss.Range("B" & nextRow).Value = Worksheets("Providers").Range("B" & Target.Row).Value 'Copy the value of Column C of Sheet Providers to Column A on the same row of Column B wss.Range("A" & nextRow).Value = Worksheets("Providers").Range("C" & Target.Row).Value End If
Dim sheetName As String Dim rowValues As String Dim FilePath As String Dim ws As Worksheet Dim lastRow As Long Dim i As Integer
'check if selection is in column A If Target.Column <> 1 Or IsEmpty(Me.Range("D" & Target.Row)) Then Exit Sub 'Application.EnableEvents = True 'get sheet name from selected cell in column D sheetName = Me.Range("D" & Target.Row).Value
'check if sheet name exists 'On Error Resume Next 'Set ws = ThisWorkbook.Sheets(sheetName) 'If ws Is Nothing Then 'MsgBox sheetName & " sheet does not exist.", vbExclamation 'Exit Sub 'End If 'On Error GoTo 0
'get last non-blank row in column J of the sheet Set ws = ThisWorkbook.Sheets(sheetName) lastRow = ws.Cells(Rows.Count, "J").End(xlUp).Row
'get values from the last row For i = 1 To 10 'A-J rowValues = rowValues & ws.Cells(lastRow, i).Value & "," Next i rowValues = Left(rowValues, Len(rowValues) - 1) 'remove last comma
'create text file with row values FilePath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\LastJob.txt" 'replace with your desired file path 'Open FilePath For Output As #1 'Print #1, rowValues 'Close #1
Open FilePath For Output As #1 For i = 1 To 10 'A-J Print #1, ws.Cells(lastRow, i).Value Next i Close #1
'open text file Shell "notepad.exe " & FilePath, vbNormalFocus 'replace notepad.exe with your preferred text editor End If End Sub
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "2" Then ' Exit the rest of the sub if the target is I2 Application.EnableEvents = True Application.CalculateFullRebuild 'MsgBox ("Close Prompt, Press Keyboard F9 or Scroll Up Down to show Results") Exit Sub End If
- Assistant
There are multiple commented out lines of code in the Worksheet_SelectionChange event. The line that sets ws to Nothing is preventing the code from executing properly. Removing the single quote at the beginning of the line will solve the issue.
- User
- Assistant
There doesn't appear to be an error in the code. However, it's possible that the file path for the text document has changed and is no longer valid. You should check to make sure that the file path in the FilePath variable is still correct and that the user running the code has access to write to the location.