Please can you write me a VBA code that will do the following. If workbook 'Premises…
- User
Please can you write me a VBA code that will do the following.
If workbook 'Premises Staff Overtime' is not open then open it in the background. Check each value in workbook 'Schedule Checks' worksheet 'Open' range R3:R8 against the values in workbook 'Premises Staff Overtime' worksheet 'Leave' range CQ2:CQ7 If a match is found, copy the value in range CT2:CT7 of the same row where the match is found of sheet 'Leave' and paste the value only into the range U3:U8 of the same row where the match exists in worksheet 'Open' close workbook 'Premises Staff Overtime' without saving and without any pop up warnings nor prompts.
- Assistant
Here is the VBA code that will do what you have described:
Sub CopyValues() Dim wb1 As Workbook Dim wb2 As Workbook Dim ws1 As Worksheet Dim ws2 As Worksheet Dim cell As Range Dim matchCell As Range On Error Resume Next Set wb1 = Workbooks("Premises Staff Overtime") On Error GoTo 0 If wb1 Is Nothing Then Set wb1 = Workbooks.Open("C:\Path\to\Premises Staff Overtime.xlsx", UpdateLinks:=0) End If Set wb2 = ThisWorkbook Set ws1 = wb1.Sheets("Leave") Set ws2 = wb2.Sheets("Open") For Each cell In ws2.Range("R3:R8") Set matchCell = ws1.Range("CQ2:CQ7").Find(cell.Value, LookIn:=xlValues) If Not matchCell Is Nothing Then ws2.Cells(matchCell.Row, "U").Value = ws1.Cells(matchCell.Row, "CT").Value End If Next cell wb1.Close SaveChanges:=False End SubPlease replace
"C:\Path\to\Premises Staff Overtime.xlsx"with the actual path to the "Premises Staff Overtime" workbook on your system. This code assumes that the workbooks have the specified sheet names and ranges as mentioned in your requirements. - User
The VBA code fetches the values but it does not past it in the correct row in sheet 'Open'. In my example of my excel sheets the match in sheet 'Leave' is on row 6 and the match in sheet 'Open' is on row 3. It should therefore paste the match value found in sheet 'Leave' into the match row 3 of sheet 'Open'. Here is the code: Sub LeaveDates() ThisWorkbook.Worksheets("Open").Unprotect Password:="edit" Application.EnableEvents = False Dim wb1 As Workbook Dim wb2 As Workbook Dim ws1 As Worksheet Dim ws2 As Worksheet Dim cell As Range Dim matchCell As Range
On Error Resume Next Set wb1 = Workbooks("Premises Staff Overtime") On Error GoTo 0 If wb1 Is Nothing Then Dim filePath As String filePath = ThisWorkbook.Path & "\Premises Staff Overtime.xlsb" Set wb1 = Workbooks.Open(filePath, UpdateLinks:=0) End If Set wb2 = ThisWorkbook Set ws1 = wb1.Sheets("Leave") Set ws2 = wb2.Sheets("Open") For Each cell In ws2.Range("R3:R8") Set matchCell = ws1.Range("CQ2:CQ7").Find(cell.Value, LookIn:=xlValues) If Not matchCell Is Nothing Then ws2.Cells(matchCell.Row, "U").Value = ws1.Cells(matchCell.Row, "CT").Value End If Next cellApplication.EnableEvents = True ThisWorkbook.Worksheets("Open").Protect Password:="edit" wb1.Close SaveChanges:=False End Sub
- Assistant
In the revised code you provided, the issue might be with the way you are identifying the row in sheet 'Open' to paste the value. Since you want to paste the value into the same row where the match exists in the 'Open' worksheet (not the 'Leave' worksheet as previously mentioned), you need to adjust the row reference accordingly.
Here is the updated code with the row reference corrected to paste the values in the correct row of sheet 'Open':
Sub LeaveDates() ThisWorkbook.Worksheets("Open").Unprotect Password:="edit" Application.EnableEvents = False Dim wb1 As Workbook Dim wb2 As Workbook Dim ws1 As Worksheet Dim ws2 As Worksheet Dim cell As Range Dim matchCell As Range Dim matchRow As Long On Error Resume Next Set wb1 = Workbooks("Premises Staff Overtime") On Error GoTo 0 If wb1 Is Nothing Then Dim filePath As String filePath = ThisWorkbook.Path & "\Premises Staff Overtime.xlsb" Set wb1 = Workbooks.Open(filePath, UpdateLinks:=0) End If Set wb2 = ThisWorkbook Set ws1 = wb1.Sheets("Leave") Set ws2 = wb2.Sheets("Open") For Each cell In ws2.Range("R3:R8") Set matchCell = ws1.Range("CQ2:CQ7").Find(cell.Value, LookIn:=xlValues) If Not matchCell Is Nothing Then matchRow = matchCell.Row - 1 ' Adjust to match the correct row in 'Open' worksheet ws2.Cells(matchRow, "U").Value = ws1.Cells(matchCell.Row, "CT").Value End If Next cell Application.EnableEvents = True ThisWorkbook.Worksheets("Open").Protect Password:="edit" wb1.Close SaveChanges:=False End Sub - User
Please can you correct the VBA code to do the following.
If workbook 'Premises Staff Overtime' is not open then open it in the background. For each value in workbook 'Schedule Checks' worksheet 'Open' range R3:R8 find the matching value in workbook 'Premises Staff Overtime' worksheet 'Leave' range CQ2:CQ7 If a match is found, in sheet 'Leave' range CQ2:CQ7, then copy the value in range CT2:CT7 of the same row of sheet 'Leave' and paste the value only into the range U3:U8 of the same row where the match exists in range R3:R8 of worksheet 'Open' close workbook 'Premises Staff Overtime' without saving and without any pop up warnings nor prompts.
- Assistant
I have modified the VBA code to paste the matched values from the 'Leave' sheet into the correct row of the 'Open' sheet based on the match found. Here is the corrected version of the code:
Sub LeaveDates() ThisWorkbook.Worksheets("Open").Unprotect Password:="edit" Application.EnableEvents = False Dim wb1 As Workbook Dim wb2 As Workbook Dim ws1 As Worksheet Dim ws2 As Worksheet Dim cell As Range Dim matchCell As Range Dim matchRow As Long On Error Resume Next Set wb1 = Workbooks("Premises Staff Overtime") On Error GoTo 0 If wb1 Is Nothing Then Dim filePath As String filePath = ThisWorkbook.Path & "\Premises Staff Overtime.xlsb" Set wb1 = Workbooks.Open(filePath, UpdateLinks:=0) End If Set wb2 = ThisWorkbook Set ws1 = wb1.Sheets("Leave") Set ws2 = wb2.Sheets("Open") For Each cell In ws2.Range("R3:R8") Set matchCell = ws1.Range("CQ2:CQ7").Find(cell.Value, LookIn:=xlValues) If Not matchCell Is Nothing Then matchRow = cell.Row ' Get the row of the current cell in 'Open' sheet ws2.Cells(matchRow, "U").Value = ws1.Cells(matchCell.Row, "CT").Value End If Next cell Application.EnableEvents = True ThisWorkbook.Worksheets("Open").Protect Password:="edit" wb1.Close SaveChanges:=False End Sub - User
Is it possible to open a workbook without it being shown: If wb1 Is Nothing Then Dim filePath As String filePath = ThisWorkbook.Path & "\Premises Staff Overtime.xlsb"
Set wb1 = Workbooks.Open(filePath, UpdateLinks:=0) End If - Assistant
Yes, it is possible to open a workbook without it being shown by setting the
Visibleproperty of the workbook toFalse. Here is how you can modify the code to open the workbook 'Premises Staff Overtime' in the background:If wb1 Is Nothing Then Dim filePath As String filePath = ThisWorkbook.Path & "\Premises Staff Overtime.xlsb" Set wb1 = Workbooks.Open(filePath, UpdateLinks:=0, ReadOnly:=True, Visible:=False) End IfBy setting
Visible:=False, the 'Premises Staff Overtime' workbook will be opened in the background without being shown to the user. - User
When I run the code I get an error 'Named argument not found' on the use of Visible:=False
- Assistant
I apologize for the confusion. To open a workbook without it being shown, you can use a different approach by activating the workbook after opening it and then setting the
Windowsproperty toFalse. Here's how you can adjust the code:If wb1 Is Nothing Then Dim filePath As String filePath = ThisWorkbook.Path & "\Premises Staff Overtime.xlsb" Set wb1 = Workbooks.Open(filePath, UpdateLinks:=0, ReadOnly:=True) wb1.Windows(1).Visible = False End IfIn this updated code, the
Windows(1).Visible = Falseline ensures that the workbook is opened in the background without displaying it to the user.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.