0

I am trying to write a script for Excel and it keeps showing me an error "syntax error" at the line "Return ..." But I don't see any problem with the code. What am I doing wrong?

Function LoadCurrentUnitArray() As String()

    Dim currentUnitArray() As String, unit_count As Integer, i As Integer

   

    unit_count = WorksheetFunction.CountA(Worksheets(1).Columns(2))

    ReDim currentUnitArray(unit_count)

    For i = 1 To unit_count

        currentUnitArray(i) = Cells(i + 1, 2).Value

    Next

    Return currentUnitArray

End Function

enter image description here

BigBen
  • 46,229
  • 7
  • 24
  • 40
user828950
  • 33
  • 1
  • 5
  • 3
    There's no `Return` in VBA: `LoadCurrentUnitArray = currentUnitArray`. – BigBen May 26 '21 at 17:04
  • Side note: use `Long`, not `Integer`. See [this](https://stackoverflow.com/questions/26409117/why-use-integer-instead-of-long) why. – BigBen May 26 '21 at 17:05
  • Thank you! I opened Excel Developer tab for the first time today, didn't know Visual Basic and VBA are different things. And thanks for the data type tip. – user828950 May 26 '21 at 17:17
  • 2
    You can take a screenshot in Windows by pressing `WIN+SHIFT+S` then you can select the area you want to snip. The snip tool puts the image in your clipboard where you can post directly to SO by pressing `CTRL+V`. Much faster than taking a picture with an external device. You will get better image quality too. – HackSlash May 26 '21 at 18:10
  • @HackSlash Thank you for advice. Unfortunetelly I was making the post from phone and coded on different device, where I cannot access stackexchange. I just wanted to show the kind of environment I am coding in in case there was something specific to it. Sorry for the ruined aesthetics. – user828950 May 29 '21 at 09:56
  • @MathieuGuindon Thank you for the editor tip. Will try it next time. – user828950 May 29 '21 at 09:59
  • @MathieuGuindon why is the multiple-declarations Dim statement a problem? – user828950 May 29 '21 at 10:05

0 Answers0