[英]VBA Copy and Paste to new sheet based on validating date
I am trying to modify the VBA @Glitch_Doctor worked with me on. 我正在尝试修改与我合作的VBA @Glitch_Doctor。 The "Description" range has changed on the New PO tab and needs to summarize in text form on the PO tab.
“新建采购订单”选项卡上的“描述”范围已更改,需要在采购订单标签上以文本形式进行汇总。 I have all working currently it copies text to the appropriate column and row but does not summarize what is in the range C21:C44.
我目前正在所有工作,它将文本复制到适当的列和行,但未总结C21:C44范围内的内容。 Appreciate anyone's help getting the new data to summarize based on category and date, which it is not currently doing.
感谢任何人的帮助,使他们可以根据类别和日期汇总新数据,而目前还没有这样做。
This is the new items added to the code: 这是添加到代码中的新项目:
Dim Dsc As Variant
Dsc = Sheets("New PO").Range("C21:C44")
For Each cell In Description
'To get the row number then total the required information
If cell.Text = Count Then
Row = cell.Row
Dsc = Dsc + Sheets("NEW PO").Range("C21:C44" & Row).Text
End If
Next cell
This is the full VBA: 这是完整的VBA:
Sub Copy_Data()
Dim Count, Qty As Long
Dim CatRng, MonthRng, SDate, CxlDate, PoNumb, Vendor, Description As Range
Dim Total As Currency
Dim StrTarget As String
Dim Dsc As Variant
Dim Row, PORow, Col As Integer
With Sheets("NEW PO").Range("I21:I44").Copy
End With
With Sheets("NEW PO").Range("G21:G44")
.PasteSpecial xlPasteValues, , False, False
End With
Range("A1").Select
Application.CutCopyMode = False
Set CatRng = Sheets("NEW PO").Range("G21:G44")
Set MonthRng = Sheets("POs").Range("M122:X122")
StrTarget = Sheets("New PO").Range("W12")
Set SDate = Sheets("New PO").Range("U12")
Set CxlDate = Sheets("New PO").Range("U13")
Set PoNumb = Sheets("New PO").Range("N10")
Set Vendor = Sheets("New PO").Range("D14")
Set Description = Sheets("New PO").Range("C21:C44")
Dsc = Sheets("New PO").Range("C21:C44")
Count = 0
For Count = 0 To 99
Total = 0
Qty = 0
'So that the values reset each time the cat changes
For Each cell In CatRng
'To get the row number then total the required information
If cell.Value = Count Then
Row = cell.Row
Qty = Qty + Sheets("NEW PO").Range("T" & Row).Value
Total = Total + Sheets("NEW PO").Range("AA" & Row).Value
'I guessed ext cost only as it has been totaled at the bottom,
'this is easily changed though
End If
Next cell
For Each cell In Description
'To get the row number then total the required information
If cell.Text = Count Then
Row = cell.Row
Dsc = Dsc + Sheets("NEW PO").Range("C21:C44" & Row).Text
End If
Next cell
'Now put the totals into a PO only if there is a quantity of items
If Qty > 0 Then
PORow = Sheets("POs").Range("L1048576").End(xlUp).Row + 1
'I'll let you sort the PO number and other fields out but the main 3 are done below
With Sheets("POs")
.Range("I" & PORow).Value = Qty
.Range("L" & PORow).Value = Count
.Range("C" & PORow).Value = SDate
.Range("D" & PORow).Value = CxlDate
.Range("B" & PORow).Value = PoNumb
.Range("F" & PORow).Value = Vendor
.Range("H" & PORow).Value = Dsc
'My understanding here is that the target month in U12 is in the same format as
'the anticipated Receipt month, I hope this is what you were looking for
For Each cell In MonthRng
If cell.Value = StrTarget Then
Col = cell.Column
.Cells(PORow, Col).Value = Total
'Used .cells here as both column and row are now integers
'(only way i can ever get it to work)
End If
Next cell
End With
End If
Next Count
End Sub
Link to the working file: https://www.dropbox.com/s/l2ikw6cr0rqzde8/Inventory%20Plan%20Sample.xlsm?dl=0 链接到工作文件: https : //www.dropbox.com/s/l2ikw6cr0rqzde8/Inventory%20Plan%20Sample.xlsm?dl=0
Screen Capture with New PO tab, PO Tab, PO tab after macro runs Screen Capture of Tabs 宏运行后具有“新建PO”选项卡,“ PO”选项卡,“ PO”选项卡的屏幕捕获
If you are looking to count unique values in C21:C44 per your earlier comment then the code examples here ( Count unique values in Excel ) should work for you. 如果您希望根据先前的注释计算C21:C44中的唯一值,则此处的代码示例( Excel中的唯一值 )应该适合您。
I tested this answer ( https://stackoverflow.com/a/36083024/7612553 ) and it works. 我测试了这个答案( https://stackoverflow.com/a/36083024/7612553 ),它可以工作。 I added
And cell.Value <> ""
so it would not count blank cells passed to the function. 我添加了
And cell.Value <> ""
因此它不计入传递给该函数的空白单元格。
Public Function CountUnique(rng As Range) As Long
Dim dict As Scripting.Dictionary
Dim cell As Range
Set dict = New Scripting.Dictionary
For Each cell In rng.Cells
If Not dict.Exists(cell.Value) And cell.Value <> "" Then
dict.Add cell.Value, 0
End If
Next
CountUnique = dict.Count
End Function
Then you could replace the For Each cell In Description
loop with a call to CountUnique(Description)
然后,您可以使用对
CountUnique(Description)
的调用来替换“ For Each cell In Description
For the scripting dictionary to work, you need to add a reference to Microsoft Scripting Runtime: Tools > References... > check "Microsoft Scripting Runtime" 为了使脚本字典正常工作,您需要添加对Microsoft脚本运行时的引用:工具>引用...>选中“ Microsoft脚本运行时”
I believe this solved the question. 我相信这解决了这个问题。 Converted Dsc to a string and incorporated it into the Catrng array.
将Dsc转换为字符串并将其合并到Catrng数组中。 The missing link was
Dsc=""
to reset the value each time the array returned 缺少的链接为
Dsc=""
以在每次返回数组时重置该值
Sub Copy_Data()
Dim Count As Long
Dim Qty As Long
Dim CatRng As Range
Dim MonthRng As Range
Dim SDate As Range
Dim CxlDate As Range
Dim PoNumb As Range
Dim Vendor As Range
Dim Description As Range
Dim Total As Currency
Dim StrTarget As String
Dim Dsc As String
Dim Row As Integer
Dim PORow As Integer
Dim Col As Integer
With Sheets("NEW PO").Range("I21:I44").Copy
End With
With Sheets("NEW PO").Range("G21:G44")
.PasteSpecial xlPasteValues, , False, False
End With
Range("A1").Select
Application.CutCopyMode = False
Set CatRng = Sheets("NEW PO").Range("G21:G44")
Set MonthRng = Sheets("POs").Range("M122:X122")
StrTarget = Sheets("New PO").Range("W12")
Set SDate = Sheets("New PO").Range("U12")
Set CxlDate = Sheets("New PO").Range("U13")
Set PoNumb = Sheets("New PO").Range("N10")
Set Vendor = Sheets("New PO").Range("D14")
Set Description = Sheets("New PO").Range("C21:C44")
Count = 0
For Count = 0 To 99
Total = 0
Qty = 0
Dsc = ""
'So that the values reset each time the cat changes
For Each cell In CatRng
'To get the row number then total the required information
If cell.Value = Count Then
Row = cell.Row
Qty = Qty + Sheets("NEW PO").Range("T" & Row).Value
Total = Total + Sheets("NEW PO").Range("AA" & Row).Value
Dsc = Sheets("NEW PO").Range("C" & Row).Value
'I guessed ext cost only as it has been totaled at the bottom,
'this is easily changed though
End If
Next cell
'Now put the totals into a PO only if there is a quantity of items
If Qty > 0 Then
PORow = Sheets("POs").Range("L1048576").End(xlUp).Row + 1
'I'll let you sort the PO number and other fields out but the main 3 are done below
With Sheets("POs")
.Range("I" & PORow).Value = Qty
.Range("L" & PORow).Value = Count
.Range("C" & PORow).Value = SDate
.Range("D" & PORow).Value = CxlDate
.Range("B" & PORow).Value = PoNumb
.Range("F" & PORow).Value = Vendor
.Range("H" & PORow).Value = Dsc
'My understanding here is that the target month in U12 is in the same format as
'the anticipated Receipt month, I hope this is what you were looking for
For Each cell In MonthRng
If cell.Value = StrTarget Then
Col = cell.Column
.Cells(PORow, Col).Value = Total
'Used .cells here as both column and row are now integers
'(only way i can ever get it to work)
End If
Next cell
End With
End If
Next Count
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.