Consulting

Results 1 to 15 of 15

Thread: getting #N/A as value

  1. #1

    getting #N/A as value

    HI guys,

    When i tried to read some range from a closed excel sheet using the below code,

    [vba]
    Sheet2.Range("A1:H8") = "= 'C:\My Documents\OzGrid\" & "[Book1.xls]Sheet2'!RC"[/vba]

    i am getting the text values as #N/A, while i am getting the numbers correctly.

    Help me what i need to do to overcome the #N/A error

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Are you certain that those values are not actually the saved values in the source worksheet?
    Be as you wish to seem

  3. #3
    yes, i could able to get the number in the stored sheet also i am getting zero's for the cells which doesn't have any value, but the problem is if there is any text present in the stored sheet then i am getting #N/A.

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Is the text in the source sheet a literal value typed in the cell?
    Be as you wish to seem

  5. #5
    No, it's a auto generated sheet from a tool.

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    That doesn't really address the point of my question - do the cells contain formulas or actual values?
    Be as you wish to seem

  7. #7
    sorry

    It has the actual values only.
    "No formulas"

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    I am afraid I do not know then. I have never heard of that behaviour before and cannot replicate it, unless the source cells actually contain that error.
    Be as you wish to seem

  9. #9
    one thing i want to tell you,

    If i am going to open that source excel and close after saving it, the code works fine.

    is there any format issue???

  10. #10
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Then your output program is doing something odd. I can't imagine what if there are no formulas involved.
    Be as you wish to seem

  11. #11
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    From your first post, that only works IF both workbooks are open. To read one cell from a closed workbook use:
    [VBA]Sub t()
    MsgBox GetValue("x:\test", "test.xlsx", "Sheet1", "A1")
    End Sub

    '=GetValue("c:\files", "budget.xls", "Sheet1", "A1")
    Private Function GetValue(path, file, sheet, ref)
    ' path = "d:\files"
    ' file = "budget.xls"
    ' sheet = "Sheet1"
    ' ref = "A1:R30"

    Dim arg As String

    If Right(path, 1) <> "\" Then path = path & "\"

    If Dir(path & file) = "" Then
    GetValue = "file not found"
    Exit Function
    End If

    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
    Range(ref).Range("a1").Address(, , xlR1C1)

    GetValue = ExecuteExcel4Macro(arg)
    End Function[/VBA]

  12. #12
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Kenneth,
    The original code works perfectly with a closed source file since it just puts a straight link formula into each cell.
    Be as you wish to seem

  13. #13
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I don't understand what RC is. Should it not be RC1 or something like that or is RC a local name in the sheet?
    [VBA]Sheet2.Range("A1:H8") = "= 'C:\My Documents\OzGrid\" & "[Book1.xls]Sheet2'!RC" [/VBA]

  14. #14
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    RC is R1C1 notation for the same row and column as the formula is entered into. (it is effectively R[0]C[0] as a relative reference).
    Be as you wish to seem

  15. #15
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Quote Originally Posted by sathishesb
    No, it's a auto generated sheet from a tool.
    what you refer to as a tool, sounds like it may be automating excel to generate and save the values into the source workbook. In which case perhaps the automated instance of excel is not getting shutdown properly due to faulty code in the tool.

    What happens if you manually enter some text into a test workbook in some of the same cells with the same text values, then save and close that. Then run your code with the test workbook as the source?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •