简体   繁体   English

VBA按单元格将单元格从一张纸添加到另一张纸

[英]VBA Adding cells from one sheet to another by cell

i wanna create button which add a cell from one sheet to another by time that was added. 我想创建一个按钮,按时添加一个单元格到另一个单元格。 In image i showing example what i wanted to do. 在图像中我展示了我想做的例子。 Adding first cells 添加第一个细胞

So far i got this 到目前为止我得到了这个

Sub CopyBasedonSheet1()
Dim i As Integer
Dim j As Integer
Sheet1LastRow = Worksheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row

i = 1
For j = 1 To Sheet1LastRow
    If Worksheets("Sheet1").Cells(j, 2).Value = "a" Then
        Worksheets("Sheet2").Cells(i, 1).Value = Worksheets("Sheet1").Cells(j, 1).Value
        Worksheets("Sheet2").Cells(i, 2).Value = Worksheets("Sheet1").Cells(j, 2).Value
        i = i + 1
    End If
Next j
End Sub

But when i add next cell and press button again it will overwrite whole cells in sheet 2. And i wanted to do it something like this: adding next cell Can you help me? 但是,当我添加下一个单元格并再次按下按钮时,它将覆盖表单2中的整个单元格。我想这样做: 添加下一个单元格你能帮助我吗?

Your code says put Sheet1!A:A into Sheet2!A:A and Sheet1!B:B into Sheet2!B:B where Sheet1!B:B is "a" but your images say put Sheet1!A:A into Sheet2!A:A where Sheet1!B:B is "a" and disregard Sheet1!B:B. 你的代码说Sheet1!A:A到Sheet2!A:A和Sheet1!B:B到Sheet2!B:B里Sheet1!B:B是“a”但是你的图像说把Sheet1!A:A放到Sheet2中! A:Sheet1!B:B是“a”而忽略Sheet1!B:B。 I'll choose the latter. 我会选择后者。

Option Explicit

Sub CopyBasedonSheet1()
    Dim j As Long, Sheet1LastRow As Long, Sheet2NextRow As Long     

    Sheet1LastRow = Worksheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
    Sheet2NextRow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row + _
          IsEmpty(Worksheets("Sheet2").Range("A1"))

    With Worksheets("Sheet1")
        For j = 1 To Sheet1LastRow
            If .Cells(j, "B").Value = "a" And _
              IsError(Application.Match(.Cells(j, "A").Value, Worksheets("Sheet2").Columns("A"), 0)) Then
                Worksheets("Sheet2").Cells(Sheet2NextRow, "A").Value = .Cells(j, "A").Value
                Sheet2NextRow = Sheet2NextRow + 1
            End If
        Next j
    End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据公用单元格将单元格从一张纸添加到另一张纸 - Adding cells from one sheet to another based on common cell 如何使用 VBA 将一张纸上的 3 个单元格组合成另一张纸上的 1 个单元格? - How to use VBA to combine 3 cells from one sheet into 1 cell in another sheet? VBA 将多个工作簿中的一个单元格复制到另一个工作表中的特定单元格中 - VBA to copy a cell from multiple workbooks into specific cells in the another sheet 根据另一个单元格中的条件将单元格从一个工作表复制到另一个工作表 - Copy cells from one sheet to another based on criteria in another cell 将非空白单元格从一张纸复制到另一张纸的 VBA 代码 - VBA Code to Copy Non Blank Cells From one Sheet to Another 从一个工作表复制单元格并粘贴到另一个工作表中到可变单元格 - Copy cells from one sheet and paste in another to a variable cell Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another? vba循环,以便将特定单元格从一张纸复制到另一张纸 - vba loop in order to copy specific cells from one sheet to another VBA将数据从一张纸复制到另一张纸(到空白单元格) - VBA copy data from one sheet to another (to the blank cells) Excel VBA用于隐藏一个工作表中的单元格(如果它们匹配另一个工作表中的单元格) - Excel VBA for hiding cells in one sheet if they match cells in another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM