I am trying to write a vba event that does the following; I want to click on any cell in…
- User
I am trying to write a vba event that does the following;
I want to click on any cell in a row and for the active row of the active cell, copy the values of column D, E, F, & G of the same row, open a new note text document an paste the cells of the row copied. Each cell pasted must start with a new line in the text document. After the cells have been pasted, the last line will have the text 'Reminder of Reminder' pasted also.
Can you please show me how to write this event.
- Assistant
Sure, here is the VBA code for the event you described:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'Only run the code if a single cell is selected If Target.Count = 1 Then 'Copy values from columns D, E, F, and G of the active row Dim noteText As String noteText = Range("D" & Target.Row).Value & vbCrLf & _ Range("E" & Target.Row).Value & vbCrLf & _ Range("F" & Target.Row).Value & vbCrLf & _ Range("G" & Target.Row).Value & vbCrLf & _ "Reminder of Reminder"
'Create a new note text document Dim noteFile As Object Set noteFile = CreateObject("WScript.Shell").CreateShortcut(Environ$("USERPROFILE") & "\Desktop\Note.txt") 'Write the copied values to the note text document noteFile.writeline noteText noteFile.Save 'Release memory Set noteFile = Nothing End IfEnd Sub
This code should be placed in the worksheet module of the worksheet where the event should occur. To do this, right-click on the worksheet tab, select "View Code", and paste the code into the empty code window.
- User
I want to run the event from a form button and not from SelectionChange.
- Assistant
No problem. Here is the VBA code to accomplish this:
- Add a new button to your form, and assign the following macro to the button:
Private Sub btnCopyToNote_Click()
'Only run the code if a single cell is selected If TypeName(Selection) = "Range" And Selection.Cells.Count = 1 Then 'Copy values from columns D, E, F, and G of the active row Dim noteText As String noteText = Range("D" & Selection.Row).Value & vbCrLf & _ Range("E" & Selection.Row).Value & vbCrLf & _ Range("F" & Selection.Row).Value & vbCrLf & _ Range("G" & Selection.Row).Value & vbCrLf & _ "Reminder of Reminder" 'Create a new note text document Dim noteFile As Object Set noteFile = CreateObject("WScript.Shell").CreateShortcut(Environ$("USERPROFILE") & "\Desktop\Note.txt") 'Write the copied values to the note text document noteFile.writeline noteText noteFile.Save 'Release memory Set noteFile = Nothing MsgBox "Values copied to note!" Else MsgBox "Please select a single cell in the row you want to copy." End If - User
I am getting an automation error
- Assistant
This error can occur if there is an issue with the way the VBA code is interacting with the Windows Script Host Shell object. Here are a few things you can try to resolve the automation error:
-
Make sure that the Windows Script Host is enabled on your computer. To check that it is enabled, open the Command Prompt and run the command
cscript //h:cscript. If this command returns an error, you may need to enable the Windows Script Host by runningregsvr32 %systemroot%\system32\vbscript.dllin the Command Prompt as an administrator. -
Try using a different file path for the note file. Instead of saving the note file to the desktop, try saving it to a different location (such as a folder in your Documents directory). You can update the file path in the VBA code by changing the string passed to the
CreateShortcutfunction. -
Ensure that the Windows Script Host Shell object is available for use in VBA. To do this, open the VBA editor and go to Tools > References. Scroll down to find "Windows Script Host Object Model" and ensure that it is selected. If it is not selected, select it and click OK.
-