Exception when leaving the while condition

Hello everyone,

I created a code in VBnet to insert data into an excel column, then first it validates if in the left column there is some data, if yes it should enter data, if it does not exit the condition while

VbNet code:

Dim num As Int32 = 2
Dim HValue As String = xlWorksheet.Range(“H”+num.ToString).Value.ToString

  While  HValue  <> ""
	  xlWorksheet.Range("J"+num.ToString).Value = "=Sum(H"+num.ToString +"/L"+num.ToString+")"
	  num = num + 1
	  HValue = xlWorksheet.Range("H"+num.ToString).Value.ToString
  End While

this is the result:

image

the codes within while work, but when the result of while is failed it returns the following error to me:

Invoke code: Exception has been thrown by the target of an invocation.

Hey @rafael.murakami! I don’t know if, due to the complexity of the problem, the best approach would be an “Invoke Code” with VBA. Could you explain to me what you are looking for to think of something? If the only viable solution for you is via VBA, let me know.

Thank you!!

1 Like