1

I have the below code, but for some reason, the "myRng.Offset(LastRow, 0).Value" is "Empty" and therefore the calculations are not performed and I receive the Overflow error due to a divide by 0.

Dim ws1 As Worksheet, wb1 As Workbook, LastRow As Long, LastCol As Long, myCol As String
Dim RowTest As Long, myRng As Range, FXwb As Workbook, ws2 As Worksheet
Dim FXrng As Range, FXval As Variant, Cur As String

LastRow = ws1.Range("B" & Rows.Count).End(xlUp).Row
Set myRng = Range("1:1").Find("LocalCurrency")
If myRng.Offset(LastRow, 0).Value <> "USD" Then
    Cur = myRng.Offset(LastRow, 0).Value
    FXwb.Activate
    Set FXrng = Range("C:C").Find(Cur)
    FXval = FXrng.Offset(0, 1).Value
    wb1.Activate
    Set myRng = Range("1:1").Find("Commitment (USD)")
    myRng.Offset(LastRow, 0).Value = myRng.Offset(LastRow, -1).Value / FXval
    Set myRng = Range("1:1").Find("Funding (USD)")
    myRng.Offset(LastRow, 0).Value = myRng.Offset(LastRow, -1).Value / FXval
    Set myRng = Range("1:1").Find("Adjusted Valuation (USD)")
    myRng.Offset(LastRow, 0).Value = myRng.Offset(LastRow, -1).Value / FXval
    Else
    Set myRng = Range("1:1").Find("Commitment (USD)")
    myRng.Offset(LastRow, 0).Value = myRng.Offset(LastRow, -1).Value
    Set myRng = Range("1:1").Find("Funding (USD)")
    myRng.Offset(LastRow, 0).Value = myRng.Offset(LastRow, -1).Value
    Set myRng = Range("1:1").Find("Adjusted Valuation (USD)")
    myRng.Offset(LastRow, 0).Value = myRng.Offset(LastRow, -1).Value
End If

New edit - changed FXval from Long to Variant - code works perfectly now! thanks to Jeeped for the answer and Chris Neilson for the suggestions

2
  • FWIW, your use of Find is problematic. Because you haven't specified anything other than the What parameter, you might not get the results you expect. The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method (either the user in Excel or with VBA). If you do not specify values for these arguments the next time you call the method, the saved values are used. Also, you are using implicit references to the ActiveSheet Commented Sep 13, 2018 at 1:18
  • I will definitely look into the LookIn/LookAt - thanks! Commented Sep 13, 2018 at 14:39

1 Answer 1

1

Your maths are wrong. You are starting at row 1 and offsetting the total number of rows so you end up 1 row beyond the populated values.

myRng.Offset(LastRow - 1, 0).Value
'alternate
ws1.cells(LastRow, myRng.column).Value
Sign up to request clarification or add additional context in comments.

5 Comments

would that be true throughout the snippet?
If you want the last populated cell in each 'found' column then yes, it would be true throughout. Perhaps the alternate I offered makes more sense.
yeah - i just added "-1" to all of them and it did work, but now the FXval = FXrng.Offset(0, 1).Value throws off Object variable not set error. Should this be a variant or string? the data is a number
It would appear that cur cannot be found in Range("C:C").
was looking at the wrong column - should have been B not C - I'm all set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.