subscript out of range issue. I have a workbook and I need to copy data from a sheet to another which already contains data in it of the same workbook

mareena

New Member
Joined
Feb 19, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub customercheck()

Dim i, j, k, lastrow, lastrow2 As Integer
Dim arr(), outputarr()
Dim customername, kam, segment, unit As String
Dim endurid As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim target As Workbook


For Each wb In Application.Workbooks
 If wb.Name <> "Offline Deal prototype.xlsm" Then
 With wb.Sheets("customer")
 lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
 
ReDim arr(0 To lastrow - 1, 0 To 4)
    For i = LBound(a) To UBound(a)
         For j = LBound(a, 2) To UBound(a, 2)
                    arr(i, j) = .Cells(i + IIf(LBound(a) = 0, 1, 0), j + IIf(LBound(a, 2) = 0, 1, 0)).Value
         Next j
     Next i
 End With
 End If
 Next wb


inputData = inputbox("Enter EndurID")
For i = 1 To lastrow
    For j = 1 To 1
       If arr(i, 1) <> inputData Then
          custname = arr(i, 2)
          segment = arr(i, 3)
          kam = arr(i, 4)
          unit = arr(i, 5)
        Else: inputbox ("EndurID not found please add")
       End If
    Next j
 Next i



Workbooks("Offline Deal prototype.xlsm").Sheets("output").Activate

ReDim outputarr(0 To lastrow - 1, 0 To 37)
 
                For p = 1 To lastrow
                If IsEmpty(outputarr(i, 0)) Then
                        outputarr(p - 1, 2) = endurid
                        outputarr(p - 1, 7) = customername
                        outputarr(p - 1, 4) = segment
                        outputarr(p - 1, 11) = kam
                        outputarr(p - 1, 4) = unit
                    
                    Exit For
                End If
            Next p
        
        


lastrow2 = Range("A" & Application.Rows.Count).End(xlUp).Row

Columns(2).NumberFormat = "@"
Range(Cells(lastrow2 + 1, 1), Cells(lastrow2 + lastrow, 38)) = outputarr

Worksheets("Action").Activate


End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi. Its always helpful to give the line that causes the error. One immediate thing that springs out is that you call your array 'arr' then refer to it as 'a' but that wouldnt cause the error you state. Its more likely the sheet called 'customer' doesnt appear in the workbook 'wb'
 
Upvote 0
Hi. Its always helpful to give the line that causes the error. One immediate thing that springs out is that you call your array 'arr' then refer to it as 'a' but that wouldnt cause the error you state. Its more likely the sheet called 'customer' doesnt appear in the workbook 'wb'
here shows the error
 

Attachments

  • Capture4.PNG
    Capture4.PNG
    11.4 KB · Views: 16
Upvote 0
How did you get to that line without error in the provided code? Please provide the code thats actually being run.
 
Upvote 0
How did you get to that line without error in the provided code? Please provide the code thats actually being run.
VBA Code:
Sub customercheck()

Dim i, j, k, lastrow, lastrow2 As Integer
Dim arr(), outputarr()
Dim customername, kam, segment, unit As String
Dim endurid As Variant
Dim wb As Workbook
Dim ws As Worksheet
Dim target As Workbook


For Each wb In Application.Workbooks
 If wb.Name <> "Offline Deal prototype.xlsm" Then
 With wb.Sheets("customer")
 lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
 
ReDim arr(0 To lastrow - 1, 0 To 4)
    For i = LBound(arr) To UBound(arr)
         For j = LBound(arr, 2) To UBound(arr, 2)
                    arr(i, j) = .Cells(i + IIf(LBound(arr) = 0, 1, 0), j + IIf(LBound(arr, 2) = 0, 1, 0)).Value
         Next j
     Next i
 End With
 End If
 Next wb


inputData = inputbox("Enter EndurID")
For i = 1 To lastrow
    For j = 1 To 1
       If arr(i, 1) <> inputData Then
          custname = arr(i, 2)
          segment = arr(i, 3)
          kam = arr(i, 4)
          unit = arr(i, 5)
        Else: inputbox ("EndurID not found please add")
       End If
    Next j
 Next i



Workbooks("Offline Deal prototype.xlsm").Sheets("output").Activate

[COLOR=rgb(250, 197, 28)]ReDim outputarr(0 To lastrow - 1, 0 To 37)[/COLOR]
 
                For p = 1 To lastrow
                If IsEmpty(outputarr(i, 0)) Then
                        outputarr(p - 1, 2) = endurid
                        outputarr(p - 1, 7) = customername
                        outputarr(p - 1, 4) = segment
                        outputarr(p - 1, 11) = kam
                        outputarr(p - 1, 4) = unit
                    
                    Exit For
                End If
            Next p
        
        


lastrow2 = Range("A" & Application.Rows.Count).End(xlUp).Row

Columns(2).NumberFormat = "@"
Range(Cells(lastrow2 + 1, 1), Cells(lastrow2 + lastrow, 38)) = outputarr

Worksheets("Action").Activate


End Sub
 
Upvote 0
Put in this line just before the line that errors:

VBA Code:
Msgbox lastrow

I suspect you get an empty message box.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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