Copying Type String Array to Range in Excel - Invalid Qualifier Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do you convert a byte array to a hexadecimal string, and vice versa?Sort array of objects by string property valueHow to check if type of a variable is string?Convert ArrayList<String> to String[] arrayCopy array by valueLoop through an array of strings in Bash?Populating an array with named range on a specific worksheetPick a specific tab for a copy rangeError calling range in Excel table causing crashCompiling error :invalid qualifier when using an array to store not matching data within a loop

“Since the train was delayed for more than an hour, passengers were given a full refund.” – Why is there no article before “passengers”?

Raising a bilingual kid. When should we introduce the majority language?

A journey... into the MIND

Protagonist's race is hidden - should I reveal it?

Why aren't road bike wheels tiny?

How to ask rejected full-time candidates to apply to teach individual courses?

Knights and Knaves question

Can a Wizard take the Magic Initiate feat and select spells from the Wizard list?

How to create a command for the "strange m" symbol in latex?

How to produce a PS1 prompt in bash or ksh93 similar to tcsh

What *exactly* is electrical current, voltage, and resistance?

Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?

Are Flameskulls resistant to magical piercing damage?

What is the ongoing value of the Kanban board to the developers as opposed to management

Married in secret, can marital status in passport be changed at a later date?

/bin/ls sorts differently than just ls

Why aren't these two solutions equivalent? Combinatorics problem

How to break 信じようとしていただけかも知れない into separate parts?

Has a Nobel Peace laureate ever been accused of war crimes?

Why not use the yoke to control yaw, as well as pitch and roll?

What kind of equipment or other technology is necessary to photograph sprites (atmospheric phenomenon)

Recursive calls to a function - why is the address of the parameter passed to it lowering with each call?

Trying to enter the Fox's den

What documents does someone with a long-term visa need to travel to another Schengen country?



Copying Type String Array to Range in Excel - Invalid Qualifier



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do you convert a byte array to a hexadecimal string, and vice versa?Sort array of objects by string property valueHow to check if type of a variable is string?Convert ArrayList<String> to String[] arrayCopy array by valueLoop through an array of strings in Bash?Populating an array with named range on a specific worksheetPick a specific tab for a copy rangeError calling range in Excel table causing crashCompiling error :invalid qualifier when using an array to store not matching data within a loop



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I want to transfer the contents from part of the a 'Type' array to an excel range (see example below), but get 'Invalid Qualifier' error. Any help would be appreciated?



Public Type typDetails
firstName As String
lastName As String
End Type

Dim userDetails(100) As typDetails

Public Sub test()

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

Worksheets(1).Range("A1:A3") = userDetails.firstName

End Sub


Thanks in advance,
Alex.










share|improve this question
























  • Which line? I doubt you can add all firstnames in one go like that.

    – SJR
    Mar 22 at 14:30

















1















I want to transfer the contents from part of the a 'Type' array to an excel range (see example below), but get 'Invalid Qualifier' error. Any help would be appreciated?



Public Type typDetails
firstName As String
lastName As String
End Type

Dim userDetails(100) As typDetails

Public Sub test()

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

Worksheets(1).Range("A1:A3") = userDetails.firstName

End Sub


Thanks in advance,
Alex.










share|improve this question
























  • Which line? I doubt you can add all firstnames in one go like that.

    – SJR
    Mar 22 at 14:30













1












1








1


1






I want to transfer the contents from part of the a 'Type' array to an excel range (see example below), but get 'Invalid Qualifier' error. Any help would be appreciated?



Public Type typDetails
firstName As String
lastName As String
End Type

Dim userDetails(100) As typDetails

Public Sub test()

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

Worksheets(1).Range("A1:A3") = userDetails.firstName

End Sub


Thanks in advance,
Alex.










share|improve this question
















I want to transfer the contents from part of the a 'Type' array to an excel range (see example below), but get 'Invalid Qualifier' error. Any help would be appreciated?



Public Type typDetails
firstName As String
lastName As String
End Type

Dim userDetails(100) As typDetails

Public Sub test()

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

Worksheets(1).Range("A1:A3") = userDetails.firstName

End Sub


Thanks in advance,
Alex.







arrays excel vba types






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 14:28









medicine_man

26113




26113










asked Mar 22 at 14:11









AlexAlex

83




83












  • Which line? I doubt you can add all firstnames in one go like that.

    – SJR
    Mar 22 at 14:30

















  • Which line? I doubt you can add all firstnames in one go like that.

    – SJR
    Mar 22 at 14:30
















Which line? I doubt you can add all firstnames in one go like that.

– SJR
Mar 22 at 14:30





Which line? I doubt you can add all firstnames in one go like that.

– SJR
Mar 22 at 14:30












2 Answers
2






active

oldest

votes


















1














Arrays: 0-based vs 1-based, 1D vs 2D




  • Workbook
    Download (Dropbox)

  • To copy the data in one go you have to put it in an adequate array,
    unfortunately using a loop, but which should be much faster than
    looping through a range.

  • The following are various options how you may achieve this.

  • I have expanded it to include the last name, too.

  • You should probably introduce a long variable for UBound(userDetails).

  • I used Private instead of Public to keep it on module-level.

Module1



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

Private userDetails(2) As typDetails

' Introduce two 0-based arrays (needs Transpose and 'size adjustment').
Sub test1()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob1"
userDetails(0).lastName = "Bobson1"
userDetails(1).firstName = "John1"
userDetails(1).lastName = "Johnson1"
userDetails(2).firstName = "Peter1"
userDetails(2).lastName = "Peterson1"

ReDim vntFirst(UBound(userDetails))
ReDim vntLast(UBound(userDetails))

For i = 0 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst) + 1) = _
Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast) + 1) = _
Application.Transpose(vntLast)
End With

End Sub

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test2()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob2"
userDetails(0).lastName = "Bobson2"
userDetails(1).firstName = "John2"
userDetails(1).lastName = "Johnson2"
userDetails(2).firstName = "Peter2"
userDetails(2).lastName = "Peterson2"

ReDim vntFirst(1 To UBound(userDetails) + 1)
ReDim vntLast(1 To UBound(userDetails) + 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1) = userDetails(i).firstName
vntLast(i + 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub

' Introduce two 2D 1-based arrays.
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob3"
userDetails(0).lastName = "Bobson3"
userDetails(1).firstName = "John3"
userDetails(1).lastName = "Johnson3"
userDetails(2).firstName = "Peter3"
userDetails(2).lastName = "Peterson3"

ReDim vntFirst(1 To UBound(userDetails) + 1, 1 To 1)
ReDim vntLast(1 To UBound(userDetails) + 1, 1 To 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1, 1) = userDetails(i).firstName
vntLast(i + 1, 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub


Module2



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 1D 1-based array.
Private userDetails(1 To 3) As typDetails

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1).firstName = "Bob4"
userDetails(1).lastName = "Bobson4"
userDetails(2).firstName = "John4"
userDetails(2).lastName = "Johnson4"
userDetails(3).firstName = "Peter4"
userDetails(3).lastName = "Peterson4"

ReDim vntFirst(1 To UBound(userDetails))
ReDim vntLast(1 To UBound(userDetails))

For i = 1 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub


Module3



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 2D 1-based array.
Private userDetails(1 To 3, 1 To 1) As typDetails

' Introduce two 2D 1-based arrays.
Sub test5()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1, 1).firstName = "Bob5"
userDetails(1, 1).lastName = "Bobson5"
userDetails(2, 1).firstName = "John5"
userDetails(2, 1).lastName = "Johnson5"
userDetails(3, 1).firstName = "Peter5"
userDetails(3, 1).lastName = "Peterson5"

ReDim vntFirst(1 To UBound(userDetails), 1 To 1)
ReDim vntLast(1 To UBound(userDetails), 1 To 1)

For i = 1 To UBound(userDetails)
vntFirst(i, 1) = userDetails(i, 1).firstName
vntLast(i, 1) = userDetails(i, 1).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub





share|improve this answer























  • Perfect! Thanks for the quick reply.

    – Alex
    Mar 22 at 19:06


















2














Think you need a loop:



Public Sub test()

Dim i As Long

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

For i = 0 To 2
Worksheets(1).Cells(1, i + 1).Value = userDetails(i).firstName
Next i

End Sub





