我按了 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 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()
这应该遍历从第 6 行第 1 列到第 15 行第 1 列的单元格。它将在迭代中获取当前单元格的值并将其与下一行日期进行比较。如果它们不相同,则在该单元格的底部添加一个边框。
这是一个简单的测试,但我遇到了问题,当我运行它时,它说外部程序无效。有任何想法吗?
我假设我需要
- 定位 Excel 文件上的工作表。
- 有一些合适的地方来添加我的样式类的全局范围声明。
- 纠正我的代码中的任何错误。
编辑:::
类模块
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
Private Sub Class_Initialize()
Dim MyStyles As style
Set MyStyles = New style
End Sub
工作簿打开
Private Sub Workbook_Open()
MyStyles.IterateThroughData()
End Sub