如何循环此索引匹配?

时间:2018-07-18 10:45:56

标签: excel vba excel-vba select-case

好,所以我在这里有了这个宏,它用AB和AC列中的索引匹配函数填充单元格。 (这部分很好)

然后,根据另一工作簿中名为AR_Curr的范围内的信息,将AC列中的这些值格式化为正确的货币,方法是将它们与这些货件的PL编号匹配(这部分无效,仅生成其中一个每当我尝试运行它时,都会出现“运行时错误'1004':无法获取WorksheetFunction类的Index属性”或“运行时错误'1004':无法获取WorksheetFunction类的Match属性”。

最后,它会复制现在填充单元格的所有值,并将它们粘贴为值并将它们居中。

有人可以告诉我如何通过搜索索引匹配项来确定相关货币来使这种选择大小写功能动态地工作吗?我的智慧到此为止。

Sub Payment_YN_Call()
'
' Payment_YN_Call Macro
'
Dim answer1 As Integer
Dim answer2 As Integer
Dim answer3 As Integer

answer1 = MsgBox("Did you update the named ranges for INV_Nums on this sheet?", vbYesNo + vbQuestion, "Payment Update")
If answer1 = vbYes Then
    answer2 = MsgBox("Do you have the AR Balance sheet open?", vbYesNo + vbQuestion, "Payment Update")
    If answer2 = vbYes Then
        answer3 = MsgBox("Did you update the AR_PL_Nums, AR_Paid, and AR_Unpaid Ranges on the AR Balance Sheet?", vbYesNo + vbQuestion, "Payment Update")
        If answer3 = vbYes Then

                Dim i As Integer
                Dim Rng1 As Range
                Dim Rng2 As Range
                Dim ARwkb As Excel.Workbook
                Dim ARwks As Excel.Worksheet
                Dim Samwkb As Excel.Workbook
                Dim Samwks As Excel.Worksheet

                Set Samwkb = Excel.Workbooks("Samples - one sheet")
                Set Samwks = Samwkb.Worksheets("samples shipment")
                Set ARwkb = Excel.Workbooks("AR balance.xlsx")
                Set ARwks = ARwkb.Worksheets("Total Trading")

                For i = 6 To Range("INV_Nums").Count + 5

                    If IsEmpty(Range("AB" & i)) Then
                        Range("AB" & i).Select
                            ActiveCell.FormulaR1C1 = _
                                "=IFERROR(IF(INDEX('[AR balance.xlsx]Total trading'!AR_Unpaid,MATCH(RC[-2],'AR balance.xlsx'!AR_Invoice_Nums,0))=0,""PAID"",""UNPAID""),"""")"
                    End If

                If IsEmpty(Range("AC" & i)) Then
                    Range("AC" & i).Select
                        ActiveCell.FormulaR1C1 = _
                            "=IFERROR(IF(RC[-1]=""PAID"",INDEX('AR balance.xlsx'!AR_Paid,MATCH(RC[-3],'AR balance.xlsx'!AR_Invoice_Nums)),""""),"""")"
                End If

                Next i


                Set Rng1 = ARwks.Range("AR_Curr")
                Set Rng2 = ARwks.Range("AR_PL_Nums")
                Dim lastrow As Long, x As Long
                lastrow = Range("INV_Nums").Count + 5
                    For y = 5 To lastrow
                        Select Case Range(Application.WorksheetFunction.Index(Rng1, Application.WorksheetFunction.Match(Range("F" & y), Rng2, 0)))

                            Case "USD"
                                Samwks.Range("AB" & y).NumberFormat = "$#,##0.00_);($#,##0.00)"
                            Case "RMB"
                                Samwks.Range("AB" & y).NumberFormat = "[$¥-zh-CN]#,##0.00;[$¥-zh-CN]-#,##0.00"
                            Case "EUR"
                                Samwks.Range("AB" & y).NumberFormat = "[$€-x-euro2] #,##0.00_);([$€-x-euro2] #,##0.00)"
                            Case "GBP"
                                Samwks.Range("AB" & y).NumberFormat = "[$£-en-GB]#,##0.00;-[$£-en-GB]#,##0.00"
                            Case "HKD"
                                Samwks.Range("AB" & y).NumberFormat = "[$HK$-zh-HK]#,##0.00_);([$HK$-zh-HK]#,##0.00)"
                            Case "JPY"
                                Samwks.Range("AB" & y).NumberFormat = "[$¥-ja-JP]#,##0.00;-[$¥-ja-JP]#,##0.00"
                        End Select
                    Next y



                Columns("AB:AC").Select
                    Selection.Copy
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                        :=False, Transpose:=False

                Columns("AB:AC").Select
                    Application.CutCopyMode = False
                    With Selection
                        .HorizontalAlignment = xlCenter
                        .VerticalAlignment = xlCenter
                        .Orientation = 0
                        .AddIndent = False
                        .IndentLevel = 0
                        .ShrinkToFit = False
                        .ReadingOrder = xlContext
                    End With



Else
End If
End If
End If



End Sub

1 个答案:

答案 0 :(得分:1)

创建一个保存货币和格式的范围,例如

Table of currencies

并将范围命名为“ currencies”。然后将宏的“选择大小写”部分更改为...

For y = 5 To lastrow
    Set thisCurrency = Range(Application.WorksheetFunction.Index(Rng1, Application.WorksheetFunction.Match(Range("F" & y), Rng2, 0)))
    Samwks.Range("AB" & y).NumberFormat = Application.WorksheetFunction.VLookup(thisCurrency, Range("currencies"), 2, False)
Next y

其优点是您可以添加新的货币或更改格式,而无需在宏中进行任何更改。