简体   繁体   English

根据第一张工作表中列的非空单元格,将特定的行范围复制到另一张工作表的特定范围

[英]Copy specific row range to another sheet's specific range based on non empty cells of a column in first sheet

Hi this is my first post so forgive me for any inconvenience , also I am noob with VBA code. 嗨,这是我的第一篇文章,对于给您带来的任何不便,请原谅,而且我对VBA代码也不满意。 I have sheet "alvin" from which I want to take certain row range if there is a value in column Q and paste it to another sheet called "order template" . 我有工作表“ alvin”,如果列Q中有一个值,我想从其中获取某个行范围,然后将其粘贴到另一个名为“订单模板”的工作表中。 I tried the following code but instead of transferring the right rows it transfers rows based on Q cells value . 我尝试了以下代码,但不是传输正确的行,而是根据Q单元格值传输行。 Eg if cell Q5 has 10 the code transfers the 10th row instead of 5th... 例如,如果单元格Q5具有10,则代码将传输第10行而不是第5行...

Sub test_TRANSFER_to_Order_template()

Application.ScreenUpdating = False

Dim ws1 As Worksheet
Set ws1 = Worksheets("ALVIN")
Dim ws2 As Worksheet
Set ws2 = Worksheets("order template")
Dim q As Range
Dim LRow As Long
LRow = ws2.Range("b" & Rows.Count).End(xlUp).Row + 0
Dim m As Long

For Each q In Range("q4", Range("q1500").End(xlUp))
    If Not IsEmpty(q) Then
        LRow = LRow + 1
        ws2.Range("b" & LRow).Value = ws1.Range("l" & q).Value
        ws2.Range("c" & LRow).Value = ws1.Range("m" & q).Value
        ws2.Range("d" & LRow).Value = ws1.Range("n" & q).Value   'part number
        ws2.Range("e" & LRow).Value = ws1.Range("q" & q).Value
        ws2.Range("f" & LRow).Value = ws1.Range("r" & q).Value

        Application.ScreenUpdating = True
    End If
Next

End Sub

As q is defined as a Range , and you are trying to get the row number, you need to modify it to q.Row . 由于q定义为Range ,并且您试图获取行号,因此需要将其修改为q.Row

For instance, 例如,

ws2.Range("b" & LRow).Value = ws1.Range("l" & q).Value

Should be: 应该:

ws2.Range("b" & LRow).Value = ws1.Range("l" & q.Row).Value

and so on for the rest of them... 其余的等等...


Edit : to make sure you are checking the right values, it's better if you fully qualify your range with the sheet Worksheets("ALVIN") . 编辑 :以确保您检查的值正确,最好是使用Worksheets("ALVIN")完全限定范围。

Modified Loop 修改循环

With ws1 ' <-- qualify the range with "ALVIN" worksheet
    For Each q In .Range("Q4", .Range("Q1500").End(xlUp))
        If Not IsEmpty(q) Then
            LRow = LRow + 1
            ws2.Range("b" & LRow).Value = .Range("l" & q.Row).Value
            ws2.Range("c" & LRow).Value = .Range("m" & q.Row).Value
            ws2.Range("d" & LRow).Value = .Range("n" & q.Row).Value   'part number
            ws2.Range("e" & LRow).Value = .Range("q" & q.Row).Value
            ws2.Range("f" & LRow).Value = .Range("r" & q.Row).Value

            Application.ScreenUpdating = True
        End If
    Next q
End With

暂无
暂无

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

相关问题 如何从一个工作表中复制特定的列并粘贴到第一行中不同范围的另一工作表中? - How to copy specific columns from one sheet and paste in another sheet in a different range at the first row? 搜索范围内的值,然后将值复制到另一张纸的第一个空白行中 - Search for a value in a range, then copy values in first empty row of another sheet 将工作表1中某些行中的特定单元格复制(取决于列中的非零值)到工作表2的最后一个空行 - Copy specific cells from sertain rows from Sheet 1 depending on non-zero value in column to the last empty row of Sheet 2 如何在一张纸到另一张纸中的特定单元格的范围内复制单元格数据? - How do you copy cell data in a range from one sheet to specific cells in another sheet? 将范围从一张纸复制到另一张纸的第一行 - Copy a range from one sheet to the first empty row from another sheet 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 2007复制范围值并粘贴到另一个范围(同一张纸)的第一个空行 - Excel 2007 Copy range values and paste to another range (Same Sheet) first empty row 从一张纸上复制一定范围的单元格,然后插入下一个空行 - Copy a range of cells from one sheet and insert into next empty row 将特定的单元格复制到另一个工作表 - Copy specific cells to another sheet 将一个工作表中一列中的单元格区域复制到另一工作表中一行中的指定单元格中 - Copying a range of cells in a column in one sheet to specified cells in a row in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM