【问题标题】:Find and paste values from one sheet to another in selected columns在选定的列中查找并将值从一个工作表粘贴到另一个工作表
【发布时间】:2022-01-23 14:39:19
【问题描述】:

我正在尝试将值从一张表粘贴到另一张表中,两张表都有一个唯一的 B 列“Bill ID”,我想要实现的是,我在表“对账”的 p、q 和 w 列中输入值“并且当我运行代码时,它应该将这些值粘贴到账单表的 p,q,w 列中,针对相同的账单 ID,两张表中的账单 ID 都在第 2 列中。到目前为止,我已经尝试编写以下代码,但它不是在职的。还有一件事,从帐单 ID 的对帐表中,它应该只搜索从 b21 到最后一个非空行。

Dim wb As Workbook: Set wb = ThisWorkbook
Dim wsDisp As Worksheet: Set wsDisp = wb.Worksheets("Reconciliation")


Dim a As String
Dim b As String
Dim c As String
Dim e As Long
Dim F As String




Application.ScreenUpdating = False

a = wsDisp.Cells(19, 16).Value
b = wsDisp.Cells(19, 17).Value
c = wsDisp.Cells(19, 23).Value


e = MsgBox("Do You Wish to Save Recovery ? " & vbNewLine & "GIDC PAID = " & a & vbNewLine & "GST PAID = " & b & vbNewLine & " LPS PAID = " & c, vbYesNo)


If e = vbNo Then Exit Sub



For i = 21 To 400
   
    
       Sheets("Bills").Cells(Cells(i, 2), 16) = Sheets("Reconciliation").Cells(i, 16)
       Sheets("Bills").Cells(Cells(i, 2), 17) = Sheets("Reconciliation").Cells(i, 17)
       Sheets("Bills").Cells(Cells(i, 2), 23) = Sheets("Reconciliation").Cells(i, 23)

    

    Next

Application.ScreenUpdating = True

对此的任何帮助都非常感谢。谢谢

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    更新匹配行中的单元格

    Option Explicit
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Purpose:      Updates the 'Bills' worksheet.
    ' Calls:        'RefColumn','GetRange'.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Sub UpdateBills()
        
        ' Source
        Const sName As String = "Reconciliation"
        Const sfRow As Long = 21
        Const suCol As String = "B"
        Const sColsList As String = "P,Q,W"
        ' Destination
        Const dName As String = "Bills"
        Const dfRow As Long = 2
        Const duCol As String = "B"
        Const dColsList As String = "P,Q,W"
        ' Workbook
        Dim wb As Workbook: Set wb = ThisWorkbook
        
        ' Create references to the read-columns ('surg' and 'durg')
        ' and write only destination column to an array ('duData').
        
        Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
        Dim surg As Range: Set surg = RefColumn(sws.Cells(sfRow, suCol))
        If surg Is Nothing Then Exit Sub ' no data
        
        Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
        Dim durg As Range: Set durg = RefColumn(dws.Cells(dfRow, duCol))
        If durg Is Nothing Then Exit Sub ' no data
        ' This is not done for the source because 'Application.Match'
        ' is multiple times faster on a range.
        Dim duData As Variant: duData = GetRange(durg)
        
        ' Write values from the write-columns to jagged arrays.
        
        ' Split the lists (comma-separated strings) to arrays.
        Dim sCols() As String: sCols = Split(sColsList, ",")
        Dim dCols() As String: dCols = Split(dColsList, ",")
        
        Dim cUpper As Long: cUpper = UBound(sCols)
        
        ' Define the jagged arrays.
        Dim sData As Variant: ReDim sData(0 To cUpper)
        Dim dData As Variant: ReDim dData(0 To cUpper)
        
        ' Define destination empty array ('drData').
        Dim drCount As Long: drCount = UBound(duData, 1)
        Dim drData As Variant: ReDim drData(1 To drCount, 1 To 1)
        
        Dim c As Long
        
        ' Write values from the source write-columns to the source jagged array,
        ' and destination empty arrays to destination jagged array.
        For c = 0 To cUpper
            sData(c) = GetRange(surg.EntireRow.Columns(sCols(c)))
            dData(c) = drData
        Next c
    
        Erase drData
        
        ' Write matches from source (jagged) array ('sData')
        ' to destination (jagged) array ('dData').
        
        Dim dr As Long
        Dim sIndex As Variant
        
        For dr = 1 To drCount
            sIndex = Application.Match(duData(dr, 1), surg, 0)
            If IsNumeric(sIndex) Then
                For c = 0 To cUpper
                    dData(c)(dr, 1) = sData(c)(sIndex, 1)
                Next c
            End If
        Next dr
        
        Erase duData
        Erase sData
        
        ' Write values from destination array tp destination ranges.
        
        For c = 0 To cUpper
            durg.EntireRow.Columns(dCols(c)).Value = dData(c)
        Next c
    
    End Sub
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Purpose:      Creates a reference to the one-column range from the first cell
    '               of a range ('FirstCell') to the bottom-most non-empty cell
    '               of the first cell's worksheet column.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function RefColumn( _
        ByVal FirstCell As Range) _
    As Range
        If FirstCell Is Nothing Then Exit Function
        
        With FirstCell.Cells(1)
            Dim lCell As Range
            Set lCell = .Resize(.Worksheet.Rows.Count - .Row + 1) _
                .Find("*", , xlFormulas, , , xlPrevious)
            If lCell Is Nothing Then Exit Function
            Set RefColumn = .Resize(lCell.Row - .Row + 1)
        End With
    
    End Function
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Purpose:      Returns the values of a range ('rg') in a 2D one-based array.
    ' Remarks:      If ˙rg` refers to a multi-range, only its first area
    '               is considered.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function GetRange( _
        ByVal rg As Range) _
    As Variant
        If rg Is Nothing Then Exit Function
        
        If rg.Rows.Count + rg.Columns.Count = 2 Then ' one cell
            Dim Data As Variant: ReDim Data(1 To 1, 1 To 1): Data(1, 1) = rg.Value
            GetRange = Data
        Else ' multiple cells
            GetRange = rg.Value
        End If
    
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多