[英]Copy and paste data from one sheet to another sheet(s) based on cell value
Hey guys I have a question as briefly stated on the title.嘿伙计们,我有一个问题,正如标题中简要说明的那样。 To be specific, I have orders everyday and I need to put them on different worksheets according to the column "delivery provider" and "channel".
具体来说,我每天都有订单,我需要根据“交付提供商”和“渠道”列将它们放在不同的工作表上。 image description here
图片说明在这里
I had created a userform before but I have to divide my data sheet into several sheets according to the del provider and channel, then with the code我之前创建了一个用户表单,但我必须根据 del 提供者和渠道将我的数据表分成几张表,然后使用代码
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
sh1.Range("A2:A" & lr).Copy Destination:=sh2.Range("B2")
to copy and paste all the columns one by one to each sheet.将所有列一一复制并粘贴到每张纸上。
But actually I have many providers(6 or more) which makes it much time consuming.但实际上我有很多提供者(6 个或更多),这使得它非常耗时。
So my concern is how can I copy and paste them into the desired worksheets according to the 2 columns.所以我关心的是如何根据 2 列将它们复制并粘贴到所需的工作表中。
I came across and idea that I can CONST each column in "data" as source column, and CONST each column in "ems_by_sea" and others as target/destination colunm, then do the FOR/NEXT statement.我偶然发现我可以将“数据”中的每一列作为源列,并将“ems_by_sea”中的每一列和其他列作为目标/目标列,然后执行 FOR/NEXT 语句。 But I have no idea how to CONST at different worksheets...
但我不知道如何在不同的工作表中使用 CONST ......
const firstrow = 2
const soucol_provider = sh1.cells("E"), soucol_channel = sh1.cells("F")
I tried to do it by this but VBA responds with error我试图这样做,但 VBA 响应错误
Application.screenupdating = False
For i = firstrow to lr
If Sh1.Cells(i, soucol_provider).Value = "EMS" and Sh1.Cells(i, soucol_channel).Value = "by air" Then
#copy and paste the data in current row to the sheet"ems_by_air' nex i I have no idea if there is much better way to achieve this...would you guys shed some light on this? FIY: It's not just copy and paste the exact data to the sheets since the column order in each sheet is different. #将当前行中的数据复制并粘贴到工作表“ems_by_air” nex 我不知道是否有更好的方法来实现这一点......你们能对此有所了解吗?FIY:这不仅仅是复制和粘贴由于每个工作表中的列顺序不同,因此工作表的确切数据。
As part of your scope, you could select
a case for your destination spreadsheet, eg:作为 scope 的一部分,您可以
select
为您的目标电子表格提供一个案例,例如:
dim destSheet as worksheet
Select case true
case deliveryProvider = "X" and channel="Y"
set destSheet = sheets(1)
case deliveryProvider = "A" and channel="B"
set destSheet = sheets(2)
End select
With destSheet
'use .notation
End with
You can set up conditions as you deem fit, to account for your number of factors/variables.您可以设置您认为合适的条件,以说明您的因素/变量的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.