简体   繁体   English

如果工作表sheet2上的单元格与工作表sheet1上的单元格匹配,则将行从工作表2复制到工作表1并循环到下一行

[英]If cell on sheet2 row1 matches cell on sheet1 then copy row from sheet 2 to sheet 1 and loop for next row

Everyone I am new to code and VBA Excell. 每个人我都是代码和VBA Excell的新手。 I have a Sub that works, I'm just not sure if it's the right way to do it or if there is a more efficient way as it takes a while to complete when run. 我有一个Sub可以工作,我只是不确定这是正确的方法,还是不确定是否有更有效的方法,因为运行时需要一段时间才能完成。 I was just wondering if someone can have a look and maybe give me some pointers. 我只是想知道是否有人可以看看并且可能给我一些指示。

I will put my code below I hope I'm doing this right. 我将代码放在下面,希望我做对了。

Thanks Carly 谢谢卡莉

Sub DataPopulate()
    Dim wb As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rng1 As Range
    Dim num As Range
    Set wb = ActiveWorkbook
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    Set rng1 = Range("F2")
    Set num = ws1.Range("F2:F4")

'When you click the Click this to populate data MSRP Pricing button you will get the yes no message box.

    If MsgBox("Click yes to continue" & vbCrLf & "Excel may say not responding!!!" _
        & vbCrLf & "It just may take a few moments", vbYesNo + vbQuestion) = vbYes Then
        'If the yes button is pushed in the message box.
        ws1.Activate
        Range("e18") = ("MSRP List")
        'MSRP List text is copied to cell e18.
        Range("h2:h16").Value = Range("g2:g16").Value
        'The product group list is copied from colum g to h.

        ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add2 Key:= _
            Range("f2:f16"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
            'The numbers in f2~f16 is sorted in assending order along with the product group name.
        End With

        Dim Lastrow As Integer
            Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
            ws1.Activate
            Range("A23:L" & Lastrow).ClearContents ' Select
            'Selection.ClearContents
            'Count from A23 to column L and the last row with data, then select that and delete.
            Range("A22") = ("Group")
            Range("b22") = ("Description")
            Range("c22") = ("Code")
            Range("d22") = ("Barcode")
            Range("e22") = ("List Number")
            'Copy the data list headings

            a = ws2.Cells(Rows.Count, 1).End(xlUp).Row
            'Count rows of CSV data on sheet2 and set veriable for "a" this is the number of times to run the loop below.
            'MsgBox (a) '<testing count number
        For i = 2 To a
        Dim d As Range
            If ws1.Range("f2").Value = ("1") And ws2.Cells(i, 1).Value = ws1.Range("g2") Then
            'Checking if order of product group f2 = 1
            'and if there is a match in sheet2 column A row 1 with G2 in product group list
                    b = ws1.Cells(Rows.Count, 1).End(xlUp).Row
                    ws2.Rows(i).Copy
                    ws1.Cells(b + 1, 1).PasteSpecial Paste:=xlPasteValues
                    'Then copy that row to sheet1 in the next empty row
                End If
                'Loop will do the next rows till "a" times loops are done
            Next

        'This is the same for below until all product groups are done
        For i = 2 To a
            If ws1.Range("f3") = 2 And ws2.Cells(i, 1).Value = ws1.Range("g3") Then
                    b = ws1.Cells(Rows.Count, 1).End(xlUp).Row
                    ws2.Rows(i).Copy
                    ws1.Cells(b + 1, 1).PasteSpecial Paste:=xlPasteValues
                End If
            Next

        For i = 2 To a
            If ws1.Range("f4") = 3 And ws2.Cells(i, 1).Value = ws1.Range("g4") Then
                    b = ws1.Cells(Rows.Count, 1).End(xlUp).Row
                    ws2.Rows(i).Copy
                    ws1.Cells(b + 1, 1).PasteSpecial Paste:=xlPasteValues
                End If
            Next

        Dim rng As Range
        Set rng = Range("F2:f1000")
        'Loop backwards through the rows
        'in the range that you want to evaluate.
        For i = rng.Rows.Count To 1 Step -1

            'If cell i in the range contains an "0", delete the entire row.
            If rng.Cells(i).Value = "0" Then rng.Cells(i).EntireRow.Delete
            'Deleting rows with at 0
        Next

        Application.CutCopyMode = False
        'ThisWorkbook.ws1.calls(1, 22).Select
            ws1.Activate
        Range("A24:E24").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -4.99893185216834E-02
            .PatternTintAndShade = 0
        End With
        Range("A23:E24").Select
        Selection.Copy
        Application.CutCopyMode = False
        Selection.Copy
        Range("A25:E1000").Select
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        Range("A21").Select
        'Adding grey scale to the rows to make is eazier to read.
        'Else


    End If
End Sub

So a basic principal of programming is that your functions/subroutines should only have one job. 因此,编程的基本原则是您的函数/子例程应该只有一项工作。 The first step I would take to improve your code would be breaking your code up into more subroutines using this principal. 我要改进代码的第一步是使用此主体将代码分解为更多的子例程。 I won't go too in depth on the advantage of this because there's already loads of resources explaining why to do things this way. 我不会在此方面做太多深入的介绍,因为已经有大量的资源说明了为什么要这样做。 This thread has some good explanations, as well as draw backs to breaking your code up too much this way. 这个线程有一些很好的解释,也有缺点,可以使您过多地破坏代码。

What I always do is start with a subroutine called Main() with a job that is simply to call the other functions in the program and pass variables between them as necessary. 我一直做的工作是从一个名为Main()的子例程开始,该子例程的工作是简单地调用程序中的其他函数,并在必要时在它们之间传递变量。 Make sure all your functions/subroutines have names that describe their purpose and then you will know exactly what your program is doing at each step of the process simply by looking at Main. 确保所有函数/子例程都具有描述其用途的名称,然后您只需查看Main,就可以确切知道程序在过程的每个步骤中正在做什么。

暂无
暂无

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

相关问题 如何将Sheet1第1行中的数据复制到Sheet2 Row2并循环遍历整个工作表 - How to copy data from Sheet1 Row 1 into Sheet2 Row2 and loop through entire sheet 在Sheet1中找到一个单元格,然后将其所在的整个行复制到Sheet2中的第一个空行 - Find A Cell In Sheet1 Then Copy The Entire Row That It Is In To The First Empty Row in Sheet2 从Sheet1复制行,并在Sheet2的底部插入 - Copy row from Sheet1 and insert at bottom of Sheet2 将数据从“工作表1”归档到“工作表2”的下一个空白行 - Archive data from “sheet1” to next blank row of “sheet2” 如果单元格的颜色为绿色,则从 Sheet1 复制一行并将其粘贴到 Sheet 2 - Copy a row from Sheet1 and paste it into Sheet 2 if color of a cell is green 将行值从 sheet1 复制到 sheet2 列 - Copy row value from sheet1 to sheet2 column 将 sheet2 第一行的最后一列与 sheet1 的 F2 单元格进行比较,如果匹配,则显示 msgbox,否则将 F2 范围粘贴到 sheet2 - Comparing last column of first row in sheet2 with F2 cell of sheet1 if it matches then show msgbox or else copy F2 range paste to sheet2 在我从 sheet1 复制到 sheet2 之前,在 A 行 sheet2 中定义列标题 - Define column headlines in row A sheet2, before I copy from sheet1 to sheet2 将Sheet1第2行中的黄色单元格依次复制到Sheet2 - Copy Yellow Cells in Row 2 of Sheet1 sequentially to Sheet2 在Sheet1中找到与Sheet2中的行数相同的行,并将文本添加到单元格值 - Find the row in Sheet1 with the same number of row in Sheet2 and add text to cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM