Convert a cell or multiple cells from SGD value to USD value based on relevant monthly exchange rate

ameenuksg

Board Regular
Joined
Jul 11, 2017
Messages
83
Hi I want to create a VBA to convert 1 or multiple cells in Sheet 1 to the appropriate value by selecting them in column A and applying the relevant monthly exchange rate from Sheet 2.

Would greatly appreciate any help:)

Sheet 1Sheet 1Sheet 2Sheet 2
ABAB
Net Sales - $Sales DateFeb 20200.7543
2003 Mar 2020Mar 20200.7489
3004 Apr 2020Apr 20200.7123
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Does the below macro do what you're looking for ?

VBA Code:
Sub ApplyRate()

Dim Ar1 As Variant, Ar2 As Variant
Ar1 = Sheets("Sheet2").Range("A1").CurrentRegion '<--- change sheet name as needed
Ar2 = Sheets("Sheet1").Range("A1").CurrentRegion '<--- change sheet name as needed

With CreateObject("scripting.dictionary")
    For x = 2 To UBound(Ar1)
        If Not .exists(Ar1(x, 1)) Then .Add WorksheetFunction.EoMonth(Ar1(x, 1), 0), Ar1(x, 2)
    Next x
    For x = 2 To UBound(Ar2)
        Ar2(x, 1) = Ar2(x, 1) * .Item(WorksheetFunction.EoMonth(Ar2(x, 2), 0))
    Next
End With

Sheets("Sheet1").Range("A1").Resize(UBound(Ar2), UBound(Ar2, 2)) = Ar2

End Sub
 
Upvote 0
What if the sales figures in Sheet 1 that I want to convert ranges from column S to AB with a choice of selecting the whole range or just 1 cell.
Also, Sheet 2 data starts in a table from row A3 to B7(after a header(Row 2) and a title(Row 1)) which will be extended further as a new month arrives.
What changes do I need to make to the above VBA?
 
Upvote 0
Hey ameenuksg,

Try the below code, it will prompt you to select the range (or multiple ranges) in question where you need to apply the rate. In case you don't want to select the range every time you run the macro, I have added some comments on what to change in the code

VBA Code:
Sub ApplyRate_V2()

Dim a, Rg As Range, c As Range
a = Sheets("Sheet2").[A1].CurrentRegion '<--- change sheet name as needed
Set Rg = Application.InputBox("Select your range", Type:=8) ' Comment this line if you want to make your range fixed
'Set Rg = sheets("Sheet1").range("S1:AB1000") 'Uncomment this line if you comment above line

With CreateObject("scripting.dictionary")
    For x = 3 To UBound(a) '<--- 3 is the 3rd row where your first number of data starts in sheet2
        If Not .exists(a(x, 1)) Then .Add WorksheetFunction.EoMonth(a(x, 1), 0), a(x, 2)
    Next x

    For Each c In Rg.SpecialCells(2)
        If IsNumeric(c.Value) And IsDate(c.Offset(, 1).Value) Then
            c.Value = c.Value * .Item(WorksheetFunction.EoMonth(c.Offset(, 1).Value, 0))
        End If
    Next
End With

End Sub
 
Upvote 0
The VBA did prompt to select a range, but the figures didn't change. I forgot to mention that the sales date on Sheet 1 is located at Column F ?
 
Upvote 0
The sales date is always in column F ? Not adjacent to the sales figure as per the first example
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top