简体   繁体   English

如何循环此索引匹配?

[英]How can I loop this index match?

Ok, so I have this macro here that is filling cells with an index match function in columns AB and AC. 好的,所以我在这里有了这个宏,它在AB和AC列中用索引匹配功能填充单元格。 (this part is fine) (这部分很好)

Then it is meant to format those values in column AC into their correct currencies based on the information in a range called AR_Curr in another workbook by matching them to the PL numbers for those shipments (this part is not working and just generates either "Run-time error '1004': Unable to get the Index property of the WorksheetFunction class" or "Run-time error '1004': Unable to get the Match property of the WorksheetFunction class" whenever I try to run it. 然后,根据另一工作簿中AR_Curr范围内的信息,将AC列中的这些值格式化为正确的货币,方法是将它们与这些货件的PL编号匹配(这部分无效,仅生成“ Run-时间错误'1004':无法获取WorksheetFunction类的Index属性”或“运行时错误'1004':无法获取WorksheetFunction类的Match属性”。

Finally it copys all the values now populating the cells and pastes them in as values and centers them. 最后,它将复制现在填充单元格的所有值,并将它们粘贴为值并将它们居中。

Could someone please tell me how I can make this select case function work dynamically by searching for an index match to determine the associated currency? 有人可以告诉我如何通过搜索索引匹配项来确定关联货币来使这种选择大小写功能动态地工作吗? I'm at my wits end here. 我的智慧到此为止。

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

Create a range holding the currency and format,such as 创建一个包含货币和格式的范围,例如

货币表

And name the range "currencies". 并将范围命名为“货币”。 Then change the Select Case section of your macro to... 然后将宏的“选择大小写”部分更改为...

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

This has the advantage that you can add new currencies or change formats without making any changes in the macro. 这样的好处是您可以添加新的货币或更改格式,而无需在宏中进行任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM