+ Reply to Thread
Results 1 to 8 of 8

Write text to multiple cells using variants

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Write text to multiple cells using variants

    Hey there,

    I have created a sheet which gets populated by information but have left out the top row. I want to use the tope row to enter my header fields but was looking to see if there a more effcicent way of doing this.

    I am using the following code below but was wondering if there was a way using arrays as variants or ranges to do it more efficiently.

    With wsNew
        wsNew.Activate
        .Range("A1").Value = "Compt. Code"
        .Range("A2").Value = "Test 2"
        .Range("A3").Value = "Test 3"
        .Range("A4").Value = "Test 4"
        .Cells.Select
        Selection.Columns.AutoFit
        Selection.Rows.AutoFit
    End With
    cheers for your help.

    Jag
    Last edited by therealjag; 11-18-2009 at 10:56 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,535

    Re: Write text to multiple cells using variants

    A few approaches, one might be:

    Dim vHeaders As Variant
    vHeaders = Array("Compt. Code", "Test 2", "Test 3", "Test 4")
    With wsNew
        With .Range("A1").Resize(UBound(vHeaders)+1)
            .Value = Application.Transpose(vHeaders)
            .Columns.AutoFit
            .Rows.AutoFit
        End With
    End With
    EDIT: I revised the above to use a resize thereby making the code more flexible, ie header range will expand contract based on number of items in Array.
    Last edited by DonkeyOte; 11-18-2009 at 08:35 AM.

  3. #3
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Write text to multiple cells using variants

    Hello.

    To me that looks pritty good. VBA does not require selection very often
    With wsNew
        .Range("A1").Value = "Compt. Code"
        .Range("A2").Value = "Test 2"
        .Range("A3").Value = "Test 3"
        .Range("A4").Value = "Test 4"
        .Columns.AutoFit
        .Rows.AutoFit
    End With


  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,136

    Re: Write text to multiple cells using variants

    I thought the headers were supposed to be in the top row, not first column?
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Re: Write text to multiple cells using variants

    Hey sorry yeah was meant to say A1, B1...so forth, was just in a rush writing it as i was going off to lunch!

  6. #6
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Re: Write text to multiple cells using variants

    Hey yeah actually when I noticed that I cant figure out how to paste the variant in normally without tranposing it I tried to figure it out myself. I thought it would be an easy job but cant find the function for it!

    Does anyone know how to paste in the data without transposing it?

    Cheers

    Jag

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,136

    Re: Write text to multiple cells using variants

    Dim vHeaders As Variant
    vHeaders = Array("Compt. Code", "Test 2", "Test 3", "Test 4")
    With wsNew
        With .Range("A1").Resize(, UBound(vHeaders)+1)
            .Value = vHeaders
            .Columns.AutoFit
            .Rows.AutoFit
        End With
    End With

  8. #8
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Re: Write text to multiple cells using variants

    Lol damn I knew it would have been easy but never knew it was that easy!..cheers for the wisdom romperstomper and cheers to DonkyOte for the original solution

    Jag

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1