Solved

set/show a string in vba in excel

Posted on 2014-01-22
4
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

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 33

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 31

Expert Comment

by:gowflow
ID: 39801067
Your welcome
gowflow
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 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