Dynamic Subroutine Generation Creates Compile Error
Oct 4, 2007
I'm am running Excel 2003 SP2 on Windows XP SP2.
I have a macro which modifies cells in a spreadsheet. But for brevity sake, I have an empty spreadsheet which has an Auto_open macro and two subroutines in it. The subroutines initially consist of just the Sub and End Sub statements.
The Auto_open procedure reads in lines from two text files (generated by another application), and inserts the lines into each of the empty subroutines. Auto_open then runs the two subroutines.
I have been able to get this dynamic creation of the subroutines to work for one subroutine, but not for the second. I receive the compile error: "Only comments may appear after End Sub, End Function, or End Property". I don't see anything wrong with the inserted code.
The modified subroutines appear fine, and if I save the macros with the modified code, close excel, rename the text files so they are no longer read in, and re-open the spreadsheet, the auto_open procedure and the two subroutines run fine. This tells me the code itself is okay, and yet it won't work during the initial run.
Below is the macro code in its original state:
Sub Auto_open()
Dim fso, f
Dim VBCodeMod As Object
Dim LineNum As Long
Dim StrFileName As String
ShowVisualBasicEditor = True
Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
End Sub
----------------------
The contents of the read in file, %TEMP%subA.txt, is just one line:
MsgBox("inside SubA")
The contents of the read in file, %TEMP%subB.txt, is just one line:
MsgBox("inside SubB")
------------------------
When I open the spreadsheet the first time, it gives me the first message box from Sub A, but then generates the error and highlights the Sub SubB() line.
To duplicate the problem:
1. Insert the macro into a spreadsheet. Save and exit it.
2. Create the files %TEMP%subA.txt and %TEMP%subB.txt containing the single MsgBox lines.
3. Open up the spreadsheet. SubA will run and a message box will appear. Then the compilation error will occur.
4. Save and exit the spreadsheet.
SubA will now contain:
Sub SubA()
MsgBox("inside SubA")
End Sub
SubB will now contain:
Sub SubB()
MsgBox("inside SubA")
End Sub
5. Rename the two text files, so that the next time you open the spreadsheet it won't try to insert the lines from the files into the subroutines.
6. Re-open the spreadsheet. Two message boxes will now appear, one from SubA and one from SubB.
View 9 Replies
ADVERTISEMENT
Nov 17, 2013
I met problem in calling into a subroutine. It is error 424. I have dim all variable. However I still get it.
Code:
Sub UpdateAll()
'
' UpdateAll Macro
[Code]....
I got the error when I pressed F8 to step into the getOneRecord() function, which is highlighted in red.
View 2 Replies
View Related
Mar 2, 2007
I'm having trouble calling a subroutine from a command button. It's puzzling because I've set up buttons before and didn't have this trouble.
Here is my button
Private Sub EPConversionButton1_Click(ByVal target As Range)
Convert_Hrs_EP target
End Sub
And here is the subroutine.
Sub Convert_Hrs_EP(target As Range)
End Sub
There's nothing there yet, but I keep getting error messages regarding the transfer from the button code to the subroutine.
The message is: Procedure declaration does not match description of event or procedure having the same name. The Help file says this means that my procedure has the same name as an event, but does not have the same signature. But it's not so.
View 9 Replies
View Related
Dec 12, 2006
I've written some code that fires on a worksheet_change event. It worked fine for the initial bit of code I wrote, but then once I repeated it for several different ranges, I got an error stating COMPILE ERROR: PROCDEURE TOO LARGE. (This amount of code below works fine, it's only when I add the rest it becomes too large.)
Private Sub Worksheet_Change(ByVal Target As Range)
'Unprotect Sheet
ActiveSheet.Unprotect Password:="clemson"
'Freeze screen
Application.ScreenUpdating = False
'Turn Off Auto Calc
With Application
.Calculation = xlManual
.MaxChange = 0.001
End With
' Master Bath Shower Listello
If Not Intersect(Target, Range("Listello1ShowerMasterBath")) Is Nothing Then
If UCase(Target.Value) = "NONE" Then
Range("Listello_1_Options_Shower_Master_Bath").EntireRow.Hidden = True:
Range("Listello_2_Shower_Master_Bath").EntireRow.Hidden = True:..........
View 9 Replies
View Related
Feb 7, 2007
I am trying to run create a simple macro that copies and paste special values - something I have done 100's of times but for some reason I keep getting an error message - even though I recorded the macro and didnt write it by hand - see below:
Sub Macro6()
Cells.Select
selection.Copy
selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
For which I get 'Compile Error - Expected Function or Variable'
View 6 Replies
View Related
Apr 26, 2006
I have some code that, although works fine in Excel 2003, does not in Excel 1997. I receive this error when I try running it:
COMPILE ERROR:
NAMED ARGUMENT NOT FOUND
Sub HPVAL()
Dim r As Range, myStr As String
myStr = "HP"
Set r = Cells. Find(What:=myStr, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not r Is Nothing Then
r = r.Value
While Not r Is Nothing
Set r = Cells.FindNext(r)
If Not r Is Nothing Then
r = r.Value
End If
Wend
End If
End Sub
It looks like Excel is getting hung up on the "SearchFormat:=" portion of the code.
View 2 Replies
View Related
May 20, 2009
I am looking for a more efficient way to write a macro (a sample from the macro is below). This is just the first part of the macro. I need to repeat these same steps (seen for row 5 below) for rows 5 to 50. My script worked until I hit row 35 and then I got the "compile error.." message. There must be a way to use "loop" to write this more efficiently, no?
View 14 Replies
View Related
May 17, 2014
VB:
Selection.FormulaArray = _
"=IF(RC[-7]=""Weekly"",RC[-1],IF((ParentCode=RC[-10])*(ClassType=""Active""),IF((EndDate=EOMONTH(EndDate,0))*(RC[-7]
={""Monthly"",""Quarterly""}),RC[-1],0),IF(RC[-7]=""Daily"",IF(SUMPRODUCT(((ParentCode=RC[-10])*(FundType=""C"")*
(ClassType=""Inactive Class"")*(TermDate<>"""")*(TermDate>=StartDate)*(TermDate<=EndDate))+((ParentCode=RC[-10])*
[Code] .....
I recorded macro for this formula its giving me syntax error i did " _" after the break but its still not working..
View 4 Replies
View Related
Feb 1, 2010
"Compile error - Syntax Error"
and the following line highlighted in yellow
"Sub CreateWorkbooks()"
It worked on Excel 2000 but not now and dont work either in Excel 2007.
Heres the complete code .....
View 9 Replies
View Related
May 4, 2008
Can some see why this Code would fail intermittently?
This gets executed after a Commandbutton is selected.
The Error Message is attached.
View 14 Replies
View Related
Sep 14, 2009
I am using the following code to put a combo box in my userform. When I try to run it I get the following Compile Error: For without next. What does it mean and how do I fix it?
View 9 Replies
View Related
May 2, 2008
just make some files to make my work easier. Few too many watching the game last night, and I think im lost here. Am I attempting to do too much here ? I ran the first For statement Fine, but I cant get the second to work. I dont understand where my next statement should be entered.
View 4 Replies
View Related
Jul 20, 2009
VBA but familiar with other programming languages.
I have looked through previous posts with the same problem and I see that most people forget to add the "endIf" before looping. From what I can tell, I have ended all of my "If" statements.
The goal of this macro is to take temporary data and finding a match in other sheets and copying from "Temp" and pasting into the other sheets (possibly in the first blank cell, depending on the case) then deleting the row and moving on to the next row and repeating the process until "Temp" is empty.
View 9 Replies
View Related
May 11, 2014
I am new to VBA. I have been getting compile error on the below code:
Public Sub hourCalculation()
Dim rcount As Integer
Dim rindex As Integer
Dim logintime As Date
Dim logoutime As Date
[Code] ..........
View 9 Replies
View Related
Jun 11, 2008
Every time I wrote something wrong I stupid and annoying pop windows appears and I have to press ok different times and be quick to correct the problem, otherwise it appears again.
View 9 Replies
View Related
Dec 3, 2009
I'm trying to write a macro that will insert a excel formula into a specific cell. When I try to run the macro I receive a compile/ syntax error. I don't understand why as the formula works in excel. Here is the code (formula only)
View 4 Replies
View Related
Mar 19, 2013
I have an application that will require I create 20+ objects from a class I have created call "cWorkCenter." This is my first project using objects in VBA. I've created a string variable called "Title." THe application will loop through a list of resources on a worksheet, assign the name of that resource to the variable called "Title" (eg. Title=Range("A1")), then create an object named whatever the string is that "Title" represents. For example, if the first item in my list is "Resource1", then the first object created should be called "Resource1"
When I do this I get the following error: Compile Error: Duplicate declaration in current scope.
Below is the part of the code giving me trouble.
VB:
Public Title As String
Sub Loader_OO()
Title = "Resource1" [code].....
View 2 Replies
View Related
Jun 10, 2014
I attempted to initialize values on a few Dim statements, but got compile errors: "expected end of sentence".
VB:
Dim sumTtl As Currency = 0
Dim searchTxt = "West"
View 4 Replies
View Related
Apr 9, 2014
Trring to use function edate() in VBA macro, I've activated atpvbaen.xls in Tools/Register but get compile error:"Sub or function not defined".
View 5 Replies
View Related
Jul 9, 2014
Have two worksheets in same workbook. First worksheet is "ReArrangedAddr" Which basically has a command button to click to run a "Sub" behind the second worksheet "Orig SH Register". When I click on button on first worksheet, I get error "Compile Error: Sub or Function not defined"
* * * * * code behind command button * * * * *
View 14 Replies
View Related
Aug 12, 2014
New to VBA and just trying to make some edits to some existing code. I have basically copied a pre-existing form and module and changed names from "Appendix" to "Drawing" as I am trying to replicate what the piece of code already produces for a table of appendices, for my drawings.
However, when I try and run the form I get Compile error: Variable not defined with lstDrawings highlighted.
[Code] ...........
Is this something that should be defined in my global module which I am missing?
View 3 Replies
View Related
Oct 9, 2007
I was wondering if anyone knows how to fix this. I've sent out a worksheet with macros that call up userforms but when someone runs the form and tries to call up some userforms they get this error.
View 9 Replies
View Related
Jan 14, 2009
I've written some VB code in Excel 2003 which hides and unhides worksheets dependant upon 'Yes' or 'No' in a named range. This works Ok in Excel 2003, however if I try and use it in Excel 2007 it shows error 'Compile Error in Hidden Sheet'. When I debug it the error 'Compile Error - Method or Data Member Not Found' appears. The code in question is as follows:
View 9 Replies
View Related
Mar 31, 2009
I am trying to use the Find function within some VBA code but keep encountering a compile error. Code works fine on it's own as below but doesn't work within the VBA code. I can't figure out what part of code needs to be modified.
The desired result in J2 = "Jim"
the value in cell I2 = "Jim |Anderson"
Working Function as follows:
View 3 Replies
View Related
Jul 9, 2009
I sent the attached to a user who said it wouldn't run when the 'Draw Venn' button is click - some sort of RefEdit compile error. I can't replicate the error and can't find what's causing the error.
I had started using RefEdit control but couldn't get it to work the way I wanted, so just went with input boxes. I run this on XL07 SP2, other user has XL03 - not sure if that's relevant to the error they're getting.
View 4 Replies
View Related
Feb 16, 2010
Hello, Im using a script that allows me to autocomplete a data validation list using a combo box and the code shown below. However I am getting a compile error ambigous name detected and the following line is highlighted
"Private Sub Worksheet_SelectionChange(ByVal Target As Range)"
I know its because this is used twice but I am not sure what to do in order to fix this.
View 11 Replies
View Related
Nov 21, 2011
Compile Error: Invalid Outsid Procedure
I have Compile error coming up when I try to run Sub DeleteRecord on Sheet 2. It brings me to the VBE with sheet 1 code brought up and the Compile Error showing, with the xlup portion of the code highlighted. I okay the error and it highlights the Sub DeleteRecord on Sheet 2 with a yellow arrow.
Sheet 1 code as follows:
Dim y As Long
y = Range("A65536").End(xlUp).Row
Range("A1").AutoFill Destination:=Range("A1:A" & y), Type:=xlFillDefault
Range("J1").AutoFill Destination:=Range("J1:J" & y), Type:=xlFillDefault
Range("K1").AutoFill Destination:=Range("K1:K" & y), Type:=xlFillDefault
Range("L1").AutoFill Destination:=Range("L1:L" & y), Type:=xlFillDefault
Range("M1").AutoFill Destination:=Range("M1:M" & y), Type:=xlFillDefault
Range("N1").AutoFill Destination:=Range("N1:N" & y), Type:=xlFillDefault
Range("O1").AutoFill Destination:=Range("O1:O" & y), Type:=xlFillDefault
Range("P1").AutoFill Destination:=Range("P1:P" & y), Type:=xlFillDefault
Sheet 2 code at follows:
Sub DeleteRecord()
'
' DeleteRecord Macro
'
'
Application.ScreenUpdating = False
ActiveSheet.Unprotect
ActiveCell.Offset(4, 3).Range("A1:H2").Select
[code]....
View 3 Replies
View Related
Jan 21, 2012
The following code has been used previously to enter data from a userform to a worksheet without a problem. However, since I added some new bits of code I am getting a compile error with the message variable not defined.
Here is part of what I have so far and the bit that is highlighted after the error comes up is the 'Set ws' line;
Code:
Private Sub CommandButton1_Click()
Worksheets("Duties").Range("C5") = txtdate
Worksheets("Duties").Range("H5") = txtarea
Worksheets("Duties").Range("N5") = txttea
Set ws = Worksheets("Duties")
'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
The code I have recently added:
Code:
Private Sub UserForm_Initialize()
Set wsRes = Worksheets("Resources")
With wsRes
.Range("B1", .Range("B" & Rows.Count).End(xlUp)).AdvancedFilter xlFilterCopy, , .Range("L1"), True
With .Range("L2", .Range("L" & Rows.Count).End(xlUp))
cboTeam.List = .Value
.EntireColumn.Clear
[Code] .......
View 8 Replies
View Related
May 17, 2012
I have a two set of Macros in a workbook. One is to create a command button on a sheet and other macro will run onece the created commond button is clicked.
Macro runs fine. Command button gets created, but when I click the command button to run another macro it gives error "Compile Error: Sub or Function not defined" highlighting the code "Call Add" at the code entered for sheet. This code is added by macro in the sheet1. I am attaching a sample file as well as codes.
Sub CreateButton()
Dim Obj As Object
Dim Code As String
Sheets("Sheet1").Select
'create button
Set Obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=600, Top:=30, Width:=100, Height:=35)
Obj.Name = "InsertInvoiceButton"
'buttonn text
[code]....
View 9 Replies
View Related
Aug 13, 2013
Why do I get the below error when I open my workbook? The highlighted function is below.
Private Sub CommandButton1_Click()
Dim myText As Variant
If TextBox1.Value = "code" Then
UserForm1.Hide
Else
Me.Label1.Visible = True
Me.Label1.ForeColor = vbRed
End If
End Sub
View 1 Replies
View Related