Can you please write for me a VBA code that will do the following.
In my workbook 'Master Planner'
for the following sheets Day, Week, Month, Fortnight, QtrYear, HalfYear, Year
for all rows from row 2 downwards with values, using column A for the checked row
copy the row values in column A, I & K
and write to a new NotePad text document
with the row values on one line,
Values of A, I & K seperated buy a ','
and each row seperated by a double space.
Start the code with a message that says
"Tasks for Service Providers Notes"
"Select, Copy & Paste as required", vbInformation, "ALL TASKS"
Assistant
User
Can you make these alterations to the VBA code. Write the values found to a NEW Notepad text document without saving, so that I can save it if I want to. Secondly after the ',' seperator, add a space like this ', '. And finally, for all the rows in a sheet written to Notepad, add a double sapce after each row.
Assistant
Certainly! Below is the VBA code that accomplishes what you've described. The code will iterate through the specified sheets in 'Master Planner', gather values from columns A, I, and K for each relevant row, and write them to a new Notepad text document. The values will be formatted as you specified, with each value separated by a comma and rows separated by double spaces.
To use the code, open your Excel workbook 'Master Planner', press ALT + F11 to open the VBA editor, insert a new module, and then paste the code below into that module. You can run the macro to generate your text file.
vba
Sub ExportToNotepad()
Dim wb As Workbook
Dim ws As Worksheet
Dim row As Long
Dim lastRow As Long
Dim outputFileName As String
Dim fileNum As Integer
Dim outputText As String
Dim sheetsArray As Variant
Dim msg As String
' Initialize
Set wb = ThisWorkbook
sheetsArray = Array("Day", "Week", "Month", "Fortnight", "QtrYear", "HalfYear", "Year")
' Show message box
msg = "Tasks for Service Providers Notes" & vbNewLine & "Select, Copy & Paste as required"
MsgBox msg, vbInformation, "ALL TASKS"
' Prepare the output file
outputFileName = Environ("USERPROFILE") & "\Desktop\tasks_for_service_providers.txt"
fileNum = FreeFile
' Create or overwrite the text file
Open outputFileName For Output As #fileNum
' Iterate through specified sheets
For Each ws In wb.Sheets(sheetsArray)
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For row = 2 To lastRow ' Start from row 2
If ws.Cells(row, "A").Value <> "" Then
' Create the output line from columns A, I, K
outputText = ws.Cells(row, "A").Value & "," & ws.Cells(row, "I").Value & "," & ws.Cells(row, "K").Value
Print #fileNum, outputText ' Write to the text file
End If
Next row
' Add double space between different sheets
Print #fileNum, vbNewLine & vbNewLine
Next ws
' Close the text file
Close #fileNum
' Notify user of completion
MsgBox "The tasks have been successfully exported to " & outputFileName, vbInformation, "Export Completed"
End Sub
How to Run the Code:
Open your Excel workbook named 'Master Planner'.
Press ALT + F11 to open the VBA editor.
In the editor, right-click on any of the items in the 'Project Explorer' pane and select Insert > Module.
Copy the code provided above and paste it into the newly created module.
Close the VBA editor.
Back in Excel, press ALT + F8, select ExportToNotepad, and click Run.
Result:
The code will create a text file named "tasks_for_service_providers.txt" on your Desktop, containing the specified data formatted as you requested. Each task from the specified sheets will be separated by double spaces. After execution, you’ll receive a confirmation message with the location of the text file.
Certainly! Below is the modified VBA code based on your requirements:
Create a new Notepad document without saving it.
Add a space after the comma separator (i.e., ', ').
Add a double space after each row written to the Notepad.
Modified VBA Code:
vba
Sub ExportToNotepad()
Dim wb As Workbook
Dim ws As Worksheet
Dim row As Long
Dim lastRow As Long
Dim outputText As String
Dim sheetsArray As Variant
Dim msg As String
Dim shellCommand As String
' Initialize
Set wb = ThisWorkbook
sheetsArray = Array("Day", "Week", "Month", "Fortnight", "QtrYear", "HalfYear", "Year")
' Show message box
msg = "Tasks for Service Providers Notes" & vbNewLine & "Select, Copy & Paste as required"
MsgBox msg, vbInformation, "ALL TASKS"
' Prepare shell command to open Notepad
shellCommand = "notepad.exe"
' Open Notepad
Shell shellCommand, vbNormalFocus
' Use Application.Wait to allow Notepad to open
Application.Wait (Now + TimeValue("0:00:01"))
' Write to Notepad
For Each ws In wb.Sheets(sheetsArray)
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
For row = 2 To lastRow ' Start from row 2
If ws.Cells(row, "A").Value <> "" Then
' Create the output line from columns A, I, K with space after comma
outputText = ws.Cells(row, "A").Value & ", " & ws.Cells(row, "I").Value & ", " & ws.Cells(row, "K").Value
' Output to Notepad (using SendKeys)
SendKeys outputText & vbNewLine & vbNewLine ' Add double space after each row
End If
Next row
Next ws
' Notify user of completion
MsgBox "Tasks have been written to Notepad. You may now save it.", vbInformation, "Export Completed"
End Sub
How to Use the Code:
Open your Excel workbook named 'Master Planner'.
Press ALT + F11 to open the VBA editor.
In the editor, right-click on any of the items in the 'Project Explorer' pane and select Insert > Module.
Copy the modified code above and paste it into the newly created module.
Close the VBA editor.
Back in Excel, press ALT + F8, select ExportToNotepad, and click Run.
Result:
The macro will run and open a new Notepad document.
It will write the task details from the specified sheets to this Notepad without saving it.
Each line will be formatted as requested, with ', ' as the separator, and there will be a double space after each row.
You can then manually save the Notepad document if desired.
Important Note:
SendKeys may not be perfectly reliable depending on system conditions and modal states. If you encounter issues with SendKeys, make sure that Notepad is the active window when the output is being sent, or consider using different methods for sending data to Notepad, such as writing to a temporary file and then opening that file in Notepad. However, doing it directly with SendKeys is the simplest approach for your immediate request without saving a file initially.