share|improve this answer























  • Data set is a lot bigger than this, and a loop would be too slow. Thanks for the reply.

    – Alex
    Mar 22 at 19:05











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55301483%2fcopying-type-string-array-to-range-in-excel-invalid-qualifier%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Arrays: 0-based vs 1-based, 1D vs 2D




  • Workbook
    Download (Dropbox)

  • To copy the data in one go you have to put it in an adequate array,
    unfortunately using a loop, but which should be much faster than
    looping through a range.

  • The following are various options how you may achieve this.

  • I have expanded it to include the last name, too.

  • You should probably introduce a long variable for UBound(userDetails).

  • I used Private instead of Public to keep it on module-level.

Module1



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

Private userDetails(2) As typDetails

' Introduce two 0-based arrays (needs Transpose and 'size adjustment').
Sub test1()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob1"
userDetails(0).lastName = "Bobson1"
userDetails(1).firstName = "John1"
userDetails(1).lastName = "Johnson1"
userDetails(2).firstName = "Peter1"
userDetails(2).lastName = "Peterson1"

ReDim vntFirst(UBound(userDetails))
ReDim vntLast(UBound(userDetails))

For i = 0 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst) + 1) = _
Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast) + 1) = _
Application.Transpose(vntLast)
End With

End Sub

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test2()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob2"
userDetails(0).lastName = "Bobson2"
userDetails(1).firstName = "John2"
userDetails(1).lastName = "Johnson2"
userDetails(2).firstName = "Peter2"
userDetails(2).lastName = "Peterson2"

ReDim vntFirst(1 To UBound(userDetails) + 1)
ReDim vntLast(1 To UBound(userDetails) + 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1) = userDetails(i).firstName
vntLast(i + 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub

' Introduce two 2D 1-based arrays.
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob3"
userDetails(0).lastName = "Bobson3"
userDetails(1).firstName = "John3"
userDetails(1).lastName = "Johnson3"
userDetails(2).firstName = "Peter3"
userDetails(2).lastName = "Peterson3"

ReDim vntFirst(1 To UBound(userDetails) + 1, 1 To 1)
ReDim vntLast(1 To UBound(userDetails) + 1, 1 To 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1, 1) = userDetails(i).firstName
vntLast(i + 1, 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub


Module2



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 1D 1-based array.
Private userDetails(1 To 3) As typDetails

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1).firstName = "Bob4"
userDetails(1).lastName = "Bobson4"
userDetails(2).firstName = "John4"
userDetails(2).lastName = "Johnson4"
userDetails(3).firstName = "Peter4"
userDetails(3).lastName = "Peterson4"

ReDim vntFirst(1 To UBound(userDetails))
ReDim vntLast(1 To UBound(userDetails))

For i = 1 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub


Module3



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 2D 1-based array.
Private userDetails(1 To 3, 1 To 1) As typDetails

' Introduce two 2D 1-based arrays.
Sub test5()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1, 1).firstName = "Bob5"
userDetails(1, 1).lastName = "Bobson5"
userDetails(2, 1).firstName = "John5"
userDetails(2, 1).lastName = "Johnson5"
userDetails(3, 1).firstName = "Peter5"
userDetails(3, 1).lastName = "Peterson5"

ReDim vntFirst(1 To UBound(userDetails), 1 To 1)
ReDim vntLast(1 To UBound(userDetails), 1 To 1)

For i = 1 To UBound(userDetails)
vntFirst(i, 1) = userDetails(i, 1).firstName
vntLast(i, 1) = userDetails(i, 1).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub





share|improve this answer























  • Perfect! Thanks for the quick reply.

    – Alex
    Mar 22 at 19:06















1














Arrays: 0-based vs 1-based, 1D vs 2D




  • Workbook
    Download (Dropbox)

  • To copy the data in one go you have to put it in an adequate array,
    unfortunately using a loop, but which should be much faster than
    looping through a range.

  • The following are various options how you may achieve this.

  • I have expanded it to include the last name, too.

  • You should probably introduce a long variable for UBound(userDetails).

  • I used Private instead of Public to keep it on module-level.

Module1



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

Private userDetails(2) As typDetails

' Introduce two 0-based arrays (needs Transpose and 'size adjustment').
Sub test1()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob1"
userDetails(0).lastName = "Bobson1"
userDetails(1).firstName = "John1"
userDetails(1).lastName = "Johnson1"
userDetails(2).firstName = "Peter1"
userDetails(2).lastName = "Peterson1"

