When I select a value from a list in sheet 'Order Form' cell D3
search sheet 'Suppliers' from A2 downwards for all instances of a match
then for each instance found, copy the values in the row A:F
to the next available row in sheet 'Suppliers' I:N starting from I2:N2 downwards.
Assistant
You can achieve this using Google Apps Script. Here's a sample script to implement the functionality you described:
javascript
function copyValuesToSuppliers() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var orderFormSheet = ss.getSheetByName('Order Form');
var suppliersSheet = ss.getSheetByName('Suppliers');
var searchValue = orderFormSheet.getRange('D3').getValue();
var searchRange = suppliersSheet.getRange('A2:A').getValues();
var targetRange = suppliersSheet.getRange('I2:N');
var lastRow = targetRange.getValues().filter(String).length + 2;
for (var i = 0; i < searchRange.length; i++) {
if (searchRange[i][0] === searchValue) {
var sourceRange = suppliersSheet.getRange(i + 2, 1, 1, 6);
var sourceValues = sourceRange.getValues()[0];
targetRange.offset(lastRow-2, 0, 1, 6).setValues([sourceValues]);
lastRow++;
}
}
}
User
Can you please provide the solution as an Excel VBA script
Assistant
Certainly! Here's the VBA script that accomplishes the task you described in Excel:
vba
Sub CopyValuesToSuppliers()
Dim orderFormSheet As Worksheet
Dim suppliersSheet As Worksheet
Dim searchValue As String
Dim i As Long
Dim lastRow As Long
Set orderFormSheet = ThisWorkbook.Sheets("Order Form")
Set suppliersSheet = ThisWorkbook.Sheets("Suppliers")
searchValue = orderFormSheet.Range("D3").Value
lastRow = suppliersSheet.Cells(Rows.Count, "I").End(xlUp).Row + 1
For i = 2 To suppliersSheet.Cells(Rows.Count, "A").End(xlUp).Row
If suppliersSheet.Cells(i, "A").Value = searchValue Then
suppliersSheet.Cells(lastRow, "I").Resize(1, 6).Value = suppliersSheet.Cells(i, "A").Resize(1, 6).Value
lastRow = lastRow + 1
End If
Next i
End Sub
User
Can you please modify the VBA code bellow to do the following.
After the line - ThisWorkbook.Sheets("Suppliers").Unprotect Password:="edit"
If the Value in D3 = "New Contractor"
Skip the rest of the VBA code
clear the value entered in D3 and popup a message "New Contrator will be added to Suppliers List"
and continue by going to the line
ThisWorkbook.Sheets("Suppliers").Protect Password:="edit"
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Me.Range("D3")) Is Nothing Then
Application.EnableEvents = False
Dim orderFormSheet As Worksheet
Dim suppliersSheet As Worksheet
Dim searchValue As String
Dim i As Long
Dim lastRow As Long
code
Assistant
Certainly! Here is the modified VBA code that incorporates the logic you described:
vba
To use this script, follow these steps:
In your Google Sheets document, go to Extensions > Apps Script.
Delete any code in the script editor and replace it with the given script.
Save the script.
Close the script editor.
Back in your Google Sheets document, go to Extensions > Macros > Import Macro.
Run the 'copyValuesToSuppliers' macro whenever you want to execute the functionality described.
This script will search for the selected value in the 'Suppliers' sheet and copy the values in columns A:F to the next available row in columns I:N.
To use this VBA script in your Excel workbook, follow these steps:
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
Go to Insert > Module to insert a new module.
Paste the provided script into the module window.
Close the VBA editor.
Press Alt + F8, select CopyValuesToSuppliers, and click Run.
Whenever you want to run the script, you can simply execute the CopyValuesToSuppliers macro in Excel to search for the selected value in 'Order Form' cell D3 and copy the matching rows from 'Suppliers' sheet columns A:F to the next available row in columns I:N.
Set orderFormSheet = ThisWorkbook.Sheets("Order Form")
Set suppliersSheet = ThisWorkbook.Sheets("Suppliers")
ThisWorkbook.Sheets("Suppliers").Unprotect Password:="edit"
ThisWorkbook.Sheets("Suppliers").Range("I2:N100").ClearContents
searchValue = orderFormSheet.Range("D3").Value
lastRow = suppliersSheet.Cells(Rows.count, "I").End(xlUp).row + 1
For i = 2 To suppliersSheet.Cells(Rows.count, "A").End(xlUp).row
If suppliersSheet.Cells(i, "A").Value = searchValue Then
suppliersSheet.Cells(lastRow, "I").Resize(1, 6).Value = suppliersSheet.Cells(i, "A").Resize(1, 6).Value
lastRow = lastRow + 1
End If
Next i
ThisWorkbook.Sheets("Suppliers").Protect Password:="edit"
Me.Range("D4").Select
Application.EnableEvents = True
End If
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, Me.Range("D3")) Is Nothing Then
Application.EnableEvents = False
Dim orderFormSheet As Worksheet
Dim suppliersSheet As Worksheet
Dim searchValue As String
Dim i As Long
Dim lastRow As Long
Set orderFormSheet = ThisWorkbook.Sheets("Order Form")
Set suppliersSheet = ThisWorkbook.Sheets("Suppliers")
ThisWorkbook.Sheets("Suppliers").Unprotect Password:="edit"
If orderFormSheet.Range("D3").Value = "New Contractor" Then
MsgBox "New Contractor will be added to Suppliers List"
orderFormSheet.Range("D3").ClearContents
ThisWorkbook.Sheets("Suppliers").Protect Password:="edit"
Me.Range("D4").Select
Application.EnableEvents = True
Exit Sub
End If
ThisWorkbook.Sheets("Suppliers").Range("I2:N100").ClearContents
searchValue = orderFormSheet.Range("D3").Value
lastRow = suppliersSheet.Cells(Rows.Count, "I").End(xlUp).Row + 1
For i = 2 To suppliersSheet.Cells(Rows.Count, "A").End(xlUp).Row
If suppliersSheet.Cells(i, "A").Value = searchValue Then
suppliersSheet.Cells(lastRow, "I").Resize(1, 6).Value = suppliersShe
et.Cells(i, "A").Resize(1, 6).Value
lastRow = lastRow + 1
End If
Next i
ThisWorkbook.Sheets("Suppliers").Protect Password:="edit"
Me.Range("D4").Select
Application.EnableEvents = True
End If
End Sub
This script will check if the value in cell D3 is "New Contractor." If it is, it will clear the value, display a message, and then skip the remaining code. If the value is not "New Contractor," it will proceed to execute the rest of the code as before.