reference to macro button is lost

E

Excel-General

I had this macro in a workbook attached to a button on sheet1.
Sub months()
'count the number of worksheets
Sheets(1).Select
n = Worksheets.Count + 1
i = 2
' Skip the first worksheet - it's the controls sheet
Text = Range("D3")
Do
Sheets(i).Select

Range("V6").Select
ActiveCell.FormulaR1C1 = Text
i = i + 1
Loop Until i = n
End Sub


It goes to the 2nd sheet and changes a date. This macro worked fine
until I added a userform now this macro button doesn't work. It now
says the variables are undefined. Well they are undefined but they
worked before. I just want to know the reason why? The macro is in
module 1. The command button code is simple.
Public Sub CommandButton1_Click()
months
End Sub

So this sheet object with the button on can't find the module because
it doesn't know to look in both the module and the userform? The
userform macro is called from sheet1 as well. Just trying to
understand the process
tnx,

Is the reason because of adding the userform now this button doesn't
know how to find the months macro in the module? It could find it
before. NOthing is really changed except for adding a userform.
tnx,
 
D

Don Guillett

You had to DIM your variables. See the 3 lines at the top of your macro.
========
HOWEVER, she my shorter, without selection macro
Sub months1()
Dim i As Long
For i = 2 To Worksheets.count
Sheets(i).Range("v6") = Range("d3")
Next i
End Sub
=======
Sub months()
Dim n As Long
Dim i As Long
Dim text

'count the number of worksheets
Sheets(1).Select
n = Worksheets.count + 1
i = 2
' Skip the first worksheet - it's the controls sheet
text = Range("D3")
Do
Sheets(i).Select

Range("V6").Select
MsgBox ActiveCell
ActiveCell.FormulaR1C1 = text
i = i + 1
Loop Until i = n
End Sub
 
E

Excel-General

You had to DIM your variables. See the 3 lines at the top of your macro.
========
HOWEVER, she my shorter, without selection macro
Sub months1()
Dim i As Long
For i = 2 To Worksheets.count
Sheets(i).Range("v6") = Range("d3")
Next i
End Sub
=======
Sub months()
Dim n As Long
Dim i As Long
Dim text

'count the number of worksheets
Sheets(1).Select
n = Worksheets.count + 1
i = 2
' Skip the first worksheet - it's the controls sheet
text = Range("D3")
Do
Sheets(i).Select

Range("V6").Select
MsgBox ActiveCell
ActiveCell.FormulaR1C1 = text
i = i + 1
Loop Until i = n
End Sub

Thanks, I just found a web of crummy macros to deal with
 
E

Excel-General

okay, I just got acquainted with google groups. It just meant thanks
I'm grateful.
 

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

Top