VBA: creating array

R2ah1ze1l

Board Regular
Joined
Nov 10, 2017
Messages
93
In the loops below, I am attempting to copy a series of data that is along a row, then I want to paste it into another workbook/sheet transposed into a column. When I get to " data_val(y) = wsExport.Range(Cells(x, y + csvdata_start)).Value" line I am receiving a 1004 Runtime error: Method 'Range' of object'_Worksheet' failed.

Adding the workbook gets me a 438 error
Removing the "wsExport." gets me the same 1004 error.

I had the data transferring with the 2 while loops but the method I used was VERY slow and I was hoping this array might help pick up the pace...

Code:
While x < lastr
x = x + 1                                                               'begins rows
wsExport.Activate
        sn_val = wsExport.Cells(x, 1).Value
        oper_val = wsExport.Cells(x, 2).Value
        dt_val = wsExport.Cells(x, 3).Value
        'tm_val = wsExport.Cells(x, 4).Value
        While y < lastc                                                 'begin column
        y = y + 1
        data_val(y) = wsExport.Range(Cells(x, y + csvdata_start)).Value
        Wend                                                            'end column
        y = 0
wsReview.Activate
        wsReview.Cells(2, ic + x).Value = sn_val
        wsReview.Cells(3, ic + x).Value = oper_val
        wsReview.Cells(4, ic + x).Value = dt_val
        'wsReview.Cells(5, ic + x).Value = tm_val
        While y < lastc
        y = y + 1
        data_val(y) = Range(Cells(ir + y, ic + x)).Value = Format(data_val, "0.0000")
        Wend
        y = 0
Wend
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try it like
VBA Code:
data_val(y) = wsExport.Cells(x, y + csvdata_start).Value
 
Upvote 0
What are wsExport & csvdata_start?
 
Upvote 0
Code:
fl_split = wsReview.Range("D2").Value
iss_num = Format(wsReview.Range("E2").Value, "Standard")
pn_v = Split(fl_split, "_")(0) 'part number
pt_v = Split(fl_split, "_")(1) 'prog type
po_v = Split(fl_split, "_")(2) 'OP number
pm_v = Split(fl_split, "_")(3) 'Mach ID

efil_nam = fl_split & "-" & iss_num 'file and sheet title

Epath = "M:\Reports Archive\" & pn_v & "\" & "OP" & po_v & "\Export" 'Export folder location (2020 Template)
sExport = efil_nam & ".csv"                                          'CSV file name
FEpath = Epath & "\" & sExport

Set wbExport = Workbooks.Open(FEpath)
Set wsExport = wbExport.Worksheets(efil_nam)
csvdata_start = 3

I use the start location in more than one location and alternate worksheets can start at column 3 up to 9 before the data come out.
 
Upvote 0
Ok, those should be fine, how have you defined & sized data_val
 
Upvote 0
Dim data_val() As Long
Then
ReDim data_val(1 To lastr) As Long

I found 1 error that I should be using lastc not lastr.

When I changed that:
Code:
        While y < lastc
        y = y + 1
        data_val(y) = wsReview.Cells(ir + y, ic + x).Value = Format(data_val, "0.0000")
        Wend
This is where I'm writing it back now throws a '13' for type mismatch.
 
Upvote 0
Ok, what is the value of lastr when you redim the array & what is the value of y when you get the error?
 
Upvote 0
lastr in the file I'm using results in a total of 109 rows
It appeared to trip on #1 because I didn't have Format(data_val ( y ),"0.0000")

However, now that I 'corrected' that, I do not see any data on my wsReview sheet when I execute the code. Though I do not receive any more errors.
 
Upvote 0
There is nothing in the code you posted that puts the array onto a sheet.
 
Upvote 0

Forum statistics

Threads
1,215,449
Messages
6,124,911
Members
449,195
Latest member
Stevenciu

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