将数据从一个工作表传输到另一个工作表的方法是什么?

huangapple go评论54阅读模式
英文:

How to Transfer data from table to another in 2 different worksheets?

问题

我有2个表格A和B。表A是摘要,也是表B的数据源,表B表示表A的详细信息。

我想要实现的目标是,表A中输入的任何内容(异常、储备、百分比列)都会相应地反映在表B中。

我已经创建了一个类模块,用于读取表A中的成员并在循环中将值保留到一个键字典中。

代码运行正常,但输出结果会以表B中的汇总结果形式呈现。

最好首先在表B中对保留的帐号进行搜索,然后开始从键字典中粘贴数据。谢谢

将数据从一个工作表传输到另一个工作表的方法是什么?

'declare variables in the class module
Option Explicit

Public Acc_Num As String
Public Exceptional As String
Public Reserve As String
Public Percentage As Double

Sub test()
    Dim dict As New Dictionary

    Dim rg As Range, rd As Range
    Set rg = Intersect(Sheets("DataSource").UsedRange, Sheets("DataSource").Range("C5:H50"))
    Set rd = Intersect(Sheets("OutPut").UsedRange, Sheets("OutPut").Range("C5:J50"))
    ' Read through the data
    Dim i, z As Long, y As Variant, row As Long, cell As Variant
    Dim Acc_Num As String, Cust_Accounts As ACC
    For i = 1 To rg.Rows.Count
        For z = 1 To rd.Rows.Count
            'row = 1
            Acc_Num = rg.Cells(i, 1).Value

            If dict.Exists(Acc_Num) = False Then
                Set Cust_Accounts = New ACC
                Cust_Accounts.Acc_Num = Acc_Num
                dict.Add Key:=Cust_Accounts.Acc_Num, Item:=Cust_Accounts
            Else

                Set Cust_Accounts = dict(Acc_Num)
            End If

            For Each y In dict.Keys

                ' Update the values
                With Cust_Accounts
                    .Exceptional = .Exceptional + rg.Cells(i, 4).Value
                    rd.Cells(z, 6).Value = .Exceptional
                    .Reserve = .Reserve + rg.Cells(i, 5).Value
                    rd.Cells(z, 7).Value = .Reserve
                    .Percentage = .Percentage + rg.Cells(i, 6).Value
                    rd.Cells(z, 8).Value = .Percentage

                End With

            Next y
            row = row + 1
        Next z
    Next i

End Sub

(Note: The code you provided seems to contain HTML entities like & and ". These entities are used for encoding special characters in HTML, so make sure to adjust the code as needed if you encounter any issues related to these entities in your programming environment.)

英文:

I have 2 tables A & B. Table A is a summary and also is the data source for table B which represents a breakdown of table A.

What I want to achieve is that anything entered in columns (Exceptional, Reserve, Percentage) in Table A Reflects in Table B accordingly.

I have created a class module to read the members in Table A and reserve the values during the loop into a key dictionary.

the code works fine but the outputs come as consolidated results in Table B.

It would be ideal to do a search first on the reserved account numbers in Table B and then start pasting the data from the key dictionary. Thank you

将数据从一个工作表传输到另一个工作表的方法是什么?

'declare variables in the class module
Option Explicit

Public Acc_Num As String
Public Exceptional As String
Public Reserve As String
Public Percentage As Double

Sub test()
    Dim dict As New Dictionary

    Dim rg As Range, rd As Range
    Set rg = Intersect(Sheets("DataSource").UsedRange, Sheets("DataSource").Range("C5:H50"))
    Set rd = Intersect(Sheets("OutPut").UsedRange, Sheets("OutPut").Range("C5:J50"))
    ' Read through the data
    Dim i, z As Long, y As Variant, row As Long, cell As Variant
    Dim Acc_Num As String, Cust_Accounts As ACC
    For i = 1 To rg.Rows.Count
        For z = 1 To rd.Rows.Count
            'row = 1
            Acc_Num = rg.Cells(i, 1).Value

            If dict.Exists(Acc_Num) = False Then
                Set Cust_Accounts = New ACC
                Cust_Accounts.Acc_Num = Acc_Num
                dict.Add Key:=Cust_Accounts.Acc_Num, Item:=Cust_Accounts
            Else

                Set Cust_Accounts = dict(Acc_Num)
            End If
            
            For Each y In dict.Keys
            
                ' Update the values
                With Cust_Accounts
                    .Exceptional = .Exceptional + rg.Cells(i, 4).Value
                    rd.Cells(z, 6).Value = .Exceptional
                    .Reserve = .Reserve + rg.Cells(i, 5).Value
                    rd.Cells(z, 7).Value = .Reserve
                    .Percentage = .Percentage + rg.Cells(i, 6).Value
                    rd.Cells(z, 8).Value = .Percentage
            
                End With
        
            Next y
            row = row + 1
        Next z
    Next i
                    
End Sub

答案1

得分: 1

使用CustomerId&AgedDebt作为字典的键将允许您在表格之间创建一对一关系。

在我的代码中,我使用枚举来命名表格的列,使用数组而不是范围来提高效率,并通过将一些任务移至它们自己的函数中来简化主要代码。

Option Explicit

Public Enum DataSourceColumn
    dsCustomerNumber = 1
    dsAgedDebt = 3
    dsExceptional
    dsReserve
    dsPercentage
End Enum

Public Enum OutPutColumn
    otCustomerNumber = 1
    otAgedDebt = 5
    otExceptional
    otReserve
    otPercentage
End Enum

Private Enum ConsolidatedDataColumns
    cdCustomerNumber = 1
    cdExceptional = 2
    cdReserve
    cdPercentage
End Enum

Function wsDataSource() As Worksheet
    Set wsDataSource = ThisWorkbook.Worksheets("DataSource")
End Function

Function wsOutPut() As Worksheet
    Set wsOutPut = ThisWorkbook.Worksheets("OutPut")
End Function

Function DataSourceTable() As ListObject
    Set DataSourceTable = wsDataSource.ListObjects(1)
End Function

Function OutPutTable() As ListObject
    Set OutPutTable = wsOutPut.ListObjects(1)
End Function

Function getDataSourceCustomerIdIndexMap() As Scripting.Dictionary
    Dim SourceData As Variant
    SourceData = DataSourceTable.DataBodyRange.Value
    
    Dim DataSourceCustomerIdIndexMap As New Scripting.Dictionary
    Dim CustomerId As String
    Dim AgedDebt As String
    Dim Key As String
    Dim r As Long
    
    For r = 1 To UBound(SourceData)
        CustomerId = SourceData(r, DataSourceColumn.dsCustomerNumber)
        AgedDebt = SourceData(r, DataSourceColumn.dsAgedDebt)
        Key = getCustomerNumberAgedDebtKey(CustomerId, AgedDebt)
        DataSourceCustomerIdIndexMap.Add Key:=Key, Item:=r
    Next
    Set getDataSourceCustomerIdIndexMap = DataSourceCustomerIdIndexMap
End Function

Sub UpdateOutputTable()
    Dim SourceData As Variant
    SourceData = DataSourceTable.DataBodyRange.Value
    Dim DataSourceCustomerIdIndexMap As Scripting.Dictionary
    Set DataSourceCustomerIdIndexMap = getDataSourceCustomerIdIndexMap
    
    Dim OutPutData As Variant
    OutPutData = OutPutTable.DataBodyRange.Value
    Dim CustomerId As String
    Dim AgedDebt As String
    Dim Key As String
    Dim r As Long
    
    For r = 1 To UBound(OutPutData)
        CustomerId = OutPutData(r, OutPutColumn.otCustomerNumber)
        AgedDebt = OutPutData(r, OutPutColumn.otAgedDebt)
        Key = getCustomerNumberAgedDebtKey(CustomerId, AgedDebt)
        
        If DataSourceCustomerIdIndexMap.Exists(Key) Then
            Dim SourceRow As Long
            SourceRow = DataSourceCustomerIdIndexMap(Key)
            OutPutData(r, OutPutColumn.otExceptional) = SourceData(SourceRow, DataSourceColumn.dsExceptional)
            OutPutData(r, OutPutColumn.otReserve) = SourceData(SourceRow, DataSourceColumn.dsReserve)
            OutPutData(r, OutPutColumn.otPercentage) = SourceData(SourceRow, DataSourceColumn.dsPercentage)
        End If
    Next
    OutPutTable.DataBodyRange.Value = OutPutData
End Sub

Private Function getCustomerNumberAgedDebtKey(CustomerId As String, AgedDebt As String)
    getCustomerNumberAgedDebtKey = CustomerId & "|" & AgedDebt
End Function
英文:

Using CustomerId & AgedDebt as the key for the Dictionary will allow you to create a one to one relationship between the tables.

In my code I used enumerations to name the columns of the tables, arrays instead of ranges to make it more efficient, and simplified the main code by moving some tasks into their own functions.

Option Explicit

Public Enum DataSourceColumn
    dsCustomerNumber = 1
    dsAgedDebt = 3
    dsExceptional
    dsReserve
    dsPercentage
End Enum

Public Enum OutPutColumn
    otCustomerNumber = 1
    otAgedDebt = 5
    otExceptional
    otReserve
    otPercentage
End Enum

Private Enum ConsolidatedDataColumns
    cdCustomerNumber = 1
    cdExceptional = 2
    cdReserve
    cdPercentage
End Enum


Function wsDataSource() As Worksheet
    Set wsDataSource = ThisWorkbook.Worksheets("DataSource")
End Function

Function wsOutPut() As Worksheet
    Set wsOutPut = ThisWorkbook.Worksheets("OutPut")
End Function

Function DataSourceTable() As ListObject
    Set DataSourceTable = wsDataSource.ListObjects(1)
End Function

Function OutPutTable() As ListObject
    Set OutPutTable = wsOutPut.ListObjects(1)
End Function

Function getDataSourceCustomerIdIndexMap() As Scripting.Dictionary
    Dim SourceData As Variant
    SourceData = DataSourceTable.DataBodyRange.Value
    
    Dim DataSourceCustomerIdIndexMap As New Scripting.Dictionary
    Dim CustomerId As String
    Dim AgedDebt As String
    Dim Key As String
    Dim r As Long
    
    For r = 1 To UBound(SourceData)
        CustomerId = SourceData(r, DataSourceColumn.dsCustomerNumber)
        AgedDebt = SourceData(r, DataSourceColumn.dsAgedDebt)
        Key = getCustomerNumberAgedDebtKey(CustomerId, AgedDebt)
        DataSourceCustomerIdIndexMap.Add Key:=Key, Item:=r
    Next
    Set getDataSourceCustomerIdIndexMap = DataSourceCustomerIdIndexMap
End Function

Sub UpdateOutputTable()
    Dim SourceData As Variant
    SourceData = DataSourceTable.DataBodyRange.Value
    Dim DataSourceCustomerIdIndexMap As Scripting.Dictionary
    Set DataSourceCustomerIdIndexMap = getDataSourceCustomerIdIndexMap
    
    Dim OutPutData As Variant
    OutPutData = OutPutTable.DataBodyRange.Value
    Dim CustomerId As String
    Dim AgedDebt As String
    Dim Key As String
    Dim r As Long
    For r = 1 To UBound(OutPutData)
        CustomerId = OutPutData(r, OutPutColumn.otCustomerNumber)
        AgedDebt = OutPutData(r, OutPutColumn.otAgedDebt)
        Key = getCustomerNumberAgedDebtKey(CustomerId, AgedDebt)
        If DataSourceCustomerIdIndexMap.Exists(Key) Then
            Dim SourceRow As Long
            SourceRow = DataSourceCustomerIdIndexMap(Key)
            OutPutData(r, OutPutColumn.otExceptional) = SourceData(SourceRow, DataSourceColumn.dsExceptional)
            OutPutData(r, OutPutColumn.otReserve) = SourceData(SourceRow, DataSourceColumn.dsReserve)
            OutPutData(r, OutPutColumn.otPercentage) = SourceData(SourceRow, DataSourceColumn.dsPercentage)
        End If
    Next
    OutPutTable.DataBodyRange.Value = OutPutData
End Sub

Private Function getCustomerNumberAgedDebtKey(CustomerId As String, AgedDebt As String)
    getCustomerNumberAgedDebtKey = CustomerId & "|" & AgedDebt
End Function

huangapple
  • 本文由 发表于 2023年6月22日 10:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定