表2共有15列数据,需要D,H,K列数据,即Cust PN,Quantity,New Due Date,Quantity,需要使用VBA将数据按日期数量即每个Cust PN在每个New Due Date的Quantity整理到P,Q,R列
时间: 2024-06-08 15:11:12 浏览: 10
以下是一个简单的VBA代码,可以实现您所需的功能:
```vb
Sub rearrangeData()
'定义变量
Dim lastRow As Long
Dim custPN As String
Dim newDueDate As Date
Dim quantity As Integer
Dim i As Long
'设置初始行号
lastRow = Range("A" & Rows.Count).End(xlUp).Row + 1
'循环遍历数据
For i = 2 To lastRow - 1
'读取数据
custPN = Cells(i, 4).Value
newDueDate = Cells(i, 8).Value
quantity = Cells(i, 11).Value
'将数据写入新的位置
Cells(i, 16).Value = custPN
Cells(i, 17).Value = newDueDate
Cells(i, 18).Value = quantity
Next i
'按照Cust PN和New Due Date对数据进行排序
Range("P1:R" & lastRow).Sort Key1:=Range("P1"), Order1:=xlAscending, _
Key2:=Range("Q1"), Order2:=xlAscending, Header:=xlYes
End Sub
```
请将此代码复制到VBA编辑器中,然后在表2上运行它。它会将Cust PN、New Due Date和Quantity数据复制到P、Q、R列,并按Cust PN和New Due Date排序。请注意,此代码假定表格的第一行是标题行,因此不会处理它。如果您的情况不同,请相应地调整代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)