Home > Blockchain >  Switch data between multiple identical sheets without losing any of the data
Switch data between multiple identical sheets without losing any of the data

Time:01-24

I have an ecxel workbook that has 20 tabs named after bed numbers for a residence. Each sheet is formatted identical and contains demographic data for the individual occupying the bed. The data is entered from a user form. I need a solution to change bed assignments without requiring user to reenter all the dats. I have thought to address this in one of two ways. I can create a form that lists the names of those occupying the beds and user will assign bed # to each individual, then rename each sheet. Or pull all the data from each sheet and re insert it to the correct sheet based on the bed change. I apologize if this is confusing. I can typically find answers but I am unsure how to even ask this question. Essentially I either need a solution to switch data between sheets without losing any of the data or rename all the sheets based on the the user entry.

CodePudding user response:

Assuming you have a form like this

enter image description here

Create a data sheet like with Load and Save buttons.

enter image description here

Load will fill the data sheet from the bed forms. Reallocate beds in column B and Save back to forms. I have included basic error and validation checks and a backup save after load for added security.

Option Explicit

Private Sub btnLoad_Click()

    Dim ws As Worksheet, wsData As Worksheet, r As Long
    Dim b As Long, c As Long, lastcol As Long, addr As String
   
    Set wsData = Sheets("Data")
    lastcol = wsData.Cells(2, Columns.Count).End(xlToLeft).Column
    For Each ws In Sheets
       If ws.Name Like "Bed #*" Then
            b = CLng(Mid(ws.Name, 4))
            r = b   3
            wsData.Cells(r, "B") = b
            For c = 3 To lastcol
                addr = wsData.Cells(2, c)
                wsData.Cells(r, c) = ws.Range(addr).Value2
            Next
       End If
    Next
    
    ' save backup
    With Application
       .ScreenUpdating = False
       .DisplayAlerts = False
       wsData.Copy
        ActiveWorkbook.SaveAs Filename:="Data_" & Format(Now, "yyyymmdd_hhmmss") & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close False
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
    
End Sub

Private Sub btnSave_Click()

    Dim ws As Worksheet, wsData As Worksheet, msg As String
    Dim b As Long, c As Long, lastcol As Long, addr As String
    
    ' get allocations bed to data row
    Dim dict, r As Long
    Set dict = CreateObject("Scripting.Dictionary")
    For r = 4 To 13
       If Not IsNumeric(Sheets("Data").Cells(r, "B")) Then
           MsgBox "Invalid bed no" & b, vbCritical, r
           Exit Sub
       End If
       b = Sheets("Data").Cells(r, "B")
       ' sanity check
       If dict.exists(b) Then
           MsgBox "Duplicate bed " & b, vbCritical, r
           Exit Sub
        ElseIf b < 1 Or b > 20 Then
           MsgBox "Invalid bed no " & b, vbCritical, r
           Exit Sub
        Else
            dict.Add b, r
        End If
    Next
    
    Set wsData = Sheets("Data")
    lastcol = wsData.Cells(2, Columns.Count).End(xlToLeft).Column
    For Each ws In Sheets
       If ws.Name Like "Bed #*" Then
            b = CLng(Mid(ws.Name, 4))
            r = dict(b) ' data row from dictonary
            ' is there a change
            If r <> b   3 Then
                For c = 3 To lastcol
                    addr = wsData.Cells(2, c)
                    ws.Range(addr).Value2 = wsData.Cells(r, c)
                Next
                msg = msg & vbLf & "Bed " & b
            End If
       End If
    Next
    
    If msg = "" Then
       MsgBox "No changes made", vbInformation
    Else
       MsgBox "Changes made to " & msg, vbInformation
    End If
    
End Sub
  •  Tags:  
  • Related