Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 14th, 2014, 07:22 AM
Registered User
 
Join Date: Oct 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default copy between arrays - subscript out of range

Need to copy elements between arrays before I paste it into my worksheet.
But when I run this subrutine I get an "Subscript out of range"-error in the line with "faktura_kopi(r, c) = faktura_tabell(r, c)"
Please help!

Sub Hent_faktura()
'
' Hent_faktura Makro
'
'
Dim faktura_tabell As Variant
Dim rader As Integer
Dim kolonner As Integer
Dim tabell_ref As String
Dim fakturanr As Integer
Dim r, c As Integer
Dim faktura_kopi() As Variant

Sheets("Fakturaarkiv").Select
fakturanr = Worksheets("Endre faktura").Range("B1").Value
Range("B6").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
omr = Selection.Address
faktura_tabell = Range(omr)
rader = UBound(faktura_tabell, 1)
kolonner = UBound(faktura_tabell, 2)

'Copy between arrays keeping only rows with fakturanr in (r,7)
For r = 1 To rader
If faktura_tabell(r, 7) = fakturanr Then
For c = 1 To kolonner
faktura_kopi(r, c) = faktura_tabell(r, c)
Next c
End If
Next r

Sheets("Endre faktura").Select
Range("a4").Select
omr = "A4:I" & 4 + UBound(faktura_kopi)
Range(omr) = faktura_kopi

End Sub

Last edited by Tore Berg; October 14th, 2014 at 07:26 AM..
 
Old November 4th, 2014, 12:42 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi Tore

The reason for the error is faktura_kopi needs to have dimension

Include the following statement before the For loop and it should work

ReDim faktura_kopi(rader , kolonner )


Regards
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Table using Range Object ArchieDog Word VBA 2 May 2nd, 2009 07:10 AM
Copy Paste Dynamic Range jgrant Excel VBA 1 February 5th, 2008 10:13 PM
Arrays - subsript out of range ronny Classic ASP Basics 2 October 19th, 2007 06:16 AM
Copy a worksheet range to a new workbook cej2583 Excel VBA 2 March 14th, 2006 11:55 PM
Looping thro' / Copy from Range iwat03 Excel VBA 1 July 3rd, 2003 11:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.