ReDim vntFirst(UBound(userDetails))
ReDim vntLast(UBound(userDetails))

For i = 0 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst) + 1) = _
Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast) + 1) = _
Application.Transpose(vntLast)
End With

End Sub

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test2()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob2"
userDetails(0).lastName = "Bobson2"
userDetails(1).firstName = "John2"
userDetails(1).lastName = "Johnson2"
userDetails(2).firstName = "Peter2"
userDetails(2).lastName = "Peterson2"

ReDim vntFirst(1 To UBound(userDetails) + 1)
ReDim vntLast(1 To UBound(userDetails) + 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1) = userDetails(i).firstName
vntLast(i + 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub

' Introduce two 2D 1-based arrays.
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob3"
userDetails(0).lastName = "Bobson3"
userDetails(1).firstName = "John3"
userDetails(1).lastName = "Johnson3"
userDetails(2).firstName = "Peter3"
userDetails(2).lastName = "Peterson3"

ReDim vntFirst(1 To UBound(userDetails) + 1, 1 To 1)
ReDim vntLast(1 To UBound(userDetails) + 1, 1 To 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1, 1) = userDetails(i).firstName
vntLast(i + 1, 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub


Module2



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 1D 1-based array.
Private userDetails(1 To 3) As typDetails

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1).firstName = "Bob4"
userDetails(1).lastName = "Bobson4"
userDetails(2).firstName = "John4"
userDetails(2).lastName = "Johnson4"
userDetails(3).firstName = "Peter4"
userDetails(3).lastName = "Peterson4"

ReDim vntFirst(1 To UBound(userDetails))
ReDim vntLast(1 To UBound(userDetails))

For i = 1 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub


Module3



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 2D 1-based array.
Private userDetails(1 To 3, 1 To 1) As typDetails

' Introduce two 2D 1-based arrays.
Sub test5()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1, 1).firstName = "Bob5"
userDetails(1, 1).lastName = "Bobson5"
userDetails(2, 1).firstName = "John5"
userDetails(2, 1).lastName = "Johnson5"
userDetails(3, 1).firstName = "Peter5"
userDetails(3, 1).lastName = "Peterson5"

ReDim vntFirst(1 To UBound(userDetails), 1 To 1)
ReDim vntLast(1 To UBound(userDetails), 1 To 1)

For i = 1 To UBound(userDetails)
vntFirst(i, 1) = userDetails(i, 1).firstName
vntLast(i, 1) = userDetails(i, 1).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub





share|improve this answer























  • Perfect! Thanks for the quick reply.

    – Alex
    Mar 22 at 19:06













1












1








1







Arrays: 0-based vs 1-based, 1D vs 2D




  • Workbook
    Download (Dropbox)

  • To copy the data in one go you have to put it in an adequate array,
    unfortunately using a loop, but which should be much faster than
    looping through a range.

  • The following are various options how you may achieve this.

  • I have expanded it to include the last name, too.

  • You should probably introduce a long variable for UBound(userDetails).

  • I used Private instead of Public to keep it on module-level.

Module1



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

Private userDetails(2) As typDetails

' Introduce two 0-based arrays (needs Transpose and 'size adjustment').
Sub test1()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob1"
userDetails(0).lastName = "Bobson1"
userDetails(1).firstName = "John1"
userDetails(1).lastName = "Johnson1"
userDetails(2).firstName = "Peter1"
userDetails(2).lastName = "Peterson1"

ReDim vntFirst(UBound(userDetails))
ReDim vntLast(UBound(userDetails))

For i = 0 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst) + 1) = _
Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast) + 1) = _
Application.Transpose(vntLast)
End With

End Sub

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test2()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob2"
userDetails(0).lastName = "Bobson2"
userDetails(1).firstName = "John2"
userDetails(1).lastName = "Johnson2"
userDetails(2).firstName = "Peter2"
userDetails(2).lastName = "Peterson2"

