How to update data that is on two worksheets but different formats

nmiller770678

New Member
Joined
Aug 9, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I have spreadsheet with a tab named Data and another named Monthly.
I have a User form that inputs the data into both these tabs with no issues.
The user form also allows for the updating of data as well as deleting of part#'s or entire records on the Data tab.
The issue I'm having is I haven't been able to get any code to work that will update changes to both the Data tab records and the Monthly records.
I am also looking for code that would delete records on both tabs but then move the remaining data so that there aren't any blank rows.

Example.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMAN
1
2Invoice #DateInvoice #Cust IDShip To StatePrepared ByExporting To?Part # 1Prod Name 1Registration 1Volume 1Lot# 1Repack 1Qty 1Total 1Part # 2Prod Name 2Registration 2Volume 2Lot# 2Repack 2Qty 2Total 2Part # 3Prod Name 3Registration 2Volume 3Lot # 3Repack 3Qty 4Total 3Part # 4Prod Name 4Registration3Volume 4Lot # 4Repack 4Qty 4Total 4Notes
36541239/28/2022654123KHKFLHJH1000-01Red Sneaker665544-0Formula123Original Order1Formula1000-04Red Sneaker w/Stripe665520-0Formula333Reorder1Formula2000-35ABlack Sneaker665543-0Formula444Reporder1FormulaFormulaFormulaNotes do not save on Monthly tab
48877659/28/2022887765KHALKH1000-01Red Sneaker665544-0Formula123Original Order1Formula
52234569/28/2022223456MYLBBNM3000-01Blue Sneaker665545-0Formula555No Reorder1Formula
64385422/1/2022438542USHKACanada4000-05Sandal55456-0Formula777Original Order1Formula4001-06Sandal w/Black SoleFormula321Reorder1Formula
72312213/4/2022231221QFYALML3002-02Black BootsFormula222Reorder2Formula
87812365/1/2023781236CCKDEML5622-01Trainer 456Original Order1Formula5622-04Trainer MultiFormula789No Reorder1Formula3451-01Boots AnkleFormula654No Reorder1Formula3002-02Black BootsFormula222Reorder2FormulaNotes do not save on Monthly tab
95216894/4/2023521689LSYCACanada4000-05Sandal55456-0Formula777Original Order2Formula
10
Data


Example.xlsx
ABCDEFGHIJKLMNOPQRST
1
2
3
4
5FormulaFormula
6Doesn't copy
7DateInvoice#Cust IDShip To StatePrepared ByExporting To?PartProd NameRegistrationVolumeQty 1Repack?Lot NumberTot Vol/QtyGroup By
89/28/2022654123KHKFLHJH1000-01Red Sneaker665544-01Original Order1232Sneaker
99/28/2022654123KHKNMHJH1000-04Red Sneaker w/Stripe665520-01Reorder3332Sneaker
109/28/2022654123KHKAZHJH2000-35ABlack Sneaker665543-01Reorder4443Sneaker
119/28/2022887765KHALKH1000-01Red Sneaker665544-01Original Order1231Sneaker
129/28/2022223456MYLBBNM3000-01Blue Sneaker665545-01No Reorder5552Sneaker
132/1/2022438542USHKACanada4000-05Sandal55456-01Original Order7771Sandal
142/1/2022438542USHKACanada4001-06Sandal w/Black Sole1Reorder3211Sandal
153/4/2022231221QFYALML3002-02Black Boots2Reorder2224Boots
165/1/2023781236CCKDEML5622-01Trainer 1Original Order4561Trainer
175/1/2023781236CCKDEML5622-04Trainer Multi1No Reorder7891Trainer
185/1/2023781236CCKDEML3451-01Boots Ankle1No Reorder6541Boots
195/1/2023781236CCKDEML3002-02Black Boots2Reorder2224Boots
204/4/2023521689LSYCACanada4000-05Sandal55456-02Original Order7774Sandal
21
22
23
Monthly
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
It is challenging to understand what you are asking for without knowing more.

The issue I'm having is I haven't been able to get any code to work that will update changes to both the Data tab records and the Monthly records.

What "changes"? Changes based on what criteria or for what action? Do you mean if a record is deleted, added or modified in one worksheet it should be deleted, added or modified in the other?

I am also looking for code that would delete records on both tabs but then move the remaining data so that there aren't any blank rows.

Again, deleting based on what or after what? Do you mean remove empty rows after records have been deleted?

Is data always located where shown?

I am guessing that you did not intend to show Invoice # in two columns in the data sheet?

In the "Monthly" worksheet you show records for more than one month?
 
Upvote 0
Hi OaklandJim.
A user can search for an invoice # (which is based off of column A invoice #) and make changes to the record which updates the record on the Data tab. I would like those same changes to be updated on all associated records on the Monthly tab. The user can also delete records from the Data tab, and I would like all associated rows on the Monthly tab to be deleted. Overall, the same data should be on both tabs just in different formats. Below is a screenshot of the actual user form; not sure if it's relevant.

Yes, the data will always be in the same columns for both Data and Monthly tabs.
Yes, the entire workbook will be used for data over years (approx. 1500-2500 records a year) so I will be adding a drop down for month and year that the user wants to report on and based on that information I would have another tab show the data in the format that they need.

The Monthly tab doesn't have to match at all times but maybe a separate button that the user pushes at the end of the month and all data on the Monthly tab is updated then? I'm still don't know how to get it into the format I need from the Data tab to the Monthly tab.


1665407616564.png
 
Upvote 0
So there may be one to four parts per row in the Data worksheet? Just so there are no surprises...Is the data in the Data worksheet actually organized and labeled exactly as shown (especially column headers) in the example?
 
Upvote 0
Is Invoice # in column A or C? I assume column C and Column A is empty?

DataToReportWorkbook.xlsm
ABC
2Invoice #DateInvoice #
36541239/28/2022654123
48877659/28/2022887765
52234569/28/2022223456
64385422/1/2022438542
72312213/4/2022231221
87812365/1/2023781236
95216894/4/2023521689
Data
 
Upvote 0
Oh man. In the data sheet there are eight columns per part #. In the Report there are 9 columns. And the columns are not in the same order. If the headers are not the same then I cannot think of a straightforward way to copy part # data into the Report for a row in the Data sheet.

DataToReportWorkbook2.xlsm
KLMNOPQRS
4123456789
5Part # 1Prod Name 1Registration 1Volume 1Lot# 1Repack 1Qty 1Total 1
6
7PartProd NameRegistrationTot Vol/QtyQty 1Repack?Lot NumberTot Vol/QtyGroup By
Report


Please advise: I sure hope that the columns for a part # are the same for the Data worksheet and the Report worksheet. Until I know otherwise I'll assume that the columns for a part # in the Report are the same as those in the Data.

DataToReportWorkbook2.xlsm
KLMNOPQRS
4123456789
5Part # 1Prod Name 1Registration 1Volume 1Lot# 1Repack 1Qty 1Total 1
6
7PartProd NameRegistrationVolumeLot NumberRepack?Qty 1TotalGroup By
Report
 
Upvote 0
So there may be one to four parts per row in the Data worksheet? Just so there are no surprises...Is the data in the Data worksheet actually organized and labeled exactly as shown (especially column headers) in the example?Y

Oh man. In the data sheet there are eight columns per part #. In the Report there are 9 columns. And the columns are not in the same order. If the headers are not the same then I cannot think of a straightforward way to copy part # data into the Report for a row in the Data sheet.

DataToReportWorkbook2.xlsm
KLMNOPQRS
4123456789
5Part # 1Prod Name 1Registration 1Volume 1Lot# 1Repack 1Qty 1Total 1
6
7PartProd NameRegistrationTot Vol/QtyQty 1Repack?Lot NumberTot Vol/QtyGroup By
Report


Please advise: I sure hope that the columns for a part # are the same for the Data worksheet and the Report worksheet. Until I know otherwise I'll assume that the columns for a part # in the Report are the same as those in the Data.

DataToReportWorkbook2.xlsm
KLMNOPQRS
4123456789
5Part # 1Prod Name 1Registration 1Volume 1Lot# 1Repack 1Qty 1Total 1
6
7PartProd NameRegistrationVolumeLot NumberRepack?Qty 1TotalGroup By
Report

Yes there can be up to 4 parts to a row on the data sheet but most often will be 1 or 2. The first post shows the sheets with the actual headers that I’m using.

Invoice # is listed twice because I started with it being in column C but realized I needed it in A. I didn’t want to have to change all the VBA I had already did so I duplicated it from C to A.

The registration, part name, qty, and volume can all be returned with a formula once the part number is put into the monthly report. This list is fairly small so that would be no issue.

At this point I am open to anything for the monthly report. I was toying with having a separate spreadsheet that is only updated at the end of the month when the user selects a reporting month and year then pushes a button. All data for the month and year selected would be returned to the spreadsheet in the below format (or any format at this point). This would alleviate the need for code to update any frequent changes….but I still can’t figure out how to copy the data into the report format below.

I was successful at using the filter function to return the invoice number and date in the first screenshot below but that’s where I got stuck again.
1665492769449.png


1665492777082.png



Thank you!!!
 
Upvote 0
Are you sure that you cannot get rid of the column A content? If you provide a link to your workbook I can try to fix your code.

Below is a link to the workbook that I set up. There is too much code to post. As is normal for me the code is a bit clunky but it works and only takes a second or so.

One reason that there is a lot of code is the four parts per row. That made the logic a bit convoluted.

Good luck! DataToReportWorkbook4.xlsm
 
Upvote 0
Here is all code. Don't laugh!

VBA Code:
Sub ConvertDataToReport()

'   -----------------------------
'           Declarations
'   -----------------------------

'   Worksheet objects 1. Data and 2. Report
    Dim wsData As Worksheet
    Dim wsReport As Worksheet
    
'   Range containing all data in the Report worksheet used to clear that data
'   before processing new data.
    Dim rAllData As Range
    
'   Data anchor cells for 1. Data worksheet, 2. Report worksheet.
    Dim rAnchorCellData As Range
    Dim rAnchorCellReport As Range
    
'   Header cells containing strings Part # 1, Part # 2 etc.
    Dim rHeaderPart1 As Range
    Dim rHeaderPart2 As Range
    Dim rHeaderPart3 As Range
    Dim rHeaderPart4 As Range
    
'   Used for iterating through a range of values.
    Dim rCell As Range
    
'   Count of rows in the Data
    Dim iDataRows As Long
    
'   Used for iterating through all data rows in the source worksheet.
    Dim iDataRow As Long

'   How many columns of data for each part #.
    Dim iDataColumnsPerPart As Long
    
'   There are this many columns of invoice data for each data row in data worksheet.
    Dim iInvoiceColumnsCount As Long
    
'   Used for iterating parts for a data row.
    Dim iPartNum As Long
    
'   Count of parts for a given data row
    Dim iPartsCountForARow As Long
    
'   While iterating all Data rows, keep track of how many Report rows have been added
    Dim iReportRowsProcessed As Long
    
'   Used to iterate through all rows in the Report wotksheet.
    Dim iReportRow As Long
    
'   Used for messaging.
    Dim sMsg As String
    
'   -----------------------------
'           Initializations
'   -----------------------------

'   The set wsData worksheet object to data worksheet
    Set wsData = ThisWorkbook.Worksheets("Data")
    
'   The set wsReport worksheet object to Report worksheet
    Set wsReport = ThisWorkbook.Worksheets("Report")
    
'   Find first cell containing the Text "Date" in Data worksheet.
    Call FindStringInSheet(wsData, "Date", rCell)
    
    If rCell Is Nothing _
     Then
        sMsg = "Unable to find the anchor cell" _
             & Chr(10) _
             & "for data in Data worksheet." _
             & Chr(10) _
             & "Looking for the word Date."
        MsgBox sMsg, vbCritical, "Processing data in Data Worksheet."
        Exit Sub
    Else
        Set rAnchorCellData = rCell
    End If

