In this VBA event below, sometimes this line does'nt always work 'xCWs.Cells(xC,…
- User
In this VBA event below, sometimes this line does'nt always work 'xCWs.Cells(xC, 12).Value = wsName ' Write sheet name to column L'. I am finding it difficult to solve because sometimes only 3 out of 50 instances will it not write to column L. Is there something in the code that can be improved so that it writes to column L 100% of the time.
Sub CopyServiceSchedules() 'Public Sub CopyServiceSchedules() Application.EnableEvents = False Application.ScreenUpdating = False Application.DisplayAlerts = False Dim ws As Worksheet 'On Error Resume Next For Each ws In ThisWorkbook.Worksheets ws.Unprotect Password:="edit" Next ws 'On Error GoTo 0 Dim xWs As Worksheet Dim xCWs As Worksheet Dim Xrg As Range Dim xStrName As String Dim xStr As String Dim xRStr As String Dim xRRg As Range Dim xC As Integer Dim wsName As String
On Error Resume Next Worksheets("SrvSrch").Visible = True Worksheets("SrvSrch").Unprotect Password:="edit" Worksheets("SrvSrch").Range("A1:N101").ClearContents xStr = "SrvSrch" xRStr = "Service" Set xCWs = ActiveWorkbook.Worksheets.Item(xStr) If xCWs Is Nothing Then Set xCWs = ActiveWorkbook.Worksheets.Add xCWs.Name = xStr End If
xC = 1 For Each xWs In ActiveWorkbook.Worksheets wsName = xWs.Name If Len(wsName) = 3 Then Set Xrg = xWs.Range("B:B") Set Xrg = Intersect(Xrg, xWs.UsedRange) For Each xRRg In Xrg If xRRg.Value = xRStr Then xWs.Range(xRRg.Offset(0, -1), xRRg.Offset(0, 9)).Copy 'Application.Wait Now + TimeValue("00:00:01") xCWs.Cells(xC, 1).PasteSpecial Paste:=xlPasteValues 'Application.Wait Now + TimeValue("00:00:01") xCWs.Cells(xC, 12).Value = wsName ' Write sheet name to column L xC = xC + 1 End If Next xRRg End If Next xWs Application.Wait Now + TimeValue("00:00:01") Worksheets("SrvSrch").Visible = False Application.CutCopyMode = False Application.DisplayAlerts = True Application.ScreenUpdating = True Application.EnableEvents = True Dim wss As Worksheet Set wss = ThisWorkbook.Sheets("Service Schedules") wss.Activate End Sub
- Assistant
One possible reason why the line 'xCWs.Cells(xC, 12).Value = wsName' may not always work is if there are errors in the data being copied from the other sheets. If there are any blank cells or errors in the copied data, it may cause the code to skip writing the sheet name to column L.
You can try adding error handling within the loop to check for any errors before writing the sheet name to column L. Additionally, you can also check if the copied range is not empty before writing the sheet name.
Here is an example of how you can modify the code to include error handling:
VBAFor Each xRRg In Xrg If IsError(xRRg.Value) Then ' Skip any cells with errors ElseIf xRRg.Value = xRStr Then xWs.Range(xRRg.Offset(0, -1), xRRg.Offset(0, 9)).Copy xCWs.Cells(xC, 1).PasteSpecial Paste:=xlPasteValues If Not IsEmpty(xCWs.Cells(xC, 1).Value) Then ' Check if the copied range is not empty xCWs.Cells(xC, 12).Value = wsName ' Write sheet name to column L End If xC = xC + 1 End If Next xRRg - User
There are no blanks and I also believe that there are no errors. The code captures and all the data I require and writes the information to each row. It just sometimes does not write to column L. Sometimes, it will write to column L, but most of the time out of 50 records it just will not write to 2 sometimes 3 cells in column L
- Assistant
Thank you for the additional information. If there are no blanks or errors in the data being copied, one possible reason for the intermittent issue of not writing to column L could be related to the timing of the copy and paste operations within the loop.
You can try adding a short delay after the paste operation before writing to column L to ensure that the paste operation is completed before writing the sheet name. This can be achieved using the
Application.Waitmethod.Here is an example of how you can modify the code to include a delay after the paste operation:
VBAFor Each xRRg In Xrg If xRRg.Value = xRStr Then xWs.Range(xRRg.Offset(0, -1), xRRg.Offset(0, 9)).Copy xCWs.Cells(xC, 1).PasteSpecial Paste:=xlPasteValues Application.Wait Now + TimeValue("00:00:01") ' Add a short delay after paste operation xCWs.Cells(xC, 12).Value = wsName ' Write sheet name to column L xC = xC + 1 End If Next xRRg - User
the wait did not change it
- Assistant
If adding a delay did not resolve the issue, another approach you can try is to directly set the value of the cell in column L without relying on the copy and paste operation. This method can help ensure the sheet name is written to column L without any dependency on the copy operation.
You can modify the code as follows to directly set the value of the cell in column L:
VBAFor Each xRRg In Xrg If xRRg.Value = xRStr Then xCWs.Cells(xC, 1).Value = xWs.Range(xRRg.Offset(0, -1), xRRg.Offset(0, 9)).Value xCWs.Cells(xC, 12).Value = wsName ' Write sheet name to column L xC = xC + 1 End If Next xRRgIn this approach, instead of copying and pasting the data, the value of the range from the source sheet is directly assigned to the corresponding cell in the destination sheet. This way, you can ensure that the sheet name is written to column L for each relevant row in all instances.
Please try this modification and see if it helps to consistently write the sheet name to column L. Let me know if you encounter any further issues.
- User
with your ammendment noting was copied to the destination sheet
- Assistant
If directly setting the value of the cell in the destination sheet did not copy the data successfully, you can try a different approach to ensure that the data is transferred accurately. One way to accomplish this is by looping through each cell in the range that needs to be copied and setting the value of each cell individually in the destination sheet.
Here is an updated version of the loop that sets the values of each cell individually:
VBADim i As Integer For Each xRRg In Xrg If xRRg.Value = xRStr Then For i = 0 To 9 xCWs.Cells(xC, i + 1).Value = xWs.Cells(xRRg.Row, xRRg.Column - 1 + i).Value Next i xCWs.Cells(xC, 12).Value = wsName ' Write sheet name to column L xC = xC + 1 End If Next xRRgIn this code snippet, each cell in the range being copied is individually set to the corresponding cell in the destination sheet. The loop goes through each cell in the range and transfers the value cell by cell. This method can help ensure that the data is copied accurately to the destination sheet.