Array and string

sapka

Board Regular
Joined
Nov 9, 2009
Messages
110
Hello,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
here is the code:<o:p></o:p>
Code:
[COLOR=black][FONT=Verdana]Sub start()[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]Dim i As Integer[/COLOR][/FONT]
 
[FONT=Verdana][COLOR=black]i = 5[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Range("A1:C1") = Array(CStr(i & "."), 111, "text")<o:p></o:p>[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]End Sub<o:p></o:p>[/FONT][/COLOR]
<o:p></o:p>
For some reasons Excel reads the first element of array as number rahter than string and for that reason I lose the dot. How to fix it (I want to use Array function)?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Excel is doing the conversion to a number for you because the cells are formatted as General. Try changing A1:A5's Cell Format to Text or let VB do it as part of your code...

Code:
Sub start()
  Dim i As Integer
  [COLOR=darkred]Range("A1:C1").NumberFormat = "@"[/COLOR]
  i = 5
  Range("A1:C1") = Array(i & ".", 111, "text")
End Sub

EDIT NOTE: Removed the CStr function call as per Wigi's posting.
 
Last edited:
Upvote 0
Code:
Sub start()
    
    Dim i As Integer
    i = 5
    Range("A1:C1") = Array(i & ".", 111, "text")

End Sub

The operator & takes care of converting i to a String. You do not need an explicit CStr function around it.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top