VBA和Excel,我不确定如何在特定的工作表上运行我的类中的方法
我按alt+f11,进入代码隐藏并编写了以下内容:VBA和Excel,我不确定如何在特定的工作表上运行我的类中的方法,excel,vba,Excel,Vba,我按alt+f11,进入代码隐藏并编写了以下内容: Class style Public Sub IterateThroughData() Dim rowIndex As Integer Dim colIndex As Integer Dim rowOffset As Integer rowOffset = 6 colIndex = 1 For rowIndex = rowOffset To
Class style
Public Sub IterateThroughData()
Dim rowIndex As Integer
Dim colIndex As Integer
Dim rowOffset As Integer
rowOffset = 6
colIndex = 1
For rowIndex = rowOffset To 15
Dim currentDate As Date
Dim nextRowDate As Date
currentDate = Cells(rowOffset, colIndex).Value
nextRowDate = Cells(rowIndex + 1, colIndex).Value
If currentDate <> nextRowDate Then
RenderYearStyle(rowIndex,colIndex)
End If
currentDate = Cells(rowIndex, colIndex).Value
Next rowIndex
End Sub
Private Sub RenderYearStyle(rowIndex As Integer, colIndex As Integer)
With Cells(rowIndex, colIndex)
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlInsideHorizontal).Weight = xlThin
End With
End Sub
End Class
Dim style As style
Set style = New style
style.IterateThroughData()
代码的底部部分
Dim style As style
Set style = New style
style.IterateThroughData()
…未包含在正确的代码块中。您需要从启动宏。这可能是允许的,但对于类和从该类派生的对象使用相同的名称(
样式
)肯定会让人困惑。哈,我正在阅读如何定义静态类,对此感到厌烦,确实,您没有看到试图让静态类工作的汗水和眼泪:)但要点是:d您编写代码的方式,默认情况下,它将在当前的ActiveSheet
上运行:如果您将sheet对象作为参数传递给类方法,您将发现更容易进行故障排除。很好,我要试一试!运行的命令能否像我编写的第一个代码一样进入全局空间?我是否需要将类放入类模块中?然后做一个工作簿\打开子文件夹?请参阅上面的代码-p.s它不工作。我到了吗?我真的希望代码在excel工作表打开时运行(我已启用所有宏设置)是-每个类需要一个单独的类模块。您的问题不清楚您是否需要工作簿\打开子项。啊,我明白了,所以我创建了一个类,并在属性检查器中设置了名称,以等待我的类声明。现在我有了一个定义了sub的类模块。现在我只需要找到一个入口点,在这里我可以运行一些代码来创建我的类的实例,并运行其中定义的方法!干杯,伙计们,我明白了
Private Sub Workbook_Open()
MyStyles.IterateThroughData()
End Sub
Dim style As style
Set style = New style
style.IterateThroughData()