ReDim vntFirst(1 To UBound(userDetails) + 1)
ReDim vntLast(1 To UBound(userDetails) + 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1) = userDetails(i).firstName
vntLast(i + 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub

' Introduce two 2D 1-based arrays.
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob3"
userDetails(0).lastName = "Bobson3"
userDetails(1).firstName = "John3"
userDetails(1).lastName = "Johnson3"
userDetails(2).firstName = "Peter3"
userDetails(2).lastName = "Peterson3"

ReDim vntFirst(1 To UBound(userDetails) + 1, 1 To 1)
ReDim vntLast(1 To UBound(userDetails) + 1, 1 To 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1, 1) = userDetails(i).firstName
vntLast(i + 1, 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub


Module2



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 1D 1-based array.
Private userDetails(1 To 3) As typDetails

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1).firstName = "Bob4"
userDetails(1).lastName = "Bobson4"
userDetails(2).firstName = "John4"
userDetails(2).lastName = "Johnson4"
userDetails(3).firstName = "Peter4"
userDetails(3).lastName = "Peterson4"

ReDim vntFirst(1 To UBound(userDetails))
ReDim vntLast(1 To UBound(userDetails))

For i = 1 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub


Module3



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 2D 1-based array.
Private userDetails(1 To 3, 1 To 1) As typDetails

' Introduce two 2D 1-based arrays.
Sub test5()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1, 1).firstName = "Bob5"
userDetails(1, 1).lastName = "Bobson5"
userDetails(2, 1).firstName = "John5"
userDetails(2, 1).lastName = "Johnson5"
userDetails(3, 1).firstName = "Peter5"
userDetails(3, 1).lastName = "Peterson5"

ReDim vntFirst(1 To UBound(userDetails), 1 To 1)
ReDim vntLast(1 To UBound(userDetails), 1 To 1)

For i = 1 To UBound(userDetails)
vntFirst(i, 1) = userDetails(i, 1).firstName
vntLast(i, 1) = userDetails(i, 1).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub





share|improve this answer













Arrays: 0-based vs 1-based, 1D vs 2D




  • Workbook
    Download (Dropbox)

  • To copy the data in one go you have to put it in an adequate array,
    unfortunately using a loop, but which should be much faster than
    looping through a range.

  • The following are various options how you may achieve this.

  • I have expanded it to include the last name, too.

  • You should probably introduce a long variable for UBound(userDetails).

  • I used Private instead of Public to keep it on module-level.

Module1



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

Private userDetails(2) As typDetails

' Introduce two 0-based arrays (needs Transpose and 'size adjustment').
Sub test1()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob1"
userDetails(0).lastName = "Bobson1"
userDetails(1).firstName = "John1"
userDetails(1).lastName = "Johnson1"
userDetails(2).firstName = "Peter1"
userDetails(2).lastName = "Peterson1"

ReDim vntFirst(UBound(userDetails))
ReDim vntLast(UBound(userDetails))

For i = 0 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst) + 1) = _
Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast) + 1) = _
Application.Transpose(vntLast)
End With

End Sub

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test2()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob2"
userDetails(0).lastName = "Bobson2"
userDetails(1).firstName = "John2"
userDetails(1).lastName = "Johnson2"
userDetails(2).firstName = "Peter2"
userDetails(2).lastName = "Peterson2"

ReDim vntFirst(1 To UBound(userDetails) + 1)
ReDim vntLast(1 To UBound(userDetails) + 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1) = userDetails(i).firstName
vntLast(i + 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub

' Introduce two 2D 1-based arrays.
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

userDetails(0).firstName = "Bob3"
userDetails(0).lastName = "Bobson3"
userDetails(1).firstName = "John3"
userDetails(1).lastName = "Johnson3"
userDetails(2).firstName = "Peter3"
userDetails(2).lastName = "Peterson3"

ReDim vntFirst(1 To UBound(userDetails) + 1, 1 To 1)
ReDim vntLast(1 To UBound(userDetails) + 1, 1 To 1)

For i = 0 To UBound(userDetails)
vntFirst(i + 1, 1) = userDetails(i).firstName
vntLast(i + 1, 1) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub


Module2



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 1D 1-based array.
Private userDetails(1 To 3) As typDetails

' Introduce two 1D 1-based arrays (needs Transpose).
Sub test3()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1).firstName = "Bob4"
userDetails(1).lastName = "Bobson4"
userDetails(2).firstName = "John4"
userDetails(2).lastName = "Johnson4"
userDetails(3).firstName = "Peter4"
userDetails(3).lastName = "Peterson4"

