使用类对象来制作不同的形状

我的Excel工作表创build一个包含各种图层的地图,显示由线条,正方形,点和三angular形组成的networking。 我有每个创作的function,需要参数如

cellLocation As Range '(takes a given cell location) shapeType As String '(oval, triangle, rectangle) Color '(red, black, whatever) sizeFactor As Double '(factors the shape size as a function of cell's width) 

我现在只是在学习类,但是想知道在这种情况下类是否有用,以及如何使用它们来简化我的代码,而不是具有6个不同的参数等的函数。

本来我有这样的function:

 Function CreateWell(cellRng As Range, wellName As String) 'creates a square of particular color, size, etc in the cellRng and names it wellName Function CreateCompressor(cellRng As Range, compName As String) 'creates an oval of particular color, size , etc. similar to other func. 

那么,因为我有大约5这些唯一的变化是颜色,大小,形状等我尝试了一个整体function:

  Function CreateShape(cellRng As Range, shpName As String, _ shpColor As String, shpSize as double, shpType As string) 

但是这似乎是混乱的(太多的论据)。 雇主们如何清理这种types的代码?

你是对的。 你需要使用面向对象的语言概念。 VBA不是一个完整的面向对象的语言,但它有一些很好的function。 我不能写你一个例子,因为有更多的方法来做到这一点。 想评论,而不是写一个答案,但我的低评级阻止我 。 阅读有关面向对象的概念和使用类,使用不同的邮票和枚举来实例化类的重载函数。 如果你想用更现代的语言(比如visual basic.net)来做这件事,请阅读关于inheritance和polymorphizem的知识 – 也是非常重要的概念来实现你所提到的任务。