Solved

set/show a string in vba in excel

Posted on 2014-01-22
4
357 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 31

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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.

708 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

17 Experts available now in Live!

Get 1:1 Help Now