Need to pass only one value from procedure

needhelpplease

New Member
Joined
May 20, 2011
Messages
4
I need to pass the value of a variable from one procedure to another without running the entire called procedure

Private Sub openReport(ByVal strProductFile as string)
MsgBox "Open previous week's report.", vbOKOnly + vbExclamation, "Open"
Application.Dialogs(xlDialogOpen).Show

strProductFile = Application.GetOpenFilename
End Sub

Public Sub vlookup()
Dim strProductFile as string
openReport strProductFile

...then I'll concatentate the name of the file with a vlookup formula

BUT

when I do it this way, it executes all of the openReport procedure...I only want the file name to be returned.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi,

To pass a variable from one procedure to another I think you have to use by reference and not ByVal
(Reference is default so can be omitted)

So the first line would be:

Code:
Private Sub openReport(strProductFile as string)

To return just the filename use Dir:
Code:
strProductFile = Dir(Application.GetOpenFilename)

However I don't know how to get around the file opening part of the procedure so I removed:

Code:
Application.Dialogs(xlDialogOpen).Show

...and used workbooks.open in the main body. I don't know the full picture so I'm just throwing in ideas.

Code:
Private Sub GetReport(strProductFile As String)
MsgBox "Open previous week's report.", vbOKOnly + vbExclamation, "Open"
strProductFile = Application.GetOpenFilename
 
End Sub
 
Public Sub vlookup()
Dim strProductFile As String
GetReport strProductFile
 
MsgBox Dir(strProductFile)
Workbooks.Open strProductFile
 
End Sub

Hope some of this helps.
 
Upvote 0
That doesn't quite make sense.

What do you want the sub to do if it's not to run the code in it?
 
Upvote 0
I wanted it to return the name of the file to use in a vlookup in another procedure, but not bring up the Open file dialog box again because the file was already open. I think I got it figured out...I did some restructuring with the procedures.

I’m actually having another issue now with a For Next look and If Then Statement.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p> </o:p>
I want to delete every row that does not have 28168-59 or 19062-59 in column B, except of course the header.<o:p></o:p>
<o:p> </o:p>
This worked (another than the header part) before I added an OR with another part#, but now that I have both, it’s deleting everything in the spreadsheet.<o:p></o:p>
<o:p> </o:p>
Any ideas?<o:p></o:p>
<o:p> </o:p>
Sub main()<o:p></o:p>
Dim ws As Worksheet<o:p></o:p>
<o:p> </o:p>
Set ws = ActiveSheet<o:p></o:p>
<o:p> </o:p>
For i = ws.Range("B65536").End(xlUp).Row To 1 Step -1<o:p></o:p>
If ws.Cells(i, 2) <> "28168-59" Or ws.Cells(i, 2) <> "19062-59" Then<o:p></o:p>
<o:p></o:p>
ws.Rows(i).Delete<o:p></o:p>
<o:p> </o:p>
End If<o:p></o:p>
Next<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
I’m sorry, I just signed up on here and I’m not familiar with the tags.<o:p></o:p>
 
Upvote 0
So all you wanted to find was the name of a file?

Why not just use it's Name property?

As for your new problem the only things I can see that might be causing it is the use of ActiveSheet and lack of a workbook reference.

To make sure you the code is runnning on the correct worksheet in the right workbook you should add a workbook reference and specify the worksheet
name explicitily.

Something like this perhaps.
Code:
' change the workbook name and extension as required
Set wbTheWorkbook = Workbooks("TheWBwiththeWSToDeleteFrom.xls")
 
Set ws = wbThisWorkbook("TheNameOfWSToDeleteFrom")
 
' loops back to row 2, assuming headers in row 1
For I = ws.Range("B65536").End(xlUp).Row To 2 Step -1
 
       Select Case ws.Range("B" & I).Value
           Case "28168-59", "19062-59"
               ' do nothing
           Case Else
               ws.Range("B" & I).EntireRow.Delete
       End Select
Next I
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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