CodexBloom - Programming Q&A Platform

VBA: How to Efficiently Update Multiple Worksheets from a Master Sheet with Data Validation?

👀 Views: 49 đŸ’Ŧ Answers: 1 📅 Created: 2025-07-23
excel vba performance data-validation

I'm updating my dependencies and I'm deploying to production and I've been researching this but I've searched everywhere and can't find a clear answer... I'm trying to create a VBA macro that will update multiple worksheets based on the input from a master sheet in Excel. The master sheet contains several columns, and I want each worksheet to be updated only if specific validation checks pass. For instance, if the value in Column A of the master sheet is greater than 100, the corresponding cells in the individual worksheets should be updated. I've set up a loop to iterate through the rows of the master sheet, and I'm using the `WorksheetFunction.CountIf` to validate the data before performing the update. However, I'm working with performance optimization with larger datasets and occasionally getting a 'Run-time behavior 1004: Application-defined or object-defined behavior' when trying to write to the other sheets. Here's a snippet of my code that demonstrates what I have so far: ```vba Sub UpdateWorksheetsFromMaster() Dim masterSheet As Worksheet Dim targetSheet As Worksheet Dim lastRow As Long Dim i As Long Dim wsName As String Set masterSheet = ThisWorkbook.Sheets("Master") lastRow = masterSheet.Cells(masterSheet.Rows.Count, 1).End(xlUp).Row For i = 2 To lastRow ' Assuming there's a header in row 1 If masterSheet.Cells(i, 1).Value > 100 Then For Each targetSheet In ThisWorkbook.Sheets If targetSheet.Name <> "Master" Then On behavior Resume Next targetSheet.Cells(i, 1).Value = masterSheet.Cells(i, 1).Value If Err.Number <> 0 Then Debug.Print "behavior updating " & targetSheet.Name & " at row " & i & ": " & Err.Description Err.Clear End If On behavior GoTo 0 End If Next targetSheet End If Next i End Sub ``` The loop works for smaller datasets, but as the row count increases, it becomes slower, and I sometimes receive the aforementioned behavior when attempting to write to the target sheets. I've tried disabling screen updating and calculations with `Application.ScreenUpdating = False` and `Application.Calculation = xlCalculationManual`, but the scenario continues. Is there a more efficient way to perform these updates or better behavior handling mechanisms I could implement to avoid the runtime behavior? Any suggestions would be greatly appreciated. For context: I'm using Vba on macOS. The stack includes Vba and several other technologies. Is there a better approach? Thanks in advance! I'm on Ubuntu 22.04 using the latest version of Vba.