How does auto Highlight all code works?


(Excel 2010) #1

I would like to turn autohighlight for Microsoft Visual Basic for Applications (VBA)

but it doesn’t work as expected.

As I see it only works if code is indented.
But if that’s true why language recognition?

Could it be forced to recognize language?

Is it possible that first post in this thread https://online-excel-solutions.com/t/preformatted-text-vba-testing/358 is automatically preformatted?


(Mittineague) #2

I don’t know if the code highlighting works with indented code blocks. You may need to use three backticks eg. ```vb

Sub test()
Dim x As Long

With ActiveCell
For i = 1 To 1000
x = 91 * i
If Right(x, 2) = "22" Then
ActiveCell.Offset(m).Value = i
m = m + 1
End If
Next i

End With

End Sub

(Excel 2010) #3

I know that.
I meant that this would automatically recognize language and then code it.

Both codes in mine above link are just copy-paste. Without < / >.

In second post it format some parts by itself.


(Mittineague) #4

All </> does is indent whatever is selected four spaces.
If what is pasted in has lines that are indented four spaces they will be seen the same as if they had been selected and then indented. A common gotcha is pasted in code with # comments and underscores that get seen as intentional Markdown syntax when they are not.

I don’t know what the “generic” highlighter highlights other than Strings, Ints, and certain words common to various languages.


(cpradio) #5

IIRC, that setting is there so it will apply code formatting/highlighting to space indented code and fenced code blocks. On Meta, that setting is not enabled.

Space indented

if (true) {
  this_is_formatted = true;
}

Fenced code block

if (true) {
  this_is_formatted = true;
}

Where as on my sandbox with it enabled, I get


(Excel 2010) #6

OK, so it means, when automatically set, it will change code block to fancy if space indented?


(cpradio) #7

Yes. That is correct.