[英]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.