Subscription out of range

ashani

Active Member
Joined
Mar 14, 2020
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hi
I'm trying to copy specific data from one workbook to another workbook. I'm using the following code and getting error message "Subscription out of range" - please can someone help me with this.
VBA Code:
Option Explicit
Option Base 1
Sub CopyTo()
    Dim Cel As Range
    Dim wsc As Worksheet, wst As Worksheet
    Dim wbc As Workbook, wbt As Workbook
    Dim arrc, arrt
    Dim k&, i&, j&, m&
    Dim mydata As Workbook

    Set wbc = ThisWorkbook
    Set wsc = wbc.Worksheets("sheet1")
    arrc = wsc.[A85:D93]
    k = 1: m = 0
    ReDim arrt(1, UBound(arrc, 1) * (UBound(arrc, 2) - 1))
    For i = 1 To UBound(arrc, 1)
        For j = 2 To UBound(arrc, 2)
            arrt(k, m + j - 1) = arrc(i, j)
        Next j
        m = m + 5
    Next i
    
    Set mydata = Workbooks.Open("C:\Users\mybook.xlsx")
    Set wbt = Workbooks("mybook.xlsx")
    Set wst = wbt.Worksheets("Data")
    i = WorksheetFunction.Match(UCase(wsc.[t3]), wst.[a5:a82], 0)
    wst.Cells(i, 2).Resize(UBound(arrt, 1), UBound(arrt, 2)) = arrt

mydata.Save
mydata.Close


End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Which line of code gives that error?
 
Upvote 0
If I change this to a5:d14

Set wbc = ThisWorkbook
Set wsc = wbc.Worksheets("sheet1")
arrc = wsc.[A85:D93]

and

i = WorksheetFunction.Match(UCase(wsc.[t3]), wst.[a5:a82], 0)

to a1:a10

then it works fine otherwise error message
 
Upvote 0
What are you trying to do with the arrt?
At the moment you are bringing in data from cols B,C & D, & then leaving two empty slots.
 
Upvote 0
That’s exactly I want to do to data from all columns from col b to aa and don’t leave any empty slots in the middle.
thank you
 
Upvote 0
In that case try
VBA Code:
Option Base 1
Sub CopyTo()
    Dim Cel As Range
    Dim wsc As Worksheet, wst As Worksheet
    Dim wbc As Workbook, wbt As Workbook
    Dim arrc, arrt
    Dim k&, i&, j&, m&
    Dim mydata As Workbook

    Set wbc = ThisWorkbook
    Set wsc = wbc.Worksheets("pcode")
    arrc = wsc.[B85:D93]
    k = 1: m = 0
    ReDim arrt(1, UBound(arrc, 1) * (UBound(arrc, 2)))
    For i = 1 To UBound(arrc, 1)
        For j = 1 To UBound(arrc, 2)
            arrt(k, m + j) = arrc(i, j)
        Next j
        m = m + UBound(arrc, 2)
    Next i
    
    Set mydata = Workbooks.Open("C:\Users\mybook.xlsx")
    Set wbt = Workbooks("mybook.xlsx")
    Set wst = wbt.Worksheets("Data")
    i = WorksheetFunction.Match(UCase(wsc.[t3]), wst.[a5:a82], 0)
    wst.Cells(i, 2).Resize(UBound(arrt, 1), UBound(arrt, 2)) = arrt

mydata.Save
mydata.Close


End Sub
 
Upvote 0
absolutely star - thank you so much
really apprecicate your help.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Hi,
Can I ask a quick question in regards to the above syntax :
Currently, if there is no value in T3 then it comes up with an error message. Is it possible to have message box instead of error message to say "Please select region"?
Many thanks once again,
 
Upvote 0

Forum statistics

Threads
1,215,526
Messages
6,125,328
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top