|
- Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
- On Error Resume Next
- Cells.FormatConditions.Delete
- With Target.EntireRow.FormatConditions
- .Delete
- .Add xlExpression, , Target.Value <> ""
- .Item(0).Interior.ColorIndex = 20
- End With
- With Target.EntireColumn.FormatConditions
- .Delete
- .Add xlExpression, , Target.Value <> ""
- .Item(1).Interior.ColorIndex = 20
- End With
- End Sub
复制代码 上面是我在VBA已经测试通过的代码,是实现非空单元格单击之后行列变色。最近在网上搜到能用VB来封装VBA代码,所以非常感兴趣就拿上面代码做实验。照着网上的好多教程例子做都没有成功实现功能,所以来请教论坛的大神门。
以下是我再VB6中的代码:
- Public Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
- Dim xlapp As Object
- Set xlapp = GetObject(, "Excel.Application")
- On Error Resume Next
- xlapp.Cells.FormatConditions.Delete
- With xlapp
- With Target.EntireRow.FormatConditions
- .Delete
- .Add xlExpression, , Target.Value <> ""
- .Item(1).Interior.ColorIndex = 20
- End With
- With Target.EntireColumn.FormatConditions
- .Delete
- .Add xlExpression, , Target.Value <> ""
- .Item(1).Interior.ColorIndex = 20
- End With
- End With
- End Sub
复制代码- Public Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
- Dim xlapp As Object
- Set xlapp = GetObject(, "Excel.Application")
- On Error Resume Next
- xlapp.Cells.FormatConditions.Delete
- With xlapp.Target.EntireRow.FormatConditions
- .Delete
- .Add xlExpression, , Target.Value <> ""
- .Item(1).Interior.ColorIndex = 20
- End With
- With xlapp.Target.EntireColumn.FormatConditions
- .Delete
- .Add xlExpression, , Target.Value <> ""
- .Item(1).Interior.ColorIndex = 20
- End With
- End Sub
复制代码 电脑上所用的excel版本是2013,VB6。并且也在VB6中引用了Microsoft Excel 15.0 Object Library,在VB6工程中按F2也有Excel的库。生成.DLL文件后也在VBA中引用并且勾选了,但是就是没有效果。可能是我封装的不对,也还请大神门指教,本人也比较小白,没有太多接触过这方面知识。
|
|