Solved

Issue with VBA and excel code

Posted on 2014-10-29
3
270 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

19 Experts available now in Live!

Get 1:1 Help Now