Как извлечь валюту из VBA / BDP «Общая валюта» - PullRequest
0 голосов
/ 24 октября 2018

У меня есть список:

           Price in EUR       Price in Home Country
Total AUD
Svenska     10
Ubinse      15 
Illuao      20
Total USD
Zelo        12
Jhasma      11
Hedsaw      17

В идеале, я хочу использовать VBA для вставки сабвуфера, который вставляет функцию BDP в каждую строку столбца Price in Home Country

Так как:

for i = 1 to 7
if IsEmpty(Cells(i,2)) = True Then
Else
Cells(i,3).Value = PriceHomeCountry(Cells(i,2), Cells(ws.Rows.Count,2).End(xlBottom).Offset(1,-1)
End if 
Next i 

Обратите внимание, что я хочу использовать Cells(ws.Rows.Count,2).End(xlBottom).Offset(1,-1).Value для обозначения "Total Insert Currency" выше

Я не уверен, как структурировать функцию PriceHomeCountry()

Предложение:

Function PriceHomeCountry(rng1 as Range, rng2 as Range)
'I want to basically separate the "Total" from the "Currency" in rng2,let's call the result rng2.1
PriceHomeCountry = "=BDP( "EUR" & rng2.1 & " Crncy")*rng1
End Function

1 Ответ

0 голосов
/ 24 октября 2018

Этот код должен сделать свое дело.Я не могу проверить использование формулы BDP, так как у меня нет надстройки, поэтому я дал три способа ее использования.

Sub Test()

    Dim rCell As Range
    Dim CalcRange As Range
    Dim CountryRange As Range
    Dim TotalCell As Range
    Dim HomeCountry As String

    'Every reference to a range that starts "." will be referencing Sheet1.
    With ThisWorkbook.Worksheets("Sheet1")

        'Define the ranges we're working with.
        Set CountryRange = .Range("A2:A9")
        Set CalcRange = .Range("C2:C9")

        'Look at each cell in C2:C9.
        For Each rCell In CalcRange
            If Not IsEmpty(rCell.Offset(, -1)) Then
                'Find the first cell before the current rCell in column A that contains the word Total.
                Set TotalCell = CountryRange.Find(What:="Total", After:=rCell.Offset(, -2), _
                    LookIn:=xlValues, LookAt:=xlPart, SearchDirection:=xlPrevious)

                'If found then check the found value is in a row higher than rCell.
                ' - FIND wraps when it reaches the top so could find a Total from lower down.
                If Not TotalCell Is Nothing Then
                    If TotalCell.Row < rCell.Row Then
                        rCell = PriceHomeCountry(rCell.Offset(, -1), TotalCell)

                        'If PriceHomeCountry isn't working this will place the formula in column C.
                        'rCell.Formula = "=BDP(""EUR" & Split(TotalCell, " ")(1) & " Curncy"",""PX_LAST"")"
                    End If                        
                End If
            End If
        Next rCell
    End With

End Sub

Public Function PriceHomeCountry(rng1 As Range, rng2 As Range) As Variant

    'Should work if you set a reference to the Bloomberge add-in in Tools ~ References.
    PriceHomeCountry = BDP("EUR" & Split(rng2, " ")(1), " Curncy", "PX_Last") * rng1

    'Might work without setting a reference.
    'PriceHomeCountry = Application.Run("BDP", "EUR" & Split(rng2, " ")(1), " Curncy", "PX_Last") * rng1

End Function  

Редактировать: Также спасибо @assylias за помощь в создании формулы.

...