'   Find first cell containing data in Report worksheet including headers.
'   Looking for the word Date, case sensitive.
    Call FindStringInSheet(wsReport, "Date", rCell)
    
    If rCell Is Nothing _
     Then
        sMsg = "Unable to find the anchor cell" _
             & Chr(10) _
             & "for data in Report worksheet." _
             & Chr(10) _
             & "Looking for the word Date."
        MsgBox sMsg, vbCritical, "Processing data for Report Worksheet."
        Exit Sub
    Else
        Set rAnchorCellReport = rCell
    End If
    
'   Last occupied data row in the Data worksheet.
    iDataRow = rAnchorCellData.Offset(1000000).End(xlUp).Row
    
'   There are this many rows of data in the Data worksheet (not including headers).
    iDataRows = iDataRow - rAnchorCellData.Row
    
'   There are this many columns of data for each part # in the Data worksheet (AND report worksheet).
'   Columns 1. Part #, 2. Prod Name, 3. Registration, 4. Volume 5. Lot#, 6. Repack, 7. Qty, 8. Total

    iDataColumnsPerPart = 8
    
'   There are this many columns of data for each invoice inthe data worksheet (AND report worksheet).
'   Columns 1. Date, 2. Invoice #, 3. Cust ID, 4. Ship To State, 5. Prepared By, 6. Exporting To.
    iInvoiceColumnsCount = 6
        
'   ----------------------------------
'        Sort Data Based on Date
'   ----------------------------------

'   Set range object to point to all data in Data worksheet including headers'
    Set rAllData = rAnchorCellData.CurrentRegion
    
'   Sort data in ascending order, based on the column with CUSTNMBR values.
    rAllData.Sort Key1:=rAnchorCellData, _
                     Order1:=xlAscending, _
                     Header:=xlYes
                   
'   ----------------------------------------------
'        Setup Part-specific Anchor Cells (4)
'   ----------------------------------------------

'   Find each Part # header -- one though four (i.e., max of four parts per data line).
    For iPartNum = 1 To 4
    
        Call FindStringInSheet(wsData, "Part # " & iPartNum, rCell)
        
        If Not rCell Is Nothing _
         Then
            If iPartNum = 1 Then Set rHeaderPart1 = rCell
            If iPartNum = 2 Then Set rHeaderPart2 = rCell
            If iPartNum = 3 Then Set rHeaderPart3 = rCell
            If iPartNum = 4 Then Set rHeaderPart4 = rCell
        End If

    Next iPartNum

'   ----------------------------------------
'        Clear Existing Data in Report
'   ----------------------------------------

'   Clear 1000 rows of data. Width of range to clear is iInvoiceColumnsCount + iDataColumnsPerPart + 1.
    rAnchorCellReport.Offset(1).Resize(1000, iInvoiceColumnsCount + iDataColumnsPerPart + 1).Clear

'   ----------------------------------------
'        Process Invoice Data Columns
'   ----------------------------------------
    
    iReportRowsProcessed = 0
    
'   Iterate through all data rows in the Data worksheet. Put Invoice data into the Report worksheet.
    For iDataRow = 1 To iDataRows
    
'       How many parts in the row being processed? Use function CountPartsInRow.
        iPartsCountForARow = CountPartsInRow(iDataRow, rHeaderPart2, rHeaderPart3, rHeaderPart4)
        
'       For each part in the respective data row, copy invoice data in Data worksheet then
'       paste those values (columns) into the Report worksheet in the invoice data columns.
        For iReportRow = 1 To iPartsCountForARow
            iReportRowsProcessed = iReportRowsProcessed + 1
            rAnchorCellData.Offset(iDataRow).Resize(1, iInvoiceColumnsCount).Copy _
            Destination:=rAnchorCellReport.Offset(iReportRowsProcessed)
        Next iReportRow
            
    Next iDataRow
    
'   ----------------------------------------
'         Process Parts Data Columns
'   ----------------------------------------
    
    iReportRowsProcessed = 0

'   Iterate through all data rows in the Data worksheet. Put parts data into the Report worksheet.
    For iDataRow = 1 To iDataRows

'       How many parts in the row being processed? Use function CountPartsInRow.
        iPartsCountForARow = CountPartsInRow(iDataRow, rHeaderPart2, rHeaderPart3, rHeaderPart4)
            
