View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default macro to standard module

First, if this code is associated with a commandbutton from the control toolbox
toolbar, then the code does belong behind the worksheet.

And if your code that adds that worksheet named Assets has not run, then you
won't have that worksheet to select.

Option Explicit
Private Sub CommandButton2_Click()

Dim NextRow As Long
Dim wks As Worksheet

'since the code is behind the worksheet (summary) that owns
'the code, you can use the Me keyword to refer to that sheet
With Me
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
With .Range("A" & NextRow)
.Value = "Assets"
.Font.Bold = True
.Font.Size = 12
End With
End With

'check to see if a worksheet named Assets really exists
Set wks = Nothing
On Error Resume Next
Set wks = Me.Parent.Worksheets("assets")
On Error GoTo 0

If wks Is Nothing Then
MsgBox "No worksheet named Assets in this workbook!"
Else
wks.Select
End If
End Sub


Any chance that your code was supposed to make a worksheet named Assets first???

Simon wrote:

I have a .xlt with 2 macros in it.
The .xlt is copied in code from Access to a .xls and then data is
transferred from Access to the .xls
Both of the macros are activated on click of seperate command buttons.
The command buttons are in Sheet1 (Code) the sheet name is "Summary"
Macro 1 runs fine but I am getting an error on the 2nd as below.
Private Sub CommandButton2_Click()
Dim FirstRow As Long
Dim LastRow As Long
Dim iRow As Long

With Worksheets("Summary")
FirstRow = 2
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With

For iRow = FirstRow To LastRow
Next iRow

Range("A" & iRow).Select
ActiveCell.Value = "Assets"
ActiveCell.Font.Bold = True
ActiveCell.Font.Size = 12
Sheets("Assets").Select ERROR HERE Run-time error 1004 select method of
range class failed

I have discovered thanks to usergroup that this needs to be in a standard
module.
Can someone tell me how I do this and then how do I call it.
Thank you


--

Dave Peterson