Help with String

wageslave101

Board Regular
Joined
Jul 18, 2007
Messages
154
This sub checks if the workbook is open, which is in the format "Rep Name.xls", this one works fine.

Code:
Sub IsRepOpen()
Dim wbook As Workbook, wkbk As Variant
    wkbk = "Rep Name"
    On Error Resume Next
    Set wbook = Workbooks(wkbk)
        If wbook Is Nothing Then
            Workbooks.Open (wkbk)
    Set wbook = Nothing
        Else
    Set wbook = Nothing
        End If
End Sub

However rather than going in and entering in the Rep Name I'd like to grab the details from a data entered into the Sheet

But when I redo the IsRepOpen() sub it does nothing. Stepping through it processes the line Workbooks.Open (wkbk) but does nothing.

Code:
Sub IsRepOpen()
Dim wbook As Workbook, wkbk As Variant
    TheRep = Worksheets("Sheet1").Range("A2").Value & " " & Worksheets("Sheet1").Range("B2").Value
    wkbk = TheRep
    On Error Resume Next
    Set wbook = Workbooks(wkbk)
        If wbook Is Nothing Then
            Workbooks.Open (wkbk)
    Set wbook = Nothing
        Else
    Set wbook = Nothing
        End If
End Sub

I thought that the string might not be reading or something so I checked with a messagebox

Code:
Sub RepName()
    MsgBox Worksheets("Sheet1").Range("A2").Value & " " & Worksheets("Sheet1").Range("B2").Value & ".xls"
End Sub

Which shows the string as Rep Name fine

Can anyone help with why this is behaving or what I need to do to make it play nice?

Thanks,
WageSlave101
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
This is just a short in the dark, but having spaces in file names or directories can often wreak havoc with certain types of code (that is why most programmers use underscores instead of blank spaces in names of folder, directories, variables, etc).

So with that in mind, try changing the name of your file to eliminate all spaces, and see if it makes a difference.
 
Upvote 0
Thanks jm14

In case it was some sort of previous formatting in the workbook and created a new one and set it up there. From the clean Workbook it behaves as expected so I've just imported the data from the old workbook :)

Thank you again :)

WageSlave
 
Upvote 0
What is wkbk?
If it is a workbook, then try declaring it as an object-

Try:
Code:
Set wkbk=TheRep

Set wbook = Workbooks(TheRep) 
        If wbook Is Nothing Then 
            Workbooks.Open (TheRep)
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,037
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top