=IFERROR(LOOKUP(2,1/(INDIRECT("'"&D3&"'!J:J")<>""),INDIRECT("'"&D3&"'!J:J")),"") This…
- User
=IFERROR(LOOKUP(2,1/(INDIRECT("'"&D3&"'!J:J")<>""),INDIRECT("'"&D3&"'!J:J")),"") This formula located in column A, looks at column D which contains the names of sheets and then gets from the named sheet, the last entry in column J. I want to be able to click on a cell in column A and make it open a text document with all the values in the row from A to J of the sought sheet
- Assistant
Unfortunately, it is not possible to achieve this using a formula alone, as formulas are not capable of executing external commands like opening a text document.
However, you can create a macro that will perform this task. Here's one possible approach:
- Open the VBA editor by pressing Alt + F11.
- Insert a new module by clicking Insert > Module.
- User
When I Right-click on a cell in column A there is no option to select “Assign Macro”.
- Assistant
If you are unable to see the "Assign Macro" option after right-clicking on the cell in column A, it is likely that macros are disabled in your Excel file. Follow these steps to enable macros:
- Click on the "File" tab in the Excel ribbon.
- Click "Options" at the bottom of the left-hand menu.
- Click on "Trust Center" in the left-hand menu.
- Click on "Trust Center Settings" on the right-hand side of the screen.
- In the Trust Center dialog box, select "Macro Settings" in the left-hand menu.
- Select the "Enable all macros" option and click "OK".
- Click "OK" in the Trust Center dialog box.
- Save your Excel file and close it.
- Re-open your Excel file and try right-clicking on a cell in column A again. The "Assign Macro" option should now be available.
If you still cannot see the "Assign Macro" option, it is possible that your version of Excel does not support macros.
- User
can the module be run from selection change
- Assistant
Yes, you can run the macro from a
Worksheet_SelectionChangeevent instead of from a right-click context menu. Here's how:- Open the VBA editor by pressing Alt + F11.
- In the VBA Project window, double-click on the worksheet where you want to run the macro (e.g. Sheet1).
- In the worksheet's module, paste the following code:
- User
The vba code works but it does not get the values of the last non blank row in column j
- Assistant
I apologize for the confusion. Here's an updated code that will look for the last non-blank row in column J of the sheet, and then retrieve the values of the entire row (columns A to J) of that row:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 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 Then Exit Sub 'get sheet name from selected cell in column D sheetName = Me.Range("D" & Target.Row).Value '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 = "C:\Users\YourName\Documents\RowValues.txt" 'replace with your desired file path Open filePath For Output As #1 Print #1, rowValues Close #1 'open text file Shell "notepad.exe " & filePath, vbNormalFocus 'replace notepad.exe with your preferred text editor End Sub - User
if the cell in column a is blank do not run the code
- Assistant
Sure, here's the updated code that checks if the selected cell in column A is blank before executing the macro:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 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 and is not blank If Target.Column <> 1 Or IsEmpty(Target) Then Exit Sub 'get sheet name from selected cell in column D sheetName = Me.Range("D" & Target.Row).Value '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 = "C:\Users\YourName\Documents\RowValues.txt" 'replace with your desired file path Open filePath For Output As #1 Print #1, rowValues Close #1 'open text file Shell "notepad.exe " & filePath, vbNormalFocus 'replace notepad.exe with your preferred text editor End Sub - User
what about if column d is empty
- Assistant
Good point. Here's the updated code that checks if the selected cell in column A is not blank and if the corresponding cell in column D is not empty before executing the macro:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 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 and is not blank and corresponding cell in column D is not empty If Target.Column <> 1 Or IsEmpty(Target) Or IsEmpty(Me.Range("D" & Target.Row)) Then Exit Sub 'get sheet name from selected cell in column D sheetName = Me.Range("D" & Target.Row).Value '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 = "C:\Users\YourName\Documents\RowValues.txt" 'replace with your desired file path Open filePath For Output As #1 Print #1, rowValues Close #1 'open text file Shell "notepad.exe " & filePath, vbNormalFocus 'replace notepad.exe with your preferred text editor End Sub