Excel 将数据从一个 sheet 移动到另一个 sheet 并放置在下一个空行中

Excel moving data from one sheet to another and placing in next empty row

我有来自 table 的值,我正在尝试编写一个 VBA 宏以移动到另一个 sheet 它将被放置在下一个空行中,

就选择数据而言,这是我所拥有的:

'storing the values to be moved
Dim DayID As String
Dim Shift As Integer
Dim Operator As String
Dim Operation As String
Dim PartNum As String
Dim Asset As String

'placing selected cells
Sheets("Raw Data").Select

   Range("A10000").Select
   Selection.End(xlUp).Select
   ActiveCell.Offset(1, 0).select

   ActiveCell.Value = DayID

我走到这一步是想看看我所做的工作是否只是将日期放在广告中,但事实并非如此。我是 VBA 的新手,还不完全了解我在做什么,所以非常感谢您的帮助!

我放置数据的列分别在 A、M、O、Q、N 和 P 列中(如果有帮助的话)

在完全不知道您要填写什么值、sheet 从什么开始以及您希望结果看起来如何的情况下,像下面这样的内容至少应该让您入门。

Sub test()

Dim rData As Worksheet
Dim lRow As Long
Dim arr(5) As Variant

Set rData = Sheets("Raw Data")
arr(0) = "A"
arr(1) = "M"
arr(2) = "O"
arr(3) = "Q"
arr(4) = "N"
arr(5) = "P"

With rData

    For Each element In arr

        lRow = .Cells(.Rows.Count, element).End(xlUp).Row + 1
        .Cells(lRow, element).Value = "Value in Column " & element

    Next element

End With

End Sub

这假定您正在处理包含代码的同一工作簿。如果没有,您可以将 "ThisWorkbook" 更改为 "ActiveWorkbook"。我包含了 With wsTarget,尽管它目前过多,但我相信随着您构建此子例程,它会变得越来越重要。编辑以将前三个变量放入相应的列中。剩下的代码留给你填写:

Sub FirstStep()

'storing the values to be moved
Dim DayID As String
Dim Shift As Integer
Dim Operator As String
Dim Operation As String
Dim PartNum As String
Dim Asset As String

Dim wsTarget As Worksheet

Set wsTarget = ThisWorkbook.Worksheets("Raw Data") 'Would be much better to change the CodeName of the sheet and reference directly.

'placing selected cells
With wsTarget
    .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = DayID
    .Cells(Rows.Count, 13).End(xlUp).Offset(1, 0).Value = Shift
    .Cells(Rows.Count, 15).End(xlUp).Offset(1, 0).Value = Operator
End With

End Sub