Guest User

Untitled

a guest
Jun 20th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. ' Set references to workbooks and sheets
  2. Set wbCompany = Workbooks("Company_model_24_draft.xlsx")
  3. Set wbRates = Workbooks("exchange_rates.xlsx")
  4. Set wsCompany = wbCompany.Sheets("INDEX1")
  5. Set wsRates = wbRates.Sheets("monthly_ECB") ' Adjust sheet name as needed
  6.  
  7. ' MAKE SURE TO DELETE IRRELEVANT ROWS IN THE EXCHANGE RATES WORKBOOK (e.g. YEARS 2000 - 2022)
  8.  
  9. ' Read Reference Currency and Last Fiscal Year Ended
  10. refCurrency = wsCompany.Range("C5").Value
  11. lastFiscalYear = wsCompany.Range("C6").Value
  12.  
  13. ' Check if D8 or D9 is already 1
  14. If wsCompany.Range("D8").Value <> 1 Then
  15. ' Search for the corresponding date range
  16. For i = 2 To wsRates.Cells(Rows.Count, 2).End(xlUp).Row
  17. dateRange = wsRates.Cells(i, 2).Value
  18.  
  19. If lastFiscalYear >= DateValue(Split(dateRange, " - ")(0)) And lastFiscalYear <= DateValue(Split(dateRange, " - ")(1)) Then
  20. ' Found the correct date range
  21. exchangeRateUSD = wsRates.Cells(i, refCurrency & "->USD").Value
  22. Exit For
  23. End If
  24. Next i
  25. ' Update D8 if not already 1
  26. wsCompany.Range("D8").Value = exchangeRateUSD
  27.  
  28. ElseIf wsCompany.Range("D9").Value <> 1 Then
  29. ' Search for the corresponding date range (EUR)
  30. For i = 2 To wsRates.Cells(Rows.Count, 2).End(xlUp).Row
  31. dateRange = wsRates.Cells(i, 2).Value
  32.  
  33. If lastFiscalYear >= DateValue(Split(dateRange, " - ")(0)) And lastFiscalYear <= DateValue(Split(dateRange, " - ")(1)) Then
  34. ' Found the correct date range
  35. exchangeRateEUR = wsRates.Cells(i, refCurrency & "->EUR").Value
  36. Exit For
  37. End If
  38. Next i
  39. ' Update D9 if not already 1
  40. wsCompany.Range("D9").Value = exchangeRateEUR
  41. End If
  42.  
Advertisement
Add Comment
Please, Sign In to add comment