最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - Convert VBA code Interior.Color to VB.NET code - Stack Overflow

programmeradmin3浏览0评论

I want to test the cells of an Excel sheet on Interior.Color. The VBA code works fine:

If cell.Interior.Color = RGB(255, 255, 204) Then

I have problems "translating" this code into VB.NET. I tried a lot of code but nothing works. I have Option Explicit On. I have Option Strict On. This code in VB.NET gives no error but it does not work:

If cell.Interior.Color Is TryCast(RGB(255, 255, 204), Object) Then

Please note in VB.NET it seems to be If cell.Interior.Color Is not If cell.Interior.Color =

Any suggestions? Thanks

I think I have to cast RGB(255, 255, 204) but I don't know how? Casting to Object gives no error but does not work.

Using most recent Visual Studio VB.NET and Add-in-express.

I want to test the cells of an Excel sheet on Interior.Color. The VBA code works fine:

If cell.Interior.Color = RGB(255, 255, 204) Then

I have problems "translating" this code into VB.NET. I tried a lot of code but nothing works. I have Option Explicit On. I have Option Strict On. This code in VB.NET gives no error but it does not work:

If cell.Interior.Color Is TryCast(RGB(255, 255, 204), Object) Then

Please note in VB.NET it seems to be If cell.Interior.Color Is not If cell.Interior.Color =

Any suggestions? Thanks

I think I have to cast RGB(255, 255, 204) but I don't know how? Casting to Object gives no error but does not work.

Using most recent Visual Studio VB.NET and Add-in-express.

Share Improve this question edited Feb 17 at 13:49 Olivier Jacot-Descombes 113k14 gold badges147 silver badges198 bronze badges asked Feb 17 at 7:23 HenriPHenriP 35 bronze badges New contributor HenriP is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

2 Answers 2

Reset to default 0

It does not work because the RGB object created with TryCast and the object returned by excel are two different objects and objects are compared by reference not by their content.

Cast the the object returned by excel instead:

If DirectCast(cell.Interior.Color, Double) = RGB(255, 255, 204) Then

Finally I was able to make Interop work with my Office 365 Pro. The Color value returned by Excel is of type Double, not Integer as I expected first.

The (VB6/VBA) RBG() function returns a Long value.

Fortunately for you the Microsoft.Visual namespace provides a similar RBG() function.

发布评论

评论列表(0)

  1. 暂无评论