基于多个条件将多行提取到有限的模板中

问题描述 投票:0回答:1

我在从“ Datadump”提取到几个连续的“ Template”中遇到问题(因为该模板只能包含10行项目)。 Before

这是我的意图:

  1. 从数据转储(上面的示例)中,可以自动将适当的值提取到模板中,每组Template最多包含10行。每个模板每天都会受到限制,或者最多只能进行10个交易,只要它们来自同一信用来源即可。

  2. 由于数据转储内容广泛,是否有可能在1张纸中包含约10套模板,并且分别打印出来后,它们就会被删除吗?

  3. 最后,为已提取到模板的代码着色

我还附上了之前和之后以供参考:)

After

Voucher Page 1

Voucher Page 2

由于我是VBA的新手,所以我对相应位置和颜色代码的输入没有问题。但是我仍在学习我认为需要此功能的循环功能?

任何帮助将不胜感激!

@编辑:

模板的值是:

1. Credit Source = Source + Source Name
2. Total = All values inside the voucher
3. Account = Item Code
4. Detail = Item Name
5. Unit Code = Unit Code
6. Value = Total Debit

这里是我现在想出的代码(试图分解过程)

Sub learn()
Set wb = ThisWorkbook
'Limiting No. Of Credits
Set dtws = Worksheets("Datadump")
Set wstr = Worksheets("trial")
Dim vcdate
vcdate = wstr.Cells(2, "B").Value
Dim vcsource
vcsource = wstr.Cells(2, "D").Value
Dim NoE As Long
With wstr.Cells(1, 1)
.Value = Application.WorksheetFunction.CountIfs(dtws.Range("A:A"), vcdate, dtws.Range("J:J"), vcsource)
NoE = wstr.Cells(1, 1).Value
If NoE < 11 Then
.Offset(, 2).Value = wstr.Cells(1, 1).Value
Else
NoE = 10
.Offset(, 2).Value = NoE
End If
End With
MsgBox NoE
'End of Limiting No. Of Entries

'Inputting Appropriately
Set tempws = Worksheets("Template")

With tempws
.Cells(4, "J").Value = vcdate
.Cells(6, "C").Value = vcsource


End With

For i = 1 To NoE
If dtws.Cells(i + 1, 1) = vcdate And dtws.Cells(i + 1, 10) = vcsource Then

With tempws
.Cells(i + 9, 1) = dtws.Cells(i + 1, 2)
'detail
.Cells(i + 9, 4) = dtws.Cells(i + 1, 3) & " - " & dtws.Cells(i + 1, 5)
'Unit Code
.Cells(i + 9, 7) = dtws.Cells(i + 1, 6)
'Value
.Cells(i + 9, 9) = dtws.Cells(i + 1, 9)

'Bottom Total Value
.Cells(20, "I").Formula = "=sum(I10:I19)"
.Cells(7, "C").Value = tempws.Cells(20, "I").Value

End With
i = i + 1
End If
Next


'Create the new Voucher Sheet

'Copy Values to Voucher Database
Set tmpWB = Workbooks.Open("C:\Users\User\Desktop\" & "VDB.xlsm")
wb.tempws.Range("C" & Rows.Count).End(xlUp).Row.Copy _
    Workbooks("VDB.xlsm").Sheets("Sheet1").Range("A2")


End Sub

我相信颜色编码不会有问题,但似乎数据提取是主要问题...

任何帮助将不胜感激!

excel database vba extract
1个回答
0
投票

Multiple Criteria Match/Index VBA Across two sheets中有多个条件匹配的示例>

多条件匹配在行中

If ThisWorkbook.Worksheets("Sheet2").Cells(s, 1).Value = ID And ThisWorkbook.Worksheets("Sheet2").Cells(s, 2).Value = Activity Then
                ThisWorkbook.Worksheets("Sheet2").Cells(s, 3).Value = ThisWorkbook.Worksheets("Sheet1").Cells(r, 3).Value
            End If

And连接多个条件,在这种情况下为2个条件。 And是逻辑AND函数,在excel中还可以使用3个其他逻辑运算符OR,XOR和NOT(https://www.ablebits.com/office-addins-blog/2014/12/17/excel-and-or-xor-not-functions/),它们也可以用于多条件匹配。比较匹配的主要结构是If

在代码中使用了两个嵌套

循环,一个循环通过sheet1的第1行和第3行,另一个循环通过sheet2的第1行和第3行,在这些循环的“核心”中两个嵌套循环进行比较,匹配。因此,如果要遍历两行两页的工作表,请使用以下命令:
For r = 2 To ThisWorkbook.Worksheets("Sheet1").UsedRange.Rows.Count

          ... 

        For s = 2 To ThisWorkbook.Worksheets("Sheet2").UsedRange.Rows.Count

           ...

        Next s
    Next r
© www.soinside.com 2019 - 2024. All rights reserved.