Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub TransferData()
- Dim mainSheet As Worksheet
- Dim destSheet As Worksheet
- Dim lastRow As Long
- Dim i As Long
- Set mainSheet = ThisWorkbook.Worksheets("Main")
- ' Loop through each district sheet and clear its data
- For Each destSheet In ThisWorkbook.Worksheets
- If destSheet.Name <> "Main" And destSheet.Name <> "Districts" Then
- destSheet.Cells.ClearContents
- End If
- Next destSheet
- lastRow = mainSheet.Cells(mainSheet.Rows.Count, "A").End(xlUp).Row
- ' Loop through the rows in the main sheet and transfer data to the respective district sheets
- For i = 2 To lastRow ' Assuming your data starts from row 2, change if necessary
- Dim district As String
- district = mainSheet.Cells(i, "E").Value ' Assuming the district column is column "E," change if necessary
- ' Find the respective district sheet and transfer the row data
- On Error Resume Next
- Set destSheet = ThisWorkbook.Worksheets(district)
- On Error GoTo 0
- ' If the district sheet is found, transfer the row data
- If Not destSheet Is Nothing Then
- lastRow = destSheet.Cells(destSheet.Rows.Count, "A").End(xlUp).Row
- destSheet.Rows(lastRow + 1).Value = mainSheet.Rows(i).Value
- End If
- Next i
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement