将数据复制并粘贴到新工作表中,该工作表将从源和目标进行比较

时间:2014-05-08 11:03:50

标签: excel excel-vba vba

我是宏新手,请在下面的情景问题中帮助我

方案

  1. 比较两个Excel工作表列(源表和目标表)并将reult放在另一个工作表(结果表)中。
  2. 注意 - 从源和目标中选择列是动态的

    1. 输入您要包含在结果表中的Colum名称以及Source&目标列。 (例如,在结果表中也包括B,C,D,F列(来自源表)。
    2. 问题

      下面是我的脚本,问题是将动态选定列从源粘贴到结果表时,它将列中的数据粘贴到源中的输入。 示例 - 将C,G,F从源粘贴到结果表时,将结果列在结果列C,G,F中,而不是粘贴在下一个空列(B,C,D)中

      在下面的脚本"工作表("结果")。单元格(Rownum,ch)= Column_source_value"导致这个问题,请帮助我如何更改单元格参数

      代码

      Dim ws1lastrow As Long, ws2lastrow As Long, ws1lastcolumn As Integer, ws2lastcolumn As Integer
      Dim maxRow As Integer, maxColumn As Integer  
      Dim i As Integer, j As Integer, k As Integer, x As Long, Column_source As String, Column_target As String
      Dim sourceValue As String
      Dim targetvalue As String
      Dim addsourcecolvalue As String, ch As Variant, AddColumn_source_num As String, AddColumn_source_value As String
      Dim addsourceValue() As String
      
      Sub Compare()
      CompareWorksheets Worksheets("Source"), Worksheets("Target")
      End Sub
      
      Sub CompareWorksheets(ws1 As Worksheet, ws2 As Worksheet)
      sourceValue = Sheets("Source").TextBox1.Value
      targetvalue = Sheets("Source").TextBox2.Value
      addsourcecolvalue = Sheets("Source").TextBox3.Value
      
      With ws1.UsedRange
          ws1lastrow = .Rows.Count
          ws1lastcolumn = .Columns.Count
      End With
      
      With ws2.UsedRange
          ws2lastrow = .Rows.Count
          ws2lastcolumn = .Columns.Count
      End With
      
      maxRow = ws1lastrow
      maxColumn = ws1lastcolumn
      
      For i = 1 To ws1lastrow
          For j = 1 To ws2lastrow
              Column_source_num = sourceValue & i
              Column_target_num = targetvalue & j
              Column_source_value = Worksheets("Source").Range(Column_source_num).Value
              Column_target_value = Worksheets("Target").Range(Column_target_num).Value
              If Column_source_value = Column_target_value Then
                  Sheets("Source").Cells.Find(What:=Column_source_value, LookAt:=xlWhole,   MatchCase:=False, SearchFormat:=False).Activate
                  Dim Rownum As String
                  Rownum = ActiveCell.Row
                  Worksheets("Result").Cells(i, 1) = Column_source_value
                  splitsourcecloumn (Rownum)
              End If
          Next j
      Next i
      End Sub
      
      Function splitsourcecloumn(Rownum)
      addsourceValue() = Split(addsourcecolvalue, ",")
      
      For Each ch In addsourceValue()
          Column_source_num = ch & Rownum
          Column_source_value = Worksheets("Source").Range(Column_source_num).Value
          Worksheets("Result").Cells(Rownum, ch) = Column_source_value
      Next ch
      End Function
      

1 个答案:

答案 0 :(得分:0)

尝试使用此功能查找第一个未使用的列。

Public Function GetLastColumn() As Long
If WorksheetFunction.CountA(Cells) > 0 Then
    GetLastColumn = Cells.Find(What:="*", _
    After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End If
End Function

我希望这有帮助!