ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Populate array (https://www.excelbanter.com/excel-programming/286515-populate-array.html)

Jake Marx[_3_]

Populate array
 
Hi Beto,

You could do something like this:

Sub test()
Dim Names As Variant

Names = Array("N1", "N2", "N3")

Sheets("Sheet1").Range("A1:C1").Value = Names
End Sub

BTW, you should probably use a different variable name than Names, as Names
is the name of a collection of Name objects. In this case, you may want to
use vNames or avNames (v for Variant or av for "array of Variants").

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Beto wrote:
Hi,
Can I populate an array in a similar way than this?

Dim Names(1 to 3) as String
Names=Array("N1","N2","N3")

I like something like that instead of:
Names(1)="N1": Names(2)="N2": Names(3)="N3"

BTW, which one would bethe best way to fill column headings that
aren't in any Workbook, just from VBA?

Regards,



Beto[_3_]

Populate array
 
Hi,
Can I populate an array in a similar way than this?

Dim Names(1 to 3) as String
Names=Array("N1","N2","N3")

I like something like that instead of:
Names(1)="N1": Names(2)="N2": Names(3)="N3"

BTW, which one would bethe best way to fill column headings that aren't
in any Workbook, just from VBA?

Regards,
--
Beto
Reply: Erase between the dot (inclusive) and the @.
Responder: Borra la frase obvia y el punto previo.


Beto[_3_]

Populate array
 
Jake Marx wrote:

Hi Beto,

You could do something like this:

Sub test()
Dim Names As Variant

Names = Array("N1", "N2", "N3")

Sheets("Sheet1").Range("A1:C1").Value = Names
End Sub


Worked beatifully... Is there a way to assign comlumn widths in a
similar way?

As you may find out I'm formatting a sheet for a report. I thought of
using a template sheet, but I'm doing this because is harder and helps
me to learn more VBA.

Regards,
--
Beto
Reply: Erase between the dot (inclusive) and the @.
Responder: Borra la frase obvia y el punto previo.


Bob Phillips[_6_]

Populate array
 
Beto,

Is this what you want

Option Base 1

Sub ColumnWidths()

Dim aryWidths
Dim i As Long

aryWidths = Array(10, 15, 22, 34)

For i = LBound(aryWidths) To UBound(aryWidths)
Columns(i).ColumnWidth = aryWidths(i)
Next i

End Sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Beto" wrote in message
...
Jake Marx wrote:

Hi Beto,

You could do something like this:

Sub test()
Dim Names As Variant

Names = Array("N1", "N2", "N3")

Sheets("Sheet1").Range("A1:C1").Value = Names
End Sub


Worked beatifully... Is there a way to assign comlumn widths in a
similar way?

As you may find out I'm formatting a sheet for a report. I thought of
using a template sheet, but I'm doing this because is harder and helps
me to learn more VBA.

Regards,
--
Beto
Reply: Erase between the dot (inclusive) and the @.
Responder: Borra la frase obvia y el punto previo.




Bob Phillips[_6_]

Populate array
 
Beto,

That works, but I added the bits about lower and upper array bounds as you
said you wanted to learn more VBA <G.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Beto" wrote in message
...
Beto wrote:

Jake Marx wrote:

Hi Beto,

You could do something like this:

Sub test()
Dim Names As Variant

Names = Array("N1", "N2", "N3")

Sheets("Sheet1").Range("A1:C1").Value = Names
End Sub


Worked beatifully... Is there a way to assign comlumn widths in a
similar way?


Nevermind, after a lot of trial & error I came up with this:

Dim vColumnWidths as Variant

vColumnWidths = Array(10, 20, 30)
Range("A1:C1").ColumnWidth = vColumnWidths

Regards,
--
Beto
Reply: Erase between the dot (inclusive) and the @.
Responder: Borra la frase obvia y el punto previo.




Beto[_3_]

Populate array
 
Beto wrote:

Jake Marx wrote:

Hi Beto,

You could do something like this:

Sub test()
Dim Names As Variant

Names = Array("N1", "N2", "N3")

Sheets("Sheet1").Range("A1:C1").Value = Names
End Sub


Worked beatifully... Is there a way to assign comlumn widths in a
similar way?


Nevermind, after a lot of trial & error I came up with this:

Dim vColumnWidths as Variant

vColumnWidths = Array(10, 20, 30)
Range("A1:C1").ColumnWidth = vColumnWidths

Regards,
--
Beto
Reply: Erase between the dot (inclusive) and the @.
Responder: Borra la frase obvia y el punto previo.


Beto[_3_]

Populate array
 
Bob Phillips wrote:

Beto,

That works, but I added the bits about lower and upper array bounds as you
said you wanted to learn more VBA <G.


Thanks, Is always better to know more ways to do the same things.

Regards,
--
Beto
Reply: Erase between the dot (inclusive) and the @.
Responder: Borra la frase obvia y el punto previo.



All times are GMT +1. The time now is 01:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com