ReDim vntFirst(1 To UBound(userDetails))
ReDim vntLast(1 To UBound(userDetails))

For i = 1 To UBound(userDetails)
vntFirst(i) = userDetails(i).firstName
vntLast(i) = userDetails(i).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = Application.Transpose(vntFirst)
.Range("B1").Resize(UBound(vntLast)) = Application.Transpose(vntLast)
End With

End Sub


Module3



Option Explicit

Private Type typDetails
firstName As String
lastName As String
End Type

' Declare as 2D 1-based array.
Private userDetails(1 To 3, 1 To 1) As typDetails

' Introduce two 2D 1-based arrays.
Sub test5()

Dim i As Long
Dim vntFirst As Variant
Dim vntLast As Variant

Erase userDetails
userDetails(1, 1).firstName = "Bob5"
userDetails(1, 1).lastName = "Bobson5"
userDetails(2, 1).firstName = "John5"
userDetails(2, 1).lastName = "Johnson5"
userDetails(3, 1).firstName = "Peter5"
userDetails(3, 1).lastName = "Peterson5"

ReDim vntFirst(1 To UBound(userDetails), 1 To 1)
ReDim vntLast(1 To UBound(userDetails), 1 To 1)

For i = 1 To UBound(userDetails)
vntFirst(i, 1) = userDetails(i, 1).firstName
vntLast(i, 1) = userDetails(i, 1).lastName
Next

With Worksheets(1)
.Range("A1").Resize(UBound(vntFirst)) = vntFirst
.Range("B1").Resize(UBound(vntLast)) = vntLast
End With

End Sub






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 16:28









VBasic2008VBasic2008

3,5372517




3,5372517












  • Perfect! Thanks for the quick reply.

    – Alex
    Mar 22 at 19:06

















  • Perfect! Thanks for the quick reply.

    – Alex
    Mar 22 at 19:06
















Perfect! Thanks for the quick reply.

– Alex
Mar 22 at 19:06





Perfect! Thanks for the quick reply.

– Alex
Mar 22 at 19:06













2














Think you need a loop:



Public Sub test()

Dim i As Long

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

For i = 0 To 2
Worksheets(1).Cells(1, i + 1).Value = userDetails(i).firstName
Next i

End Sub





share|improve this answer























  • Data set is a lot bigger than this, and a loop would be too slow. Thanks for the reply.

    – Alex
    Mar 22 at 19:05















2














Think you need a loop:



Public Sub test()

Dim i As Long

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

For i = 0 To 2
Worksheets(1).Cells(1, i + 1).Value = userDetails(i).firstName
Next i

End Sub





share|improve this answer























  • Data set is a lot bigger than this, and a loop would be too slow. Thanks for the reply.

    – Alex
    Mar 22 at 19:05













2












2








2







Think you need a loop:



Public Sub test()

Dim i As Long

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

For i = 0 To 2
Worksheets(1).Cells(1, i + 1).Value = userDetails(i).firstName
Next i

End Sub





share|improve this answer













Think you need a loop:



Public Sub test()

Dim i As Long

userDetails(0).firstName = "Bob"
userDetails(0).lastName = "Bobson"
userDetails(1).firstName = "Bob"
userDetails(1).lastName = "Bobson"
userDetails(2).firstName = "Bob"
userDetails(2).lastName = "Bobson"

For i = 0 To 2
Worksheets(1).Cells(1, i + 1).Value = userDetails(i).firstName
Next i

End Sub






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 14:32









SJRSJR

13.8k31219




13.8k31219












  • Data set is a lot bigger than this, and a loop would be too slow. Thanks for the reply.

    – Alex
    Mar 22 at 19:05

















  • Data set is a lot bigger than this, and a loop would be too slow. Thanks for the reply.

    – Alex
    Mar 22 at 19:05
















Data set is a lot bigger than this, and a loop would be too slow. Thanks for the reply.

– Alex
Mar 22 at 19:05





Data set is a lot bigger than this, and a loop would be too slow. Thanks for the reply.

– Alex
Mar 22 at 19:05

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55301483%2fcopying-type-string-array-to-range-in-excel-invalid-qualifier%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript