I would like a VBA code to do the following. In my open workbook 'Service Providers',…
- User
I would like a VBA code to do the following. In my open workbook 'Service Providers', click a link in my spreadsheet that will, capture all the data from cells J5 downwards from a closed external workbook 'Service Providers History' and from it's worksheet of the same name from where the click that activated the VBA originated. The data will be pasted into a new text document with each cell data on a seperate line and when I close the text document it is not saved.
- Assistant
Here's a VBA code that you can use to accomplish your requirements:
Sub CopyDataToTextDocument() Dim sourceWorkbook As Workbook Dim targetFilePath As String Dim targetFileNum As Integer Dim lastRow As Long Dim i As Long ' Open the external workbook Set sourceWorkbook = Workbooks.Open("C:\Path\to\ExternalWorkbook.xlsx") ' Find the last row of data in column J lastRow = sourceWorkbook.Worksheets("Service Providers History").Cells(Rows.Count, "J").End(xlUp).Row ' Initialize target text file targetFilePath = "C:\Path\to\TargetDocument.txt" targetFileNum = FreeFile ' Open the text document in write mode Open targetFilePath For Output As targetFileNum ' Iterate through cells in column J and write data to the text document For i = 5 To lastRow Print #targetFileNum, sourceWorkbook.Worksheets("Service Providers History").Range("J" & i).Value Next i ' Close the text document without saving changes Close targetFileNum ' Close the external workbook sourceWorkbook.Close False End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.