Pass variable to macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Public sCol As String


must be declared in the general/standard module at the very top outside any
procedures.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top