Click here to Skip to main content
15,916,419 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dears,

Please help, how to convert VBA to VB.net 2015

for example I have this code:

Selection.NumberFormat = "[$-2000000]#,#;[Red](#,##0)"

what will be the VB.net Code

I tried many codes on the web but unfortunately not working

Thanks in advance

What I have tried:

Dim myselection as Excel.Range

myselection.NumberFormat = "[$-2000000]#,#;[Red](#,##0)"
Posted
Updated 23-Dec-16 0:38am

This piece of code can't change formattings for MS Excel range (cell/cells), because myselection variable has been declared and never initiated.

VB.NET
Dim myselection as Excel.Range

myselection.NumberFormat = "[$-2000000]#,#;[Red](#,##0)" 


All you have to do is to initiate that variable, for example:
VB.NET
Dim myselection as Excel.Range =  CType(ExcelApp.Worksheets("Sheet1").Range("A17"), Excel.Range)
'where ExcelApp is an object which hold reference to Excel.Application
myselection.NumberFormat = "[$-2000000]#,#;[Red](#,##0)" 



For further details, please see:
Excel Object Model Overview[^]
Understanding the Excel Object Model from a .NET Developer’s Perspective[^]
Working with Ranges[^]
Range.NumberFormat Property (Excel)[^]

Good luck!
 
Share this answer
 
Comments
Mahmoud Algindy 23-Dec-16 5:39am    
Thank you and thanks for all

Actually I tried the below code and it's working for a single selected cell but I need to run the code for the current selected cells means multiple cells

Globals.ThisAddIn.Application.ActiveCell.NumberFormat = ("_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)")
You do it by completely understanding the code you're trying to convert and exactly what it's doing and then write the equivalent code in VB.NET.

Just going through and doing a line-by-line conversion without understanding what the code is doing and the environment it is running in is not going to work.
 
Share this answer
 
Hi, thank so much for everybody

I solved now and this is the code


Dim MySelection As Excel.Range = Globals.ThisAddIn.Application.Selection
MySelection.NumberFormat = "[$-2000000]#,#;[Red](#,##0)"


but the issue now how make it public because currently I should add for each button

Please refer to msdn <a href="https://msdn.microsoft.com/en-us/library/cc442817.aspx">
</a>[<a href="https://msdn.microsoft.com/en-us/library/cc442817.aspx" target="_blank" title="New Window">^</a>]
 
Share this answer
 
v3
For that you'll need to use a MaskedTextBox. This is assuming you're developing a Windows Forms application, you haven't said exactly what UI technology you are using be it WinForms, WPF, WebForms or MVC as they all have a different solution for this issue.

MaskedTextBox Class (System.Windows.Forms)[^]
 
Share this answer
 
Comments
Maciej Los 22-Dec-16 9:19am    
You're wrong. OP is talking about text fromatting in a cell.
F-ES Sitecore 22-Dec-16 9:27am    
OP hasn't said anything about the context of the code he is using. If he is using Office objects then the code is the same as the property is the same. Please feel free to offer an alternative solution :)
Richard Deeming 22-Dec-16 13:14pm    
Dim myselection as Excel.Range

That's a slight clue! :)
Mahmoud Algindy 22-Dec-16 13:30pm    
Sorry, what!
Richard Deeming 22-Dec-16 13:34pm    
I was replying to F-ES Sitecore, who said that your question has no indication that you're using Excel.

I was just pointing out that it does - the quoted line of code from the "What I have tried" section being the clue. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900