简体   繁体   English

VBA Excel基于IF语句自动复制和粘贴特定单元格

[英]VBA Excel Automatically Copy & Paste Specific cells based on IF statement

The code I have placed below is a combination of what works and what i can't get to work. 我在下面放置的代码是有效和无效的组合。

The code that is not commented will copy cells to "sheet2" from "sheet1". 未注释的代码会将单元格从“ sheet1”复制到“ sheet2”。

What I cannot get to work correctly is the code that I have disabled that would replace my Range Method of coping from "sheet1" to "sheet2". 我无法正常工作的是我已禁用的代码,该代码将替代我的范围方法,从“ sheet1”到“ sheet2”。

Also my If Then Code is what will some up what I'm trying to accomplish. 我的If Then Code也是我要完成的工作的一部分。 I'm trying to get the If statement to search all of column A and copy all Cars that are year 1991 to sheet2. 我正在尝试获取If语句来搜索所有A列,并将所有1991年的Cars复制到sheet2。

Mind my poor coding skills I'm Doing my best to show & explain so I can be helped. 记住我的编码技巧很差,我正在尽力显示和解释,以便为您提供帮助。
Here is the Sheets 1 & 2 这是床单1和2

(hxxp://s15.postimg.org/orfw7tlaz/test.jpg) (hxxp://s15.postimg.org/orfw7tlaz/test.jpg)

OLD CODE 旧代码

Sub Macro1()

Set a = Sheets("Sheet1")
Set b = Sheets("Sheet2")
Set c = Sheets("Sheet3")

Dim x
Dim z
Dim lastrow As Long, erow As Long

x = x + 1
z = 2


'lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
'lastrow = b.Cells(Rows.Count, 1).End(xlUp).Row
'For i = 2 To lastrow
lastrow = b.Range("A" & Rows.Count).End(xlUp).Row + x

'If a.Cells(i, 1) = “1991” Then


'a.Cells(i, 1).Copy
'erow = b.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'b.Paste Destination:=b.Range.Cells(erow, 4)
Range("A" & z).Copy Destination:=b.Range("D" & lastrow)

'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range.Cells(erow, 1)
Range("B" & z).Copy Destination:=b.Range("A" & lastrow)


'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range.Cells(erow, 3)
Range("C" & z).Copy Destination:=b.Range("C" & lastrow)

'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range(erow, 2)
Range("D" & z).Copy Destination:=b.Range("B" & lastrow)
'End If

'Next i
Application.CutCopyMode = False
Sheet2.Columns().AutoFit
'b.Range("A1").Select
End Sub

So I added some Lines and Began changing the cell locations to reflect the format I need and now when I run the macro it only copys the very last line from Sheet1 to sheet2. 因此,我添加了一些行,并开始更改单元格位置以反映所需的格式,现在运行宏时,它仅将最后一行从Sheet1复制到sheet2。 I believe it has to do with the order of the way these cells are. 我相信这与这些单元格的顺序有关。

        b.Cells(erow, 1) = a.Cells(i, 1)
        b.Cells(erow, 2) = a.Cells(i, 2)
        b.Cells(erow, 3) = a.Cells(i, 3)
        b.Cells(erow, 4) = a.Cells(i, 4)

Changing these back fixes it so it copys all the cells but its not what I'm trying to do. 改变这些背面可以修复它,因此它可以复制所有单元格,但不是我想要的。 The Code I'm Trying to run is Below 我正在尝试运行的代码如下

NEW CODE Working Thanks to EntryLevel! 新代码工作感谢EntryLevel!

Sub TakeTwo()
    Dim a As Worksheet
    Dim b As Worksheet
    Dim c As Worksheet

    Set a = ThisWorkbook.Sheets("Sheet1")
    Set b = ThisWorkbook.Sheets("Sheet2")
    Set c = ThisWorkbook.Sheets("Sheet3")

    Dim i As Long
    Dim lastrowsheet1 As Long
    Dim erow As Long

    lastrowsheet1 = a.Cells(Rows.Count, 1).End(xlUp).Row
    erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    For i = 2 To lastrowsheet1
        If a.Cells(i, 1).Value = "AEM" Then
            b.Cells(erow, 31) = a.Cells(i, 1)  '<------When I modify these
            b.Cells(erow, 6) = a.Cells(i, 4)   '<------The copied cells
            b.Cells(erow, 28) = a.Cells(i, 5)  '<------Don't show up
            b.Cells(erow, 26) = a.Cells(i, 6)  '<------In Sheet2
            b.Cells(erow, 46) = a.Cells(i, 11) '<------Only the last
            b.Cells(erow, 29) = a.Cells(i, 14) '<------Line found Is copied to sheet2
            erow = erow + 1
        End If
    Next i
    Application.CutCopyMode = False
    b.Columns.AutoFit
    'b.Range("A1").Select
End Sub

Now Using Same Working Code But Different function Not Working 现在使用相同的工作代码不同的功能不起作用

Sub TakeThree()
    Dim a As Worksheet
    Dim b As Worksheet
    Dim c As Worksheet

    Set a = ThisWorkbook.Sheets("Sheet1")
    Set b = ThisWorkbook.Sheets("Sheet2")
    Set c = ThisWorkbook.Sheets("Sheet3")

    Dim i As Long
    Dim lastrowsheet1 As Long
    Dim erow As Long

    lastrowsheet1 = c.Cells(Rows.Count, 1).End(xlUp).Row
    erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    For i = 2 To lastrowsheet1
        If c.Cells(i, 1).Value = b.Cells(erow, 6).Value Then 'If serial number is found from sheet2 column 6 in sheet3 Column 1
            b.Cells(erow, 8) = c.Cells(i, 2) 'Then copy description from sheet3 cell row to Sheet2 cell row
            erow = erow + 1
            End If
    Next i
    Application.CutCopyMode = False
    b.Columns.AutoFit
    c.Columns.AutoFit
    'b.Range("A1").Select
End Sub

So I added another For Loop with Dim r and added another Line erow = erow + r & now the code copys the first 2 rows needed but does not continue iterating down the list. 因此,我添加了另一个带有Dim r的For循环,并添加了另一行erow = erow + r&现在,代码将复制所需的前两行,但不再继续遍历列表。 which is confusing me. 这让我感到困惑。 here is the code below i have added. 这是我添加的以下代码。

Dim r As Long
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long

lastrowsheet1 = c.Cells(Rows.Count, 1).End(xlUp).Row
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
 For r = 1 To erow
  For i = 2 To lastrowsheet1
  If c.Cells(i, 1) = b.Cells(erow, 6) Then
     b.Cells(erow, 8) = c.Cells(i, 2)
     erow = erow + r
  End If
Debug.Print i
  Next i
  Next r

Based on your statement that I'm trying to get the If statement to search all of column A and copy all Cars that are year 1991 to sheet2 , it seems like Autofilter might be an easier solution than looping. 基于您I'm trying to get the If statement to search all of column A and copy all Cars that are year 1991 to sheet2 ,看来Autofilter可能比循环更容易。 You should be able to use something like this: 您应该可以使用以下内容:

Sub TestyTestTest()
    Dim lastrowsheet1 As Long
    Dim lastrowsheet2 As Long

    With ThisWorkbook.Sheets("Sheet1")
        .AutoFilterMode = False
        lastrowsheet1 = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Range(.Cells(1, 1), .Cells(lastrowsheet1, 4)).AutoFilter Field:=1, Criteria1:="1991"
        .Range(.Cells(2, 1), .Cells(lastrowsheet1, 4)).SpecialCells(xlCellTypeVisible).Copy
        .AutoFilterMode = False
    End With

    With ThisWorkbook.Sheets("Sheet2")
        .AutoFilterMode = False
        lastrowsheet2 = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Cells(lastrowsheet2 + 1, 1).PasteSpecial Paste:=xlPasteValues
        .AutoFilterMode = False
    End With
    Application.CutCopyMode = False
End Sub

EDIT: 编辑:

Trying to stick close to your original code, would something like this be more like what you are looking for? 尝试贴近您的原始代码,这样的事情会更像您想要的东西吗?

Sub TakeTwo()
    Dim a As Worksheet
    Dim b As Worksheet
    Dim c As Worksheet

    Set a = ThisWorkbook.Sheets("Sheet1")
    Set b = ThisWorkbook.Sheets("Sheet2")
    Set c = ThisWorkbook.Sheets("Sheet3")

    Dim i As Long
    Dim lastrowsheet1 As Long
    Dim erow As Long

    lastrowsheet1 = a.Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To lastrowsheet1
        If a.Cells(i, 1).Value = 1991 Then
            erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            b.Cells(erow, 1) = a.Cells(i, 1)
            b.Cells(erow, 2) = a.Cells(i, 2)
            b.Cells(erow, 3) = a.Cells(i, 3)
            b.Cells(erow, 4) = a.Cells(i, 4)
        End If
    Next i
    Application.CutCopyMode = False
    b.Columns.AutoFit
End Sub

SECOND EDIT - OP's NEW PROBLEM: 第二次编辑-OP的新问题:

It looks like your data is just pasting over itself because erow is defined as the row after the last row in column 1 that is not empty, but you are not actually putting any data into that column, so erow isn't moving down to the next line. 看来您的数据只是在粘贴自身,因为erow被定义为第1列中最后一行之后的行,该行不为空,但是您实际上没有将任何数据放入该列中,因此erow不会向下移动到下一行。

Basically, change the column number in this line: 基本上,更改此行中的列号:

erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row

The 1 in b.Cells(b.Rows.Count, 1) should be changed to a column number that you paste data into every time. 1b.Cells(b.Rows.Count, 1)应改为你的数据粘贴到每次一列数。 Alternatively, you could use erow as a counter and increment it manually each time through the loop. 另外,您可以将erow用作计数器,并在每次循环时手动对其进行递增。 In that case move the existing line that defines erow up underneath the line that defines lastrowsheet1 and then put erow = erow + 1 inside the loop after all the pasting has taken place but before End if . 在这种情况下移动至现有行定义erow向上限定所述线下方lastrowsheet1然后把erow = erow + 1在循环内的所有粘贴已经发生之后,但在End if If you put it after End If , you'll end up with a bunch of blank lines between your data. 如果将其放在End If ,则会在数据之间出现一堆空白行。

暂无
暂无

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

相关问题 Excel/VBA - 根据日期将数据复制并粘贴到特定行中的工作表中 - Excel/VBA - Copy and paste data into a worksheet in a specific row based on date Excel VBA宏复制单元格并粘贴到另一个 - Excel vba macro to copy cells and paste in another 复制和粘贴具有异常的Excel单元格-VBA - Copy and Paste Excel Cells With Exceptions - VBA 复制粘贴多个单元格Excel VBA - Copy Paste Multiple Cells Excel VBA VBA代码可根据条件复制特定列中的单元格并将其粘贴(值)到另一张工作表中特定列中的单元格 - VBA code to copy cells in specific column and paste (value) it to cells in specific column on another Sheet based on conditions 复制和粘贴多个单元格而不会覆盖现有单元格EXCEL VBA - Copy and paste multiple cells without overwriting existing cells EXCEL VBA VBA复制选定的单元格与数据(Excel)。 将它们粘贴到表中的特定行(Word) - VBA Copy selected cells with data (Excel). Paste them in specific row in a table (Word) Excel VBA:在特定单元格中设置值,然后复制一组单元格并粘贴值,然后迭代该值 - Excel VBA: Set a value in a specific cell, then copy a set of cells and paste values, then iterate the value Excel VBA宏可根据列中的文本将单元格值复制并粘贴到某些单元格 - Excel VBA Macros to copy and paste cell value to certain cells based on text in column 如何通过Excel中的VBA复制/粘贴已过滤/可见单元格中的公式 - How to copy/paste formula in filtered/visible cells via VBA in Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM