VBA - 复制到其他工作表

VBA - copying to other sheets

我有这个代码,昨天帮助我定义我的需求的响应者 - 但有些东西我想改变,但我的 vba 技能很低,不知道如何以及在哪里修改代码。我想让它做两件事。

  1. 正确知道它传输数据,我希望它复制它,以及在单元格中计算的值。我有一些单元格,其中有一些公式,它随之而来。我只想要计算出的值。我不知道我是否可以在某个地方使用 xlPasteValues 来获得我想要的东西?

    1. 第二个我想要的是,复制过来的时候,我想在最上面,之前的副本向下移动,所以最新的副本总是在最上面。

先谢谢你了:)

Option Explicit

Sub Copypastemeddata()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim sourceCell As Range
    Dim targetSheet As Worksheet

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Opgørsel")

    Set sourceCell = ws.Range("D3")  'Cell with sheet names for copying to

    With ws

          Set targetSheet = wb.Worksheets(sourceCell.Text)

          Dim nextRow As Long
          nextRow = GetLastRow(targetSheet, 1)
          nextRow = IIf(nextRow = 1, 1, nextRow + 1)

         .Range("A1").CurrentRegion.Copy targetSheet.Range("A" & nextRow)
         targetSheet.Columns.AutoFit

    End With

End Sub


Public Function GetLastRow(ByVal ws As Worksheet, Optional ByVal columnNumber As Long = 1) As Long

    With ws

      GetLastRow = .Cells(.Rows.Count, columnNumber).End(xlUp).Row

    End With

End Function

请试一试...

StartRow 变量定义了 targetSheet 上的目标行,您可以根据需要更改它。

Sub Copypastemeddata()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim sourceCell As Range
    Dim targetSheet As Worksheet
    Dim StartRow As Integer

    Application.ScreenUpdating = False

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Opgørsel")

    Set sourceCell = ws.Range("D3")  'Cell with sheet names for copying to
    StartRow = 1    'Destination row on targetSheet
    With ws
          Set targetSheet = wb.Worksheets(sourceCell.Text)
         .Range("A1").CurrentRegion.Copy
         targetSheet.Range("A" & StartRow).Insert shift:=xlDown
         targetSheet.Range("A" & StartRow).PasteSpecial xlPasteValues
         targetSheet.Columns.AutoFit
    End With
    Application.CutCopyMode = 0
    Application.ScreenUpdating = True
End Sub

替代

      Dim nextRow As Long
      nextRow = GetLastRow(targetSheet, 1)
      nextRow = IIf(nextRow = 1, 1, nextRow + 1)

     .Range("A1").CurrentRegion.Copy targetSheet.Range("A" & nextRow)

    With .Range("A1").CurrentRegion
        targetSheet.Rows(1).Resize(.Rows.Count).Insert shift:=xlUp
        targetSheet.Range("A1").Resize(.Rows.Count, .Columns.Count).Value = .Value
    End With