重复一个过程

时间:2015-07-09 09:50:12

标签: vba loops repeat

我写了一段代码,在发票上生成发票对帐单'基于所选客户ID的标签,其中的数据来自“数据”。标签。

我希望它为每个不同的ClientID生成发票声明。因此,我的数据选项卡中的数据将被拆分(按客户端ID)并放入语句格式,一个在发票选项卡下面。所以我的代码中添加了For Each语句。这会循环遍历所有数据并正确分组客户端ID。但是,如果客户端ID具有多个记录,则会再次为该ClientID重新生成与记录一样多的发票。即如果客户有3条记录,我将得到3张相同的发票。我只想要一个。

下面是我的代码(我已经删除了很多格式化等等,试着让它看起来更短一些)来制作一个单独的声明,抱歉,如果它效率低下,我上周一才开始vba,我必须快速学习!

Sub CreateStatements()

Dim Selection As String
Dim LastRow As Long
Dim LastRow2 As Long
Dim Client1 As String
Dim ClientRef As Variant
Dim Client As variant
Dim ClientAddr1 As variant
Dim ClientAddr2 As variant
Dim ClientAddr3 As variant
Dim ClientAddr4 As variant
Dim ClientPCode As variant
Dim ClientPhone As variant
Dim ClientFax As variant
Dim Broker As variant
Dim BrokerName As String
Dim BrokerAddr1 As String
Dim BrokerAddr2 As String
Dim BrokerAddr3 As String
Dim BrokerAddr4 As String
Dim BrokerPCode As String
Dim BrokerPhone As String
Dim BrokerFax As String
Dim OurRef As String
Dim StatementDate As Date

Worksheets("Invoice").Activate

Selection = Worksheets("Selections").Range("b6")
Client1 = Worksheets("Selections").Range("c10")

If Selection = "By Client" Then

Dim LastBranch As Long
Worksheets("Branch Invoice").Activate
LastBranch = Cells(ThisWorkbook.Worksheets("Branch Invoice").Rows.Count, "B").End(xlUp).Row

For Each ClientRef In Worksheets("Branch Invoice").Range("B5:B" & LastBranch)

If ClientRef.Value <> "" Then

cl = ClientRef.Value

Worksheets("Invoice").Activate
Client = Application.VLookup(cl, Sheets("Data").Range("I:AC"), 2, False)
ClientAddr1 = Application.VLookup(cl, Sheets("Data").Range("I:AC"), 15, False)
ClientAddr2 = Application.VLookup(cl, Sheets("Data").Range("I:AC"), 16, False)
ClientAddr3 = Application.VLookup(cl, Sheets("Data").Range("I:AC"), 17, False)
ClientAddr4 = Application.VLookup(cl, Sheets("Data").Range("I:AC"), 18, False)
ClientPCode = Application.VLookup(cl, Sheets("Data").Range("I:AC"), 19, False)
ClientPhone = Application.VLookup(cl, Sheets("Data").Range("I:AC"), 20, False)
ClientFax = Application.VLookup(cl, Sheets("Data").Range("I:AC"), 21, False)

Broker = (Application.Index(Sheets("Data").Range("C:C"), Application.Match(cl, Sheets("Data").Range("I:I"), 0))) _
& (Application.Index(Sheets("Data").Range("D:D"), Application.Match(cl, Sheets("Data").Range("I:I"), 0)))

BrokerName = Application.WorksheetFunction.VLookup(Broker, Sheets("MasterList").Range("E:o"), 4, False)
BrokerAddr1 = Application.WorksheetFunction.VLookup(Broker, Sheets("MasterList").Range("E:o"), 5, False)
BrokerAddr2 = Application.WorksheetFunction.VLookup(Broker, Sheets("MasterList").Range("E:o"), 6, False)
BrokerAddr3 = Application.WorksheetFunction.VLookup(Broker, Sheets("MasterList").Range("E:o"), 7, False)
BrokerAddr4 = Application.WorksheetFunction.VLookup(Broker, Sheets("MasterList").Range("E:o"), 8, False)
BrokerPCode = Application.WorksheetFunction.VLookup(Broker, Sheets("MasterList").Range("E:o"), 9, False)
BrokerPhone = Application.WorksheetFunction.VLookup(Broker, Sheets("MasterList").Range("E:o"), 10, False)
BrokerFax = Application.WorksheetFunction.VLookup(Broker, Sheets("MasterList").Range("E:o"), 11, False)