'       ---------------------------------------------------------
'             Process Parts Data Columns for Each Data Row
'       ---------------------------------------------------------

'       Process parts for data for the Report worksheet.
        For iReportRow = 1 To iPartsCountForARow
        
'           Copy Part # 1 data to the Report.
            If iReportRow = 1 _
             Then
                iReportRowsProcessed = iReportRowsProcessed + 1
                rHeaderPart1.Offset(iDataRow).Resize(1, iDataColumnsPerPart).Copy _
                Destination:=rAnchorCellReport.Offset(iReportRowsProcessed, iInvoiceColumnsCount)
            End If

'           If there is one then copy Part # 2 data to the Report.
            If iReportRow = 2 _
             Then
                iReportRowsProcessed = iReportRowsProcessed + 1
                rHeaderPart2.Offset(iDataRow).Resize(1, iDataColumnsPerPart).Copy _
                Destination:=rAnchorCellReport.Offset(iReportRowsProcessed, iInvoiceColumnsCount)
            End If

'           If there is one then copy Part # 3 data to the Report.
            If iReportRow = 3 _
             Then
                iReportRowsProcessed = iReportRowsProcessed + 1
                rHeaderPart3.Offset(iDataRow).Resize(1, iDataColumnsPerPart).Copy _
                Destination:=rAnchorCellReport.Offset(iReportRowsProcessed, iInvoiceColumnsCount)
            End If

'           If there is one then copy Part # 4 data to the Report.
            If iReportRow = 4 _
             Then
                iReportRowsProcessed = iReportRowsProcessed + 1
                rHeaderPart4.Offset(iDataRow).Resize(1, iDataColumnsPerPart).Copy _
                Destination:=rAnchorCellReport.Offset(iReportRowsProcessed, iInvoiceColumnsCount)
            End If
        
        Next iReportRow
        
    Next iDataRow
    
End Sub

VBA Code:
' ----------------------------------------------------------------
' Procedure Name: CountPartsInRow
' Purpose: Counts number of parts in a row in data in the Data worksheet.
' Procedure Kind: Function
' Procedure Access: Public
' Parameter piDataRow (Long): Current row being processed.
' Parameter prAnchor2 (Range): "Anchor cell" for Part # 2
' Parameter prAnchor3 (Range): "Anchor cell" for Part # 3
' Parameter prAnchor4 (Range): "Anchor cell" for Part # 4
' Return Type: Long
' Author: Jim
' Date: 10/11/2022
' ----------------------------------------------------------------

Function CountPartsInRow( _
    piDataRow As Long, _
    prAnchor2 As Range, _
    prAnchor3 As Range, _
    prAnchor4 As Range) _
As Long

'   How many parts in the row being processed?

'   There is at least one part per row.
    CountPartsInRow = 1
    
'   Look in the column cotaining Part # 2 for the data row piDataRow.
'   If not empty then there is a part #2.
    If prAnchor2.Offset(piDataRow).Value <> "" Then CountPartsInRow = 2
    
'   Look in the column cotaining Part # 3 for the data row piDataRow.
'   If not empty then there is a part #3.
    If prAnchor3.Offset(piDataRow).Value <> "" Then CountPartsInRow = 3
    
'   Look in the column cotaining Part # 4 for the data row piDataRow.
'   If not empty then there is a part #4.
    If prAnchor4.Offset(piDataRow).Value <> "" Then CountPartsInRow = 4

End Function

VBA Code:
Function FindStringInSheet(pwsSheet As Worksheet, psFindString As String, Optional ByRef prCell) As String
    
    Dim rCellFound As Range
    
    Set rCellFound = Nothing
    
    FindStringInSheet = ""
    
    On Error Resume Next
    Set rCellFound = pwsSheet.Cells.Find(What:=psFindString, LookAt:=xlWhole)
    On Error GoTo 0
    
    If Not rCellFound Is Nothing _
     Then
        If Not IsMissing(prCell) Then Set prCell = rCellFound
        FindStringInSheet = rCellFound.Address
    End If
    
End Function
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,928
Members
449,094
Latest member
teemeren

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top