1

How would this VB function will look like as C# ? The goal is to determine if a string ref represents a valid datasheet Excel.Range. This isn't a trivial VB to C# conversion as Range can't be constructed.

Private Function IsRange(ref) As Boolean
'   Returns True if ref is a Range
    Dim x As Range
    On Error Resume Next
    Set x = Range(ref)
    If Err = 0 Then IsRange = True Else IsRange = False
End Function

Best

5
  • developerfusion.com/tools Commented Dec 14, 2012 at 10:14
  • :) @Sylca unfortuantely not so easy. Commented Dec 14, 2012 at 12:32
  • Hoped it would help you.:-) Nothing is easy these days my friend. Best:-) Commented Dec 14, 2012 at 14:04
  • @Sylca : nope, it doesnt. As I meant unfortunately it doesnt help. Commented Dec 15, 2012 at 14:06
  • @HeinrichStack are you sure that your example source is VB.NET? Constructing a Range like that doesn't seem to be valid for VB.NET. Perhaps it is VBA? Commented Apr 12, 2018 at 23:37

1 Answer 1

0

I would have liked to find a way to not have to rely on an exception for checking the validity of a cell reference. But I couldn't not find a way to directly test for it.

So instead I am using the OP's approach. The problem is that Microsoft.Office.Interop.Excel.Range is an interface and can't be directly created. Per this answer the solution is to use the get_Range method from _Application or Sheet.

Microsoft.Office.Interop.Excel._Application ExcelApp;

private bool IsRange(string refr) {
   try {
      var unused = ExcelApp.Range[cellReference, Type.Missing];

      return true;
   }
   catch {
      return false;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.