Solved

set/show a string in vba in excel

Posted on 2014-01-22
4
366 Views
Last Modified: 2014-01-22
hi,
can someone help me. i'm trying to set a string, and then show it in a msgbox using vba in excel and for some reason i can't get it working.

says object doesn't support this property or method. highlighting  "Fname = Report.Range("J11").Text". if i comment this out, I get the same error message on "Msgbox Report"

can't figure out what's up

Thanks,

Sub CopyPaste2()
    Dim Wb As Workbook                  ' current workbook
    Dim Report As Workbook              ' referenced workbook
    Dim Ffn As String                   ' full file name of Report
    Dim Fname As String                    ' retrieved file name
    
    Ffn = "H:\F&O Report Instructions Macro 1-22-2014.xlsm"
    Set Report = GetWorkbook(Ffn)
    If Report Is Nothing Then
        MsgBox "Couldn't find the Report", vbCritical, _
               "Missing workbook"
        Exit Sub
    Else
        Fname = Report.Range("J11").Text
        MsgBox Fname
    End If
    'If Len(Fn) Then
    End Sub
Private Function GetWorkbook(Wn As String) As Workbook

    Dim Wb As Workbook
    Dim Sp() As String
    
    Sp = Split(Wn, "\")
    Debug.Print Sp(UBound(Sp))
    On Error Resume Next
    Set GetWorkbook = Workbooks(Sp(UBound(Sp)))
    If Err Then
        Set GetWorkbook = Workbooks.Open(Wn)
    End If
End Function

Open in new window

0
Comment
Question by:jfrank14
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 39799856
yes for sure you will get an error as you cannot associate a range to a workbook you can associate a range to a worksheet. So say in your workbook Report it is the first sheet you want to examine then do this:

replace this line
Fname = Report.Range("J11").Text

by this line
Fname = Report.worksheets(1).Range("J11").Text

gowflow
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 39799909
Alternatively, if you give the required cell (range J11) a name in the Name Manager, eg FileName, you can then use:

Fname = Report.Range("FileName").Text

When creating the name, make sure it has scope for Workbook rather than Worksheet otherwise you will endup back with the same problem of having to define the sheet.

Thanks
Rob H
0
 

Author Closing Comment

by:jfrank14
ID: 39800722
that did it, gowflow . thanks!
0
 
LVL 29

Expert Comment

by:gowflow
ID: 39801067
Your welcome
gowflow
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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

919 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

15 Experts available now in Live!

Get 1:1 Help Now