How to copy an array to another array

I need to copy the content of a 2D array to another identical 2D array. I 
currently do it this way...

for X=1 to 1000
   for Y = 1 to 1000
      A(X,Y) = B(X,Y)
   next Y
next X

Is there a better/quicker way to do it?
-- 
Stephenc
0
Utf
4/7/2010 4:14:01 PM
word.vba.general 1023 articles. 0 followers. Follow

5 Replies
8478 Views

Similar Articles

[PageSpeed] 26

stephenc wrote:
> I need to copy the content of a 2D array to another identical 2D array. I 
> currently do it this way...
>
> for X=1 to 1000
>    for Y = 1 to 1000
>       A(X,Y) = B(X,Y)
>    next Y
> next X
>
> Is there a better/quicker way to do it?

There are quicker ways, yes.  But I'd be hard pressed to say they're 
"better" unless you're optimizing a *serious* bottleneck.

I have a SafeArray class on my site that you can use to represent the 
SAFEARRAY structure of any native array.  You'd have to download that 
(from http://vb.mvps.org/samples/MapFile), and use code something like 
this (indented to highlight wordwrap):

   Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" 
(Destination As Any, Source As Any, ByVal Length As Long)

   Public Sub test()
      Dim a() As Long
      Dim b() As Long
      Dim i As Long

      Dim saA As CSafeArray
      Set saA = New CSafeArray

      Dim saB As CSafeArray
      Set saB = New CSafeArray

      ReDim a(1 To 100)
      For i = LBound(a) To UBound(a)
         a(i) = i
      Next i

      With saA
         .AttachToArray a
         ReDim b(.LowerBound To .LowerBound + .CountOfElements)
         saB.AttachToArray b
         Call CopyMemory(ByVal saB.PointerToData, _
                         ByVal .PointerToData, _
                         .SizeofElements * .CountOfElements)
      End With

      saA.ReleaseArray
      saB.ReleaseArray
   End Sub

Also, in CSafeArray, you'd want to redeclare the VarPtrArray function, 
so that it works in VBA.  This seems to do that, when I test here:

   Private Declare Function VarPtrArray Lib "vbe6.dll" Alias "VarPtr" 
(Var() As Any) As Long

Obviously, this is an *incredible* amount of work to go to, unless 
you're saving some serious cycles in the process.  Which is totally 
possible!  But if all you're trying to save is a bit of typing, well, 
you'll want to reconsider more than a few things.

Hope this helps...   Karl

-- 
..NET: It's About Trust!
http://vfred.mvps.org


0
Karl
4/7/2010 8:22:58 PM
Karl E. Peterson wrote:
> I have a SafeArray class on my site that you can use to represent the 
> SAFEARRAY structure of any native array.  

More background on that class:

Black Belt Programming, July 2000: Modify a Variable's Pointer
http://vb.mvps.org/articles/bb200007.asp

-- 
..NET: It's About Trust!
http://vfred.mvps.org


0
Karl
4/7/2010 8:35:04 PM
A = B

-- 
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"stephenc" <nospam> wrote in message 
news:6FB2F8DC-980B-491D-A72E-75CD517BD59A@microsoft.com...
> I need to copy the content of a 2D array to another identical 2D array. I
> currently do it this way...
>
> for X=1 to 1000
>   for Y = 1 to 1000
>      A(X,Y) = B(X,Y)
>   next Y
> next X
>
> Is there a better/quicker way to do it?
> -- 
> Stephenc 

0
Doug
4/8/2010 1:42:54 AM
Doug Robbins - Word MVP wrote:
> A = B

Well sure, that'll work in ~some~ cases...  <g>

-- 
..NET: It's About Trust!
http://vfred.mvps.org


0
Karl
4/8/2010 1:58:46 AM
Thanks Karl and Doug,
Sorry I didn't reply sooner but I thought I'd posted the question to the 
Excel programming forum, and that the posting had failed. So, it was here in 
the Word programming forum all the time. Doh!
Your replies answered my question exactly, so I've learned something useful 
today. Thanks again for your help!
-- 
Stephenc


"Karl E. Peterson" wrote:

> Karl E. Peterson wrote:
> > I have a SafeArray class on my site that you can use to represent the 
> > SAFEARRAY structure of any native array.  
> 
> More background on that class:
> 
> Black Belt Programming, July 2000: Modify a Variable's Pointer
> http://vb.mvps.org/articles/bb200007.asp
> 
> -- 
> ..NET: It's About Trust!
> http://vfred.mvps.org
> 
> 
> .
> 
0
Utf
4/26/2010 10:11:01 AM
Reply:

Similar Artilces:

GAL and another Address Book
I've taken the address book from WinFax and exported into a .csv file. I've imported this into a .pst profile. Now I'd like to have this "address book" of thousands of Fax Numbers available through the Exchange Server (Address Book) so users can access it easily through the Create a Fax Wizard in SBS 03, instead of sharing out one workstations book and going down that road. Is this possible and if so, is there any documentation I haven't found to do it? Thanks in advance. Joseph. Copy the items from the .pst folder into a contacts folder under Public = Fold...

DVD Copy Pro for Mac
[b]DVD Copy Pro for Mac[/b] is a DVD copy software that can support D9 to D5 compression. With simple operation, it can save DVD in four different ways including directly burning to DVD disc, saving as DVD folder, copying into DVD media format that can be played by DVD Player directly, and converting as ISO file for easy movie management. Now this cool Mac DVD Copy also supports the latest released Snow Leopard. You can download [b][url=http://www.dvd-copy-mac.net/download/dvd-copy-pro-mac.zip]DVD Copy Pro for Mac[/url][/b] freely right now. [b]More information:[/b][quote]http://www.dvd-...

Import/copy calendar in Outlook 2003
I got a new computer and installed Office 2003, and wanted to move my calendar to the new PC. I created a new data file called Personal Folders from Old PC. I wanted to copy my calendar OVER my current one in Personal Folders, but don't see a way to do so. I can copy my old calendar into the Pesonal Folders, but can't delete the new, empty Calendar. So I have Calendar, and Calendar 1 under my Personal Folders. Any way to just import/copy my reminders/appointments into my new calendar? Thanks for any advice. Ck G'Day Cathy, 1. Use the Folder List 2. Click on the OLD Cal...

Copy info from one workbook to another?
I have a problem I have a workbook with the function i want like a discribe here. The problem is i want to split the workbook but i don`t know how. Is it possible to have a main workbook with information from other workbooks at each worksheet? I have 20 workbooks with information. I would like to have a main workbook with on sheet1 every row from the 20workbooks with category1, on sheet2 every row from the 20workbooks with category2, etc, etc. The information in the 20workbooks in always only on sheet. The Colum in which the data is has no fix end. I go to fill in the rows trough a...

local copy of mail
How do you store a copy of all your mail on you local computer when using Exchange? You setup an OST/Offline Folders file. Check with your mail admin if they support that configuration. "Brandon Baker" <brandon@discussions.microsoft.com> wrote in message news:epNKuJ19FHA.4004@TK2MSFTNGP14.phx.gbl... > How do you store a copy of all your mail on you local computer when using > Exchange? > ...

Secondary Copy
Hi, My customer using Great Plains modified report(Primary Copy). Now he wants new report something similar to the modified GP report with very few changes. I tried to copy the modified report and planned to use us a secondary copy. But the report has temp tables, so I am unable to make secondary copy. Can some one help me in how to make a new report something similar to the modified report, where to launch it. Whether it is possible throught VBA & Modifier? Regards Selvakumar.S A report with a temporary table can only be a primary copy. This is because only a primary copy can b...

Outlook 2000 SP3
How can I copy my mail and calendar to CD so that I can take my received and sent mail history to a new computer with Outlook installed? Thanks. >-----Original Message----- >How can I copy my mail and calendar to CD so that I can >take my received and sent mail history to a new computer >with Outlook installed? Thanks. >. > All your data saves in C:\Documents and Settings\Username\Local Settings\Application Data\Microsoft\Outlook\archive.pst and C:\Documents and Settings\Evgeni\Local Settings\Application Data\Microsoft\Outlook\outlook.pst You can either install instan...

Copy picture ossice wizard
I have project 2007 standard edition, but the button is not working at all? Any suggestions Hi, The wizard is IMHO useless. Go to Roport, Copy Picture and do without the Wizard what millions have done before the Wizard existed. HTH -- Jan De Messemaeker Microsoft Project Most Valuable Professional +32 495 300 620 For availability check: http://users.online.be/prom-ade/Calendar.pdf "Enrique" <Enrique@discussions.microsoft.com> wrote in message news:72B5AA6C-67AE-468D-B6AE-FF16F52E57BD@microsoft.com... >I have project 2007 standard edition, but the bu...

Copy and paste versus copy and insert copied cells
What is the difference between these commands? Try them, it is pretty easy to see the difference -- Regards, Peo Sjoblom "Alana" <Alana@discussions.microsoft.com> wrote in message news:879FED29-5541-41B0-BB2C-7108D42ED593@microsoft.com... > What is the difference between these commands? ...

subtotal copy and paste
Hi I have used the subtotal option in Excel and this is fine. I have also collasped the list so only the subtotal for each category is visible. However I want to copy and paste this list (just the subtotal) into another sheet When I do this it expands and shows all the results. Is it possible to just copy the visible bit. I have looked in paste special but no luck Cheers Hager On Jan 21, 6:38=A0am, Hager <Ha...@discussions.microsoft.com> wrote: > Hi > > I have used the subtotal option in Excel and this is fine. I have also > collasped the list so only the...

Copy above fails one way, copy below fails another way
I have a table of blood pressure readings that looks something like this: A B C D E 11 12 Averages: 134 83 74 13 R# Date Sys Dia Pul 14 4 8/17/08 138 87 72 15 3 8/15/08 130 84 76 16 2 8/12/08 137 83 70 17 1 8/11/08 129 79 76 Cell A14 contains =A15+1 Cell C12 contains =AVERAGE(C14:C17) Cell D12 contains =AVERAGE(D14:D17) Cell E12 contains =AVERAGE(E14:E17) Every time I take a new reading, I want to add a row to the top of the table, between row 13 (the header) and row 14 (the previous top row). I want to push a...

Copying graphs
Hi Is it possible to range value a graph ie I would like to copy the graph to another workbook, show all the information but without the links? Thanks You could copy the chart as a picture, then paste it wherever you want. Select the chart, hold Shift while clicking on the Edit menu, Choose Copy Picture, and select the On Screen and As Picture options, then go to the other workbook/sheet and Paste. - Jon ------- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http://PeltierTech.com _______ "Dixie" <Dixie@discussions.micr...

& combine two array statements
I have {{=COUNTIF(E5:CZ5,"<"&E$1:CZ$1)}&" \ "{=COUNTIF(E5:CZ5,"<"&E$1:CZ$1)}}, trying to combine two ARRAY statements in the same cell. I could use two columns or two helper columns it just seems neater to get it all sorted out in one cell. The two array statements work fine in isolation. Suggestions appreciated. Many Thanks Paul Moles I dont think I have understood your formula. To combine two array formulas try the below.. =formula1 & " \ " & formula2 = sign only once followed by ampersands to combin...

Business Portal "Copy to Excel" not working in Query pages
Hi, I am using Business Portal 4.0. When browsing through one of the queries pages in Business Portal, i clicked "Copy to Excel" icon. I got an error saying "The Office Web Components (OWC) must be installed to copy results to Excel." at the bottom. I am using Office 2007 on my machine. I came to know that Microsoft Office Web Components will no longer be shipping in Microsoft Office. I tried installing Office 2003 Add-in: Office Web Components from http://www.microsoft.com/downloads/details.aspx?familyid=7287252C-402E-4F72-97A5-E0FD290D4B76&displaylang=en a...

How can I assign a data for one cell from another fixed cell?
I can assign data to one cell by put formular, for example: =H5. But if I add a row, my cell will get data from H6, Excel automatically modified my formular to: =H6 And now I want the data always come from =H5, no matter how many rows I add on top of it. PLease help me. I remember somebody did it, but I forget it. Thank you very much! If I understand your question correctly, you would use absolute references ($ signs to fix the row, column or both)... =$H$5 Rick "new Excel user" <new Excel user@discussions.microsoft.com> wrote in message news:BCAE5E50-1EAF-4ADA-9F24...

Copying Views
Is there any way to copy the views under leads so that i can modify them and create different ones. I need to create 15 views and i do not want to do all of them one at a time. Even if there is a way to do it in sql i would love to know -- IT Manager It can be done in SQL, though it's unsupported. What you want need to do is use INSERT ... SELECT to copy rows in the savedquerybase, and give a different savedqueryid For a supported route you can use the CRM web service and create new instances of savedquery. The following article describes how to do this for the userquery, and coul...

Having a go with assembly... how do I copy a double into a register??
I'm trying to speed up a maths routine which consists of much iterative multiplication (and adding and subtraction of ) doubles. Now if I declare my doubles as floats float f double d _asm { mov edx, f //ok mov edx, d //error operand size conflict Can someone show me the error of my ways??? Thanks "Si" <me@you.twang> wrote in message news:sm1gk0l60ng23gilu9b68hkcqi3ub9horn@4ax.com... > > > I'm trying to speed up a maths routine which consists of much > iterative multiplication (and adding and subtraction of ) doubles. > > Now if I de...

EXCEL FILE a copy/a copy/a copy ....filename
when i save my excel file i get a saved copy of the file, not the file itself so my file name of a file i save 7 times can have 7 'a copy' in it eg c./my doc/a copy/a copy/ a copy..............a copy/xx.xls Is the file saved as Read Only? Do you get a message on opening asking you if it's read only? Regards, Alan. "ve" <ve@discussions.microsoft.com> wrote in message news:88C3DEBA-2C2E-4DF0-978B-F823E120458A@microsoft.com... > when i save my excel file i get a saved copy of the file, not the file > itself so my file name of a file i save ...

How do I move the data in one cell on a page to another page/cell
What command do I use to move the containts of one cell on a page to a different page and cell??? On the receiving page and cell, type = then navigate to the sheet and cell where you wish to extract the data and press Enter. Assuming you were in cell B1 of Sheet2,and you wanted it to copy the value from Sheet1 cell A10, then Excel would insert the formula =Sheet1!A10 You can just type the formula yourself, but it is probably easier to let Excel do it for you first time. Note, if your sheet name contains spaces, then the formula need single quotes around the sheet name. ='My new s...

copy of treeview
Hello, I have to copy information from a treeview to another treeview... The first TreeCtrl is in a dialog and the second it's in another dialog. The application is Dialog Based and I use my oen solution of property sheet. So if I want to call something from another dialog i make i this way: CResultsPageDlg resDlg((CMyAppDlg*)theApp.GetMainWnd()); resDlg.CopyTreeview(m_TreeView.GetRootItem(), m_TreeView.GetRootItem()); useing the solution from http://www.codeguru.com/Cpp/controls/treeview/dragdrop/article.php/c657/ Somebody knows how to do that? Alex What is CopyTreeview method...

everything I copy from explorer end up green in Excel How can I c.
How can I change this to copy the actual colors from the html file ??? "Sacs" wrote: > ...

Copy to Clipboard from Excel 2003
Hi, I run Office 2003. In Excel, I consistently have problems with copying information from cells. The following happens: - I select a numeric cell and do a ctrl+c, message from Excel "cannot empty the clipboard". Item does not appear on clipboard " - I select a text cell and do a ctrl+c, message from Excel "an error occured while trying to retrieve information from another program. Try working with your message again". When I OK this message, it appears one more time and when I OK a second time, the item is placed on the clipboard. I do run a very useful app call...

When copying an item, quantity should not be copied
Whan an item is "copied" to make a new item, the Quantity should not be copied (just like "Balance Due" is not copied when a customer is copied) ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree" in the message pane. http://www.microsoft.com/Businesssolutions/Community/New...

backup type "copy"
I am backing up my Exchange 2003 server daily using the "full" backup type. I am backing this up to tape. As another layer of protection, I was thinking about creating a backup job that was the "copy" backup type so I could backup exchange to another server or network attached storage. This wouldn't interfere with my current backup strategy, and it would give me another copy of the database stored somewhere. In the scenario where I need to restore from tape, and the tape was bad, I could then turn to this "copy". What caveats do I need to be aware of ...

gathering info & importing to another worksheet
i am trying to track my servers sales per hour and am needing to organize my data from the week by their server numbers, sum each servers sales for the week, and pull all of this data from a separate workBOOK. i can do the rest of the formulas and set up myself, but the prior poses a problem for me... can anyone help? thanks! Dim oExcel As Excel.Application Dim oWorkbook As Workbook Dim oWorksheet As Worksheet Set oExcel = New Excel.Application Set oWorkbook = oExcel.Workbooks.Open(FileName:="C:\WorkbookName.xls", _ UpdateLinks:=0, _ IgnoreReadOnl...