I would like to write a vba code that does a similar request to the code below. What I…
- User
I would like to write a vba code that does a similar request to the code below. What I would like is for the code to open a new excel sheet and paste the values into individual cells in a row: Sub CopyAllREP() 'Application.EnableEvents = False
Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim text As String
' Define the range to search for empty cells in column G lastRow = Cells(Rows.count, "C").End(xlUp).Row Set myRange = Range("C1:C" & lastRow)
' Loop through each cell in the range and copy the corresponding non-blank rows to a string variable For Each myCell In myRange If myCell.Value = "REP" Then ' Empty cell found, add a line of text to separate the results text = text & "CONTRACTOR - - - - - -, WORK DONE - - - - - -, TASK CODE - , Inv/Qu No. -, COST - -, PR DATE - - -, WORK TYPE - - - -, PR No. -, TIME STAMP - - - - - -, CONTRACTOR CODE" & vbCrLf
' Copy corresponding non-blank rows to text string with commas For i = 1 To 11 ' Assuming data range is from A to K If Cells(myCell.Row, i).Value <> "" Then text = text & Trim(Cells(myCell.Row, i).Value) & ", " ' Add a comma after each column value End If Next i
' Trim any extra spaces and add a single line break instead of double text = Trim(text) & vbCrLf & vbCrLf End If Next myCell
' Open a new instance of Notepad and paste the results Shell "notepad.exe", vbNormalFocus SendKeys text Application.CutCopyMode = False
' Show a message box with the results MsgBox "Results copied to Notepad. Save the file as desired.", vbInformation
'Application.EnableEvents = True
End Sub
- Assistant
Sub CopyToNewSheet() 'Application.EnableEvents = False
Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim text As String
' Define the range to search for empty cells in column G lastRow = Cells(Rows.Count, "C").End(xlUp).Row Set myRange = Range("C1:C" & lastRow)
' Add a new sheet and name it Dim ws As Worksheet Set ws = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) ws.Name = "Results"
' Loop through each cell in the range and paste the corresponding non-blank rows into individual cells in a new row Dim newRow As Long newRow = 1 For Each myCell In myRange If myCell.Value = "REP" Then newRow = newRow + 1 ' Move to the next row
For i = 1 To 11 ' Assuming data range is from A to K If Cells(myCell.Row, i).Value <> "" Then ws.Cells(newRow, i).Value = Trim(Cells(myCell.Row, i).Value) ' Paste value into corresponding cell in the new sheet End If Next i End If Next myCell
' Show a message box with the results MsgBox "Results copied to a new sheet.", vbInformation
'Application.EnableEvents = True
End Sub
- User
Instead of creating a new sheet in the current workbook, I want it to create a new workbook.
- Assistant
No problem! Here is the modified code that creates a new workbook and pastes the results into a new sheet within that workbook:
Sub CopyToNewWorkbook() 'Application.EnableEvents = False Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim text As String ' Define the range to search for empty cells in column G lastRow = Cells(Rows.Count, "C").End(xlUp).Row Set myRange = Range("C1:C" & lastRow) ' Create a new workbook Dim newWb As Workbook Set newWb = Workbooks.Add ' Add a new sheet and name it Dim newWs As Worksheet Set newWs = newWb.Sheets.Add(After:=newWb.Sheets(newWb.Sheets.Count)) newWs.Name = "Results" ' Loop through each cell in the range and paste the corresponding non-blank rows into individual cells in a new row Dim newRow As Long newRow = 1 For Each myCell In myRange If myCell.Value = "REP" Then newRow = newRow + 1 ' Move to the next row For i = 1 To 11 ' Assuming data range is from A to K If Cells(myCell.Row, i).Value <> "" Then newWs.Cells(newRow, i).Value = Trim(Cells(myCell.Row, i).Value) ' Paste value into corresponding cell in the new sheet End If Next i End If Next myCell ' Save the new workbook and close it Dim filePath As String filePath = Application.GetSaveAsFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx") If filePath <> "False" Then newWb.SaveAs Filename:=filePath newWb.Close SaveChanges:=True MsgBox "Results copied to a new workbook and saved at " & filePath, vbInformation Else newWb.Close SaveChanges:=False MsgBox "Operation cancelled", vbInformation End If 'Application.EnableEvents = True End Sub - User
Code works well, but I would like to examine the excel file before I save it.
- Assistant
Sure! Here's a modified version of the previous code that opens the newly created workbook for the user to examine before saving it:
Sub CopyToNewWorkbook() 'Application.EnableEvents = False Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim text As String ' Define the range to search for empty cells in column G lastRow = Cells(Rows.Count, "C").End(xlUp).Row Set myRange = Range("C1:C" & lastRow) ' Create a new workbook Dim newWb As Workbook Set newWb = Workbooks.Add ' Add a new sheet and name it Dim newWs As Worksheet Set newWs = newWb.Sheets.Add(After:=newWb.Sheets(newWb.Sheets.Count)) newWs.Name = "Results" ' Loop through each cell in the range and paste the corresponding non-blank rows into individual cells in a new row Dim newRow As Long newRow = 1 For Each myCell In myRange If myCell.Value = "REP" Then newRow = newRow + 1 ' Move to the next row For i = 1 To 11 ' Assuming data range is from A to K If Cells(myCell.Row, i).Value <> "" Then newWs.Cells(newRow, i).Value = Trim(Cells(myCell.Row, i).Value) ' Paste value into corresponding cell in the new sheet End If Next i End If Next myCell ' Open the new workbook for the user to examine newWb.Activate ' Show a message box with instructions to the user MsgBox "Please examine the new workbook and make any necessary changes before saving it. To save the file, click File > Save As, or use the keyboard shortcut Ctrl+S.", vbInformation, "New Workbook Created" 'Application.EnableEvents = True End Sub - User
Can the following be added to to each cell in row 1 as header: COMPANY, WORK, PR CODE, TSS Code, QUO/INV, VALUE, PR DATE, NOTES, P REQUEST, TimeStamp, Providers.
- Assistant
Certainly! Here's a modified version of the code that adds the headers to row 1 in the new sheet:
Sub CopyToNewWorkbook() 'Application.EnableEvents = False Dim i As Long, lastRow As Long Dim myRange As Range, myCell As Range Dim text As String ' Define the range to search for empty cells in column G lastRow = Cells(Rows.Count, "C").End(xlUp).Row Set myRange = Range("C1:C" & lastRow) ' Create a new workbook Dim newWb As Workbook Set newWb = Workbooks.Add ' Add a new sheet and name it Dim newWs As Worksheet Set newWs = newWb.Sheets.Add(After:=newWb.Sheets(newWb.Sheets.Count)) newWs.Name = "Results" ' Add the headers to row 1 newWs.Cells(1, 1).Value = "COMPANY" newWs.Cells(1, 2).Value = "WORK" newWs.Cells(1, 3).Value = "PR CODE" newWs.Cells(1, 4).Value = "TSS Code" newWs.Cells(1, 5).Value = "QUO/INV" newWs.Cells(1, 6).Value = "VALUE" newWs.Cells(1, 7).Value = "PR DATE" newWs.Cells(1, 8).Value = "NOTES" newWs.Cells(1, 9).Value = "P REQUEST" newWs.Cells(1, 10).Value = "Timestamp" newWs.Cells(1, 11).Value = "Providers" ' Loop through each cell in the range and paste the corresponding non-blank rows into individual cells in a new row Dim newRow As Long newRow = 1 For Each myCell In myRange If myCell.Value = "REP" Then newRow = newRow + 1 ' Move to the next row For i = 1 To 11 ' Assuming data range is from A to K If Cells(myCell.Row, i).Value <> "" Then newWs.Cells(newRow, i).Value = Trim(Cells(myCell.Row, i).Value) ' Paste value into corresponding cell in the new sheet End If Next i End If Next myCell ' Open the new workbook for the user to examine newWb.Activate ' Show a message box with instructions to the user MsgBox "Please examine the new workbook and make any necessary changes before saving it. To save the file, click File > Save As, or use the keyboard shortcut Ctrl+S.", vbInformation, "New Workbook Created" 'Application.EnableEvents = True End Sub - User
Can I change this 'If myCell.Value = “REP” Then' to use the value in cell L4
- Assistant
Sure! Here's the modified code that uses the value in cell L4:
- User
I want to select from a list in cell L4. Once I have made my selection, I want this vba code to run: CopyPRcode
- Assistant
Sure! Here's an example of how the code to select the value from a list in cell L4 and run the
CopyPRcodemacro might look:Sub RunCopyPRcode() 'Application.EnableEvents = False ' Get the search value from a dropdown list in cell L4 Dim searchValue As String searchValue = Range("L4").Value ' Call the CopyPRcode macro with the search value as an argument CopyPRcode searchValue 'Application.EnableEvents = True End SubThis code gets the search value from cell L4, assuming that the value is selected from a dropdown list. It then calls the
CopyPRcodemacro with the search value as an argument. You can replaceCopyPRcodewith the name of the macro you want to run.Note that this code assumes that the
CopyPRcodemacro is defined in the same workbook and module as theRunCopyPRcodemacro. If theCopyPRcodemacro is defined in a different workbook or module, you will need to modify the code accordingly. - User
can the code run on the Workbook Change or Selection event
- Assistant
Yes, the code can run on the
Workbook_ChangeorWorksheet_SelectionChangeevents. Here's an example of how it would look for theWorkbook_Changeevent:Private Sub Workbook_Change(ByVal Target As Range) If Not Intersect(Target, Range("L4")) Is Nothing Then ' Check if the changed cell is L4 ' Get the search value from a dropdown list in cell L4 Dim searchValue As String searchValue = Range("L4").Value ' Call the CopyPRcode macro with the search value as an argument CopyPRcode searchValue End If End SubThis code checks if the changed cell is L4 using the method, which determines if two ranges intersect. If the changed cell is L4, it gets the search value from cell L4 and calls the macro with the search value as an argument.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.