What would be the best way to write repetitive code like this?
Below is my code, which opens up a Monarch instance, and does some actions with Monarch (Monarch is a datamining tool)
I have to repeat the below code, simply changing a few parameters each time, is there a way to programatically do this? I want to know the most effeicent way of doing this.
a = MonarchObj.SetReportFile(rawdataS10, False)
openModel = MonarchObj.SetModelFile(freeKidsModel)
MonarchObj.CurrentFilter = "Under 60"
SummerDateShow = MonarchObj.SetFieldVisible("Date2", False)
SummerDateHide = MonarchObj.SetFieldVis开发者_JAVA百科ible("Date", True)
ExportTOS = MonarchObj.JetExportTable(saveDir, "FreeKidsS10", 0)
MonarchObj.CloseAllDocuments
An example of the parts i'm changing are "Under 60" "FreeKidsS10" and the Date & Date2 fields.
If simply copy and pasting this code is the most effecient code, you can tell me to go away
You could make a procedure that takes as parameters the variable fields and then call that procedure wherever you would otherwise copy and paste. The signature of that method would be something like this:
Public Sub DataMiningMethod(ByVal modelFile as object, _
ByVal currentFilter as String, _
ByVal date1 as Boolean, _
ByVal date2 as Boolean, _
ByVal exportTableName as String)
Thus the entire method would look something like this:
Public Sub DataMiningMethod(ByVal modelFile as object, _
ByVal currentFilter as String, _
ByVal date1 as Boolean, _
ByVal date2 as Boolean, _
ByVal exportTableName as String)
a = MonarchObj.SetReportFile(rawdataS10, False)
openModel = MonarchObj.SetModelFile(modelFile )
MonarchObj.CurrentFilter = currentFilter
SummerDateShow = MonarchObj.SetFieldVisible("Date2", date2 )
SummerDateHide = MonarchObj.SetFieldVisible("Date", date1 )
ExportTOS = MonarchObj.JetExportTable(saveDir, exportTableName , 0)
MonarchObj.CloseAllDocuments
End Sub
You can create a user defined function - there are many examples on google - here is one -
http://www.fontstuff.com/vba/vbatut01.htm
Put this code in a method and if you want to be more precise put in a class and class it with differ parameters each time like
public void mymethod(string currentFilter, bool date2, bool date)
{
a = MonarchObj.SetReportFile(rawdataS10, False)
openModel = MonarchObj.SetModelFile(freeKidsModel)
MonarchObj.CurrentFilter = currentFilter
SummerDateShow = MonarchObj.SetFieldVisible("Date2", date2)
SummerDateHide = MonarchObj.SetFieldVisible("Date", date)
ExportTOS = MonarchObj.JetExportTable(saveDir, "FreeKidsS10", 0)
MonarchObj.CloseAllDocuments
}
I see you are using Excel. You can try using another sheet with data and making this methods getting each line at a time,something like:
A B C D
1 Under60 FreeKidsS10 Date Date2
2 Under80 FreeKidsS20 DateZ Date3
3 Over80 FreeKidsS30 DateX Date4
and you change you macro to use something like:
a = MonarchObj.SetReportFile(rawdataS10, False)
openModel = MonarchObj.SetModelFile(freeKidsModel)
MonarchObj.CurrentFilter = 'Sheet2!$A1'
SummerDateShow = MonarchObj.SetFieldVisible('Sheet2!$D1', False)
SummerDateHide = MonarchObj.SetFieldVisible('Sheet2!$C1', True)
ExportTOS = MonarchObj.JetExportTable(saveDir, 'Sheet2!$B1', 0)
MonarchObj.CloseAllDocuments
I am not sure on the exact code, but I thing you can go along this line to reach necessary target
精彩评论