0

Someone knows what should i do? it says expected array

dim szamok As String
szamok = Range("f2")
Dim hossz As Integer

ReDim karakterek(1 To Len(szamok)) As Characters

For i = 1 To Len(szamok)
karakterek(i) = szamok(i)
Next i
braX
  • 11,506
  • 5
  • 20
  • 33
josthi
  • 9
  • 3
  • 1
    (1) You don't `Redim` if you haven't dimmed in the first place; (2) I think your declaration should be of type `String` rather than `Characters` if it contains text. – SJR Dec 04 '18 at 17:06
  • I have a int "f2". those are 0 and 1 numbers. i want to make them into a character based array. Sorry for my bad english. – josthi Dec 04 '18 at 17:09
  • Also `szamok(i)` is wrong as `szamok` is a string not an array. So if F2 contains "Fred" do you want an array of 4 elements, "F", "R","E" and "D"? – SJR Dec 04 '18 at 17:10
  • Yes. i Redimed the karakterek array because it doesnt let me dim because of 1 to Len(szamok) – josthi Dec 04 '18 at 17:11
  • So F2 contains a series of 0s and 1s? Could you attach a screenshot? When you initially dim it you don't declare dimensions, just use `Dim karakterek() As String`. – SJR Dec 04 '18 at 17:13
  • `szamok` is not an array, it is a string. To get each character from it, use: `= mid(szamok, i, 1)` intead of `szamok(i)` – cybernetic.nomad Dec 04 '18 at 17:13
  • How can i attach a screenshot? – josthi Dec 04 '18 at 17:15
  • You can upload your image to imgur.com and post a link to it. Someone with enough repuation will be able to embed it for you – cybernetic.nomad Dec 04 '18 at 17:21
  • 1
    @SJR, you don't need to use Dim if you ReDim before use. You just can't use Preserve. –  Dec 04 '18 at 17:23
  • Have a look here: https://stackoverflow.com/a/13196878/78522 – iDevlop Dec 04 '18 at 17:30
  • @user10735198 - I stand corrected. – SJR Dec 04 '18 at 17:30

2 Answers2

1

I believe this is what you need:

Dim szamok As String
Dim hossz As Long
Dim karakterek As Variant

szamok = Range("F2").Value

ReDim karakterek(1 To Len(szamok))

For i = 1 To Len(szamok)
    karakterek(i) = Mid(szamok, i, 1)
Next i

Changes to original code:

  1. Dim karakterek as Variant before Redim

  2. Get characters from szamok using Mid

cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31
  • Thank you very much. Im working on a project where i convert binary to decimal but i cant deal with the fracture binaries so i decided to deal first with the integer and then calculate the fracture part here is my code yet – josthi Dec 04 '18 at 17:25
0

As described here, you can simply use

Dim bytes() as Byte
bytes = StrConv("Xmas", vbFromUnicode)
iDevlop
  • 24,841
  • 11
  • 90
  • 149