Runtime error for macro that works in workbook created in

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

Guest

Hi there,

I created a macro to fill in fields and saved it in my personal workbook so
it could be used for all my workbooks. It works in the original workbook I
created it in, but when I try to open it in others, I get the 1004 runtime
error!

Is there something I'm doing wrong? All my fields are named, and when I run
it, it fills in the first field but gives me the error immediatly after.

Here's my code, hopefully someone can offer me help.
Thanks!
Crystal

Sub Macro1()
'
' FormFill Macro
'
Range("REQUESTORF").Select
ActiveCell.FormulaR1C1 = "Crystal"

Range("REQUESTORL").Select
ActiveCell.FormulaR1C1 = "Nassouri"

Range("ADDRESS").Select
ActiveCell.FormulaR1C1 = "Oakway"

Range("E2").Select
End Sub
 
Two things: first, is your code in the right place? Where are you running from? Second, allow me to give you a hand. This code will
both eliminate your error and allow for some feedback to help determine what the problem is. Just assign a hotkey to this new
procedure and go! When it runs it will either inform you that it couldn't find any named ranges in the workbook, or it will tell you
which workbook it acted upon, the number of named ranges found and the number of "fields" filled (preferably 3, right?) -- any
problems let me know what the result is and we'll track it down. :)


Sub FillFields()

Dim aName As Name, aCount As Single, fCount As Single

aCount = 0
fCount = 0

For Each aName In ThisWorkbook.Names
aCount = aCount + 1
If UCase(aName.Name) = "REQUESTORF" Then
aName.RefersToRange.Formula = "Crystal"
fCount = fCount + 1
ElseIf UCase(aName.Name) = "REQUESTORL" Then
aName.RefersToRange.Formula = "Nassouri"
fCount = fCount + 1
ElseIf UCase(aName.Name) = "ADDRESS" Then
aName.RefersToRange.Formula = "Oakway"
fCount = fCount + 1
End If
Next aName

If aCount = 0 Then
MsgBox prompt:="Couldn't find any of the fill fields!", _
Title:="FillFields Error"
Else
MsgBox prompt:="Processed workbook " & ThisWorkbook.Name _
& vbNewLine & "Found " & aCount & " named ranges; filled " _
& fCount & " fields.", Title:="FillFields Result"
End If

End Sub
 

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