Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 63
Default Pass variable to macro

Hello and TIA. I'm trying to pass along a variable from a workbook_open macro
to another macro, but the variable is not getting passed along. The macro
"GenerateWord" in a standard module isn't recognizing "sCol".

Public sCol As String
Sub Workbook_Open()
sCol = InputBox("Enter column.")
GenerateWord
End Sub

Sub GenerateWord()
Dim iLastRow As Long
'Set range
With Workbooks("words.xls").Sheets("test")
iLastRow = .Cells(Rows.Count, sCol).End(xlUp).Row 'error occurs here
......
End Sub

Regards,
Jason
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Pass variable to macro

Public sCol As String


must be declared in the general/standard module at the very top outside any
procedures.

--
Regards,
Tom Ogilvy

"Jason Morin" wrote in message
...
Hello and TIA. I'm trying to pass along a variable from a workbook_open

macro
to another macro, but the variable is not getting passed along. The macro
"GenerateWord" in a standard module isn't recognizing "sCol".

Public sCol As String
Sub Workbook_Open()
sCol = InputBox("Enter column.")
GenerateWord
End Sub

Sub GenerateWord()
Dim iLastRow As Long
'Set range
With Workbooks("words.xls").Sheets("test")
iLastRow = .Cells(Rows.Count, sCol).End(xlUp).Row 'error occurs here
......
End Sub

Regards,
Jason



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default Pass variable to macro

Hii Jason

You could try
iLastRow = .Cells(Rows.Count, ThisWorkbook.sCol).End(xlUp).Row

but that's not really passing a variable, it's making the variable
available. To really pass it, do

Sub Workbook_Open()
Dim sCol As String
sCol = InputBox("Enter column.")
Call GenerateWord(sCol)
End Sub

Sub GenerateWord(sCol As String)
MsgBox "You passed " & sCol
End Sub

Note that iLastRow will still err as is, because Cells need a numeric column
reference, not a column letter / string variable.

HTH. Best wishes Harald

"Jason Morin" skrev i melding
...
Hello and TIA. I'm trying to pass along a variable from a workbook_open

macro
to another macro, but the variable is not getting passed along. The macro
"GenerateWord" in a standard module isn't recognizing "sCol".

Public sCol As String
Sub Workbook_Open()
sCol = InputBox("Enter column.")
GenerateWord
End Sub

Sub GenerateWord()
Dim iLastRow As Long
'Set range
With Workbooks("words.xls").Sheets("test")
iLastRow = .Cells(Rows.Count, sCol).End(xlUp).Row 'error occurs here
......
End Sub

Regards,
Jason



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
pass variable from one workbook to another calebjill Excel Discussion (Misc queries) 2 January 28th 09 07:38 PM
How to pass a variable into an SQL statement CLamar Excel Discussion (Misc queries) 0 June 5th 06 02:17 PM
Pass a variable into a range? Ian Fleming[_2_] Excel Programming 1 September 7th 05 09:45 AM
Pass variable from user form to macro r wilcox Excel Programming 1 May 23rd 05 07:27 PM
Using Public to Pass Variable D.Parker Excel Programming 8 March 24th 05 10:39 PM


All times are GMT +1. The time now is 05:07 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"