1

I would like to get a string up to the newline. For this I am trying to use InStr(text, vbCrLf), but the function returns 0. MsgBox displays the text considering the line jump. Why it returns 0?

' VBA

InStr(text, "/r") ' returns 0
InStr(text, "/n") ' returns 0
InStr(text, vbCrLf) ' returns 0
urdearboy
  • 14,439
  • 5
  • 28
  • 58
Manoel Gumes
  • 11
  • 1
  • 2

1 Answers1

1

Thanks to @Mathieu Guindon for noticing OP was looking for wrong character.

I was looking at your '\n' and jumped to an alternative I like to use which is the Chr code

InStr(text, chr$(10))

Quote:

The ASCII character code 10 is sometimes written as \n and it is sometimes called a New Line or NL. ASCII character 10 is also called a Line Feed or LF

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • `vbCrLf` is `Chr$(10)` & `Chr$(13)` though. OP was just not looking for the right thing – Mathieu Guindon Aug 28 '19 at 15:34
  • @MathieuGuindon oooh while your here.... isn't Chr$(10) newline? So are you saying Chr$(10) & Chr$(13) is equivalent to \n ? – QHarr Aug 28 '19 at 15:36
  • `vbNewLine` is `vbCrLf` (on Windows); `Chr(10)` is `vbLf`, and `Chr(13)` is `vbCr` ;-) – Mathieu Guindon Aug 28 '19 at 15:37
  • @MathieuGuindon so \n is chr$(10) ? - that was what I was focusing on. I completely missed the other part. :-( – QHarr Aug 28 '19 at 15:38
  • 1
    [this](https://stackoverflow.com/a/1761086/1188513) explains it better than I could in a comment - `\r` would be the "carriage return", i.e. `vbCr` so `Chr(13)` – Mathieu Guindon Aug 28 '19 at 15:41
  • 1
    for future ref you can check an [ascii table](https://www.asciitable.xyz/images/ascii-table.png) to see what each `Chr(dec)` represents – Marcucciboy2 Aug 28 '19 at 15:43