Solved

Issue with VBA and excel code

Posted on 2014-10-29
3
274 Views
Last Modified: 2014-10-29
I have the two following blocks of code to pull data from an external excel sheet:

Sub Host03GetValue()
    p = "c:\Test\"
    'p is the path to the document'
    f = "HOST03.xls"
    'f is the filename of the excel sheet'
    s = "tabvInfo"
    's is the sheet name'
    a = "A16"
    'a is the reference cell'
    Application.ScreenUpdating = False
    For r = 2 To 42
        For c = 1 To 1
            a = Cells(r, c).Address
            Cells(r + 8, c + 1) = GetValue(p, f, s, a)
        Next c
    Next r
    Application.ScreenUpdating = True
End Sub

Open in new window


Public Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
    Dim arg As String
'   Make sure the file exists
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If
'   Create the argument
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)
'   Execute an XLM macro
    GetValue = ExecuteExcel4Macro(arg)
End Function

Open in new window


The problem is that for the empty cells, it puts in a o rather than leaving it blank which is my desired effect.  I've tried added in a call for IsEmpty() but unfortunately I'm not able to get it to work properly due to my lack of experience with coding.  Any help/advice is appreciated.
0
Comment
Question by:Uptime Legal Systems
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Anthony Berenguel earned 500 total points
ID: 40411094
try this...
Public Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
    Dim arg As String
'   Make sure the file exists
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If
'   Create the argument
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)
'   Execute an XLM macro
    GetValue = ExecuteExcel4Macro(arg)
    if GetValue  = 0 then
       getvalue = ""
    end if
End Function

Open in new window

0
 
LVL 6

Author Closing Comment

by:Uptime Legal Systems
ID: 40411098
That is perfect, exactly what I needed.  Thanks much!
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 40411134
Awesome! I'm glad you're sorted out. Have a good day!

ab
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now