LastRow = Cells(ThisWorkbook.Worksheets("Invoice").Rows.Count, "A").End(xlUp).Row + 1

Range("a" & LastRow) = Client
Range("a" & LastRow + 1) = ClientAddr1
Range("a" & LastRow + 2) = ClientAddr2
Range("a" & LastRow + 3) = ClientAddr3
Range("a" & LastRow + 4) = ClientAddr4
Range("a" & LastRow + 5) = ClientPCode
Range("a" & LastRow + 6) = "Phone: " & ClientPhone
Range("a" & LastRow + 7) = "Fax: " & ClientFax
Range("F" & LastRow) = BrokerName
Range("F" & LastRow + 1) = BrokerAddr1
Range("F" & LastRow + 2) = BrokerAddr2
Range("F" & LastRow + 3) = BrokerAddr3
Range("F" & LastRow + 4) = BrokerAddr4
Range("F" & LastRow + 5) = BrokerPCode
Range("F" & LastRow + 6) = "Phone: " & BrokerPhone
Range("F" & LastRow + 7) = "Fax: " & BrokerFax

LastRow = Cells(ThisWorkbook.Worksheets("Invoice").Rows.Count, "A").End(xlUp).Row + 5

Range("A" & LastRow) = "STATEMENT OF ACCOUNT"
Range("A" & LastRow & ":F" & LastRow).Merge
Range("A" & LastRow).HorizontalAlignment = xlCenter
Range("A" & LastRow + 1) = "Invoice" & vbCrLf & "Date"
Range("B" & LastRow + 1) = "Effective" & vbCrLf & "Date"
Range("C" & LastRow + 1) = "Risk" & vbCrLf & "Reference"
Range("D" & LastRow + 1) = "Transaction" & vbCrLf & "Type"
Range("E" & LastRow + 1) = "Policy" & vbCrLf & "Type"
Range("F" & LastRow + 1) = "Balance Due" & vbCrLf & "£"

'Find All Occurrences

Dim CellPos As Long
Dim rng As Range
Dim v As Variant

CellPos = LastRow + 2
Dim w1 As Worksheet
Dim w2 As Worksheet
Set w1 = Sheets("Branch Invoice")
Set w2 = Sheets("Invoice")
    w1.Activate
For Each rng In Intersect(Range("B:B"), ActiveSheet.UsedRange)
v = rng.Value

If InStr(v, Clientref) > 0 Then
rng.Offset(0, 1).Copy w2.Cells(CellPos, 1)
rng.Offset(0, 2).Copy w2.Cells(CellPos, 2)
rng.Offset(0, 3).Copy w2.Cells(CellPos, 3)
rng.Offset(0, 4).Copy w2.Cells(CellPos, 4)
rng.Offset(0, 5).Copy w2.Cells(CellPos, 5)
rng.Offset(0, 6).Copy w2.Cells(CellPos, 6)
CellPos = CellPos + 1
End If
Next rng

Worksheets("Invoice").Activate

LastRow2 = Cells(ThisWorkbook.Worksheets("Invoice").Rows.Count, "F").End(xlUp).Row

Range("F" & LastRow2 + 1) = WorksheetFunction.Sum(Range("F" & LastRow & ":F" & LastRow2))
Range("F" & LastRow2 + 1).NumberFormat = "£#,##0.00"

Range("E" & LastRow2 + 1) = "TOTAL DUE"
End if
Next ClientRef
End If
End Sub

谢谢!

0 个答案:

没有答案