VBA can't import range into array when the code is run with F5 but it will if it's run line by line (F8)





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















So as the title indicates, when run with F5, the VBA code I've written prompts an error whenever it reaches the line where it has to import a range into a two dimensional array:



Vendor = wb.Sheets("Output").Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


However, whenever run line by line (F8), the VBA code doesn't prompt any error at all.



To give some context, the objective of this code is to transpose rows of data into two columns and to, then, import these two columns into a two dimensional array for use in a loop.



It's a strange behavior that I have no clue as to how to explain it.



I'd appreciate your help.



Thanks



Option Explicit
Sub VendorFinder()

'variable declaration
Dim msg As String
Dim ans As Integer
Dim rng As Range
Dim DescRng As Range
Dim DescCol As Range
Dim VendorCol As Range
Dim j As Long
Dim Vendor As Variant
Dim wb As Workbook
Dim sFile As String
Dim myVendor As Variant
Dim FirstRow As Range
Dim VendorRng As Range
Dim r&, cnt&
Dim rangeroo As Range, rngRow As Range

On Error GoTo BadEntry

TryAgain:

'set columns
Set DescCol = Application.InputBox("Select Description Column", "Obtain Object Range", Type:=8)
Set VendorCol = Application.InputBox("Select Vendor Column", "Obtain Object Range", Type:=8)
Set FirstRow = Application.InputBox("Select First Row with Data", "Obtain Object Range", Type:=8)

'set ranges
Set DescRng = Range(Cells(FirstRow.Row, DescCol.Column), Cells(Cells(Rows.Count, DescCol.Column).End(xlUp).Row, DescCol.Column))
Set VendorRng = Range(Cells(FirstRow.Row, VendorCol.Column), Cells(Cells(Rows.Count, DescCol.Column).End(xlUp).Row, VendorCol.Column))
myVendor = VendorRng.Value2

'import vendors
sFile = "D:DesktopVendor List.xlsx"
Application.ScreenUpdating = False
Set wb = Application.Workbooks.Open(sFile)
Set rangeroo = wb.Sheets("Source").Range("A1").CurrentRegion
r = 1
For Each rngRow In rangeroo.Rows
cnt = WorksheetFunction.CountA(rngRow.Cells)
With wb.Sheets("Output").Cells(r, 1).Resize(cnt)
.Value = rngRow.Cells(1).Value
.Offset(, 1).Value = Application.Transpose(rngRow.Resize(, cnt).Value)
End With
r = r + cnt
Next

Vendor = wb.Sheets("Output").Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

wb.Close False
Application.ScreenUpdating = True

For Each rng In DescRng

If Cells(rng.Row, VendorCol.Column).Value = "" Then

For j = LBound(Vendor) To UBound(Vendor)

If InStr(1, rng.Value, Vendor(j, 2), vbTextCompare) > 0 Then
myVendor(rng.Row - FirstRow.Row + 1, 1) = Vendor(j, 1)

Exit For

End If

Next j

End If

Next rng

VendorRng.Resize(UBound(myVendor) - LBound(myVendor) + 1, 1) = myVendor

Exit Sub

BadEntry:

msg = "You have clicked on cancel for one of the prompts."
msg = msg & vbNewLine
msg = msg & "Do you wish to try again?"
ans = MsgBox(msg, vbRetryCancel + vbExclamation)
If ans = vbRetry Then Resume TryAgain

End Sub









share|improve this question





























    0















    So as the title indicates, when run with F5, the VBA code I've written prompts an error whenever it reaches the line where it has to import a range into a two dimensional array:



    Vendor = wb.Sheets("Output").Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


    However, whenever run line by line (F8), the VBA code doesn't prompt any error at all.



    To give some context, the objective of this code is to transpose rows of data into two columns and to, then, import these two columns into a two dimensional array for use in a loop.



    It's a strange behavior that I have no clue as to how to explain it.



    I'd appreciate your help.



    Thanks



    Option Explicit
    Sub VendorFinder()

    'variable declaration
    Dim msg As String
    Dim ans As Integer
    Dim rng As Range
    Dim DescRng As Range
    Dim DescCol As Range
    Dim VendorCol As Range
    Dim j As Long
    Dim Vendor As Variant
    Dim wb As Workbook
    Dim sFile As String
    Dim myVendor As Variant
    Dim FirstRow As Range
    Dim VendorRng As Range
    Dim r&, cnt&
    Dim rangeroo As Range, rngRow As Range

    On Error GoTo BadEntry

    TryAgain:

    'set columns
    Set DescCol = Application.InputBox("Select Description Column", "Obtain Object Range", Type:=8)
    Set VendorCol = Application.InputBox("Select Vendor Column", "Obtain Object Range", Type:=8)
    Set FirstRow = Application.InputBox("Select First Row with Data", "Obtain Object Range", Type:=8)

    'set ranges
    Set DescRng = Range(Cells(FirstRow.Row, DescCol.Column), Cells(Cells(Rows.Count, DescCol.Column).End(xlUp).Row, DescCol.Column))
    Set VendorRng = Range(Cells(FirstRow.Row, VendorCol.Column), Cells(Cells(Rows.Count, DescCol.Column).End(xlUp).Row, VendorCol.Column))
    myVendor = VendorRng.Value2

    'import vendors
    sFile = "D:DesktopVendor List.xlsx"
    Application.ScreenUpdating = False
    Set wb = Application.Workbooks.Open(sFile)
    Set rangeroo = wb.Sheets("Source").Range("A1").CurrentRegion
    r = 1
    For Each rngRow In rangeroo.Rows
    cnt = WorksheetFunction.CountA(rngRow.Cells)
    With wb.Sheets("Output").Cells(r, 1).Resize(cnt)
    .Value = rngRow.Cells(1).Value
    .Offset(, 1).Value = Application.Transpose(rngRow.Resize(, cnt).Value)
    End With
    r = r + cnt
    Next

    Vendor = wb.Sheets("Output").Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

    wb.Close False
    Application.ScreenUpdating = True

    For Each rng In DescRng

    If Cells(rng.Row, VendorCol.Column).Value = "" Then

    For j = LBound(Vendor) To UBound(Vendor)

    If InStr(1, rng.Value, Vendor(j, 2), vbTextCompare) > 0 Then
    myVendor(rng.Row - FirstRow.Row + 1, 1) = Vendor(j, 1)

    Exit For

    End If

    Next j

    End If

    Next rng

    VendorRng.Resize(UBound(myVendor) - LBound(myVendor) + 1, 1) = myVendor

    Exit Sub

    BadEntry:

    msg = "You have clicked on cancel for one of the prompts."
    msg = msg & vbNewLine
    msg = msg & "Do you wish to try again?"
    ans = MsgBox(msg, vbRetryCancel + vbExclamation)
    If ans = vbRetry Then Resume TryAgain

    End Sub









    share|improve this question

























      0












      0








      0








      So as the title indicates, when run with F5, the VBA code I've written prompts an error whenever it reaches the line where it has to import a range into a two dimensional array:



      Vendor = wb.Sheets("Output").Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


      However, whenever run line by line (F8), the VBA code doesn't prompt any error at all.



      To give some context, the objective of this code is to transpose rows of data into two columns and to, then, import these two columns into a two dimensional array for use in a loop.



      It's a strange behavior that I have no clue as to how to explain it.



      I'd appreciate your help.



      Thanks



      Option Explicit
      Sub VendorFinder()

      'variable declaration
      Dim msg As String
      Dim ans As Integer
      Dim rng As Range
      Dim DescRng As Range
      Dim DescCol As Range
      Dim VendorCol As Range
      Dim j As Long
      Dim Vendor As Variant
      Dim wb As Workbook
      Dim sFile As String
      Dim myVendor As Variant
      Dim FirstRow As Range
      Dim VendorRng As Range
      Dim r&, cnt&
      Dim rangeroo As Range, rngRow As Range

      On Error GoTo BadEntry

      TryAgain:

      'set columns
      Set DescCol = Application.InputBox("Select Description Column", "Obtain Object Range", Type:=8)
      Set VendorCol = Application.InputBox("Select Vendor Column", "Obtain Object Range", Type:=8)
      Set FirstRow = Application.InputBox("Select First Row with Data", "Obtain Object Range", Type:=8)

      'set ranges
      Set DescRng = Range(Cells(FirstRow.Row, DescCol.Column), Cells(Cells(Rows.Count, DescCol.Column).End(xlUp).Row, DescCol.Column))
      Set VendorRng = Range(Cells(FirstRow.Row, VendorCol.Column), Cells(Cells(Rows.Count, DescCol.Column).End(xlUp).Row, VendorCol.Column))
      myVendor = VendorRng.Value2

      'import vendors
      sFile = "D:DesktopVendor List.xlsx"
      Application.ScreenUpdating = False
      Set wb = Application.Workbooks.Open(sFile)
      Set rangeroo = wb.Sheets("Source").Range("A1").CurrentRegion
      r = 1
      For Each rngRow In rangeroo.Rows
      cnt = WorksheetFunction.CountA(rngRow.Cells)
      With wb.Sheets("Output").Cells(r, 1).Resize(cnt)
      .Value = rngRow.Cells(1).Value
      .Offset(, 1).Value = Application.Transpose(rngRow.Resize(, cnt).Value)
      End With
      r = r + cnt
      Next

      Vendor = wb.Sheets("Output").Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

      wb.Close False
      Application.ScreenUpdating = True

      For Each rng In DescRng

      If Cells(rng.Row, VendorCol.Column).Value = "" Then

      For j = LBound(Vendor) To UBound(Vendor)

      If InStr(1, rng.Value, Vendor(j, 2), vbTextCompare) > 0 Then
      myVendor(rng.Row - FirstRow.Row + 1, 1) = Vendor(j, 1)

      Exit For

      End If

      Next j

      End If

      Next rng

      VendorRng.Resize(UBound(myVendor) - LBound(myVendor) + 1, 1) = myVendor

      Exit Sub

      BadEntry:

      msg = "You have clicked on cancel for one of the prompts."
      msg = msg & vbNewLine
      msg = msg & "Do you wish to try again?"
      ans = MsgBox(msg, vbRetryCancel + vbExclamation)
      If ans = vbRetry Then Resume TryAgain

      End Sub









      share|improve this question














      So as the title indicates, when run with F5, the VBA code I've written prompts an error whenever it reaches the line where it has to import a range into a two dimensional array:



      Vendor = wb.Sheets("Output").Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


      However, whenever run line by line (F8), the VBA code doesn't prompt any error at all.



      To give some context, the objective of this code is to transpose rows of data into two columns and to, then, import these two columns into a two dimensional array for use in a loop.



      It's a strange behavior that I have no clue as to how to explain it.



      I'd appreciate your help.



      Thanks



      Option Explicit
      Sub VendorFinder()

      'variable declaration
      Dim msg As String
      Dim ans As Integer
      Dim rng As Range
      Dim DescRng As Range
      Dim DescCol As Range
      Dim VendorCol As Range
      Dim j As Long
      Dim Vendor As Variant
      Dim wb As Workbook
      Dim sFile As String
      Dim myVendor As Variant
      Dim FirstRow As Range
      Dim VendorRng As Range
      Dim r&, cnt&
      Dim rangeroo As Range, rngRow As Range

      On Error GoTo BadEntry

      TryAgain:

      'set columns
      Set DescCol = Application.InputBox("Select Description Column", "Obtain Object Range", Type:=8)
      Set VendorCol = Application.InputBox("Select Vendor Column", "Obtain Object Range", Type:=8)
      Set FirstRow = Application.InputBox("Select First Row with Data", "Obtain Object Range", Type:=8)

      'set ranges
      Set DescRng = Range(Cells(FirstRow.Row, DescCol.Column), Cells(Cells(Rows.Count, DescCol.Column).End(xlUp).Row, DescCol.Column))
      Set VendorRng = Range(Cells(FirstRow.Row, VendorCol.Column), Cells(Cells(Rows.Count, DescCol.Column).End(xlUp).Row, VendorCol.Column))
      myVendor = VendorRng.Value2

      'import vendors
      sFile = "D:DesktopVendor List.xlsx"
      Application.ScreenUpdating = False
      Set wb = Application.Workbooks.Open(sFile)
      Set rangeroo = wb.Sheets("Source").Range("A1").CurrentRegion
      r = 1
      For Each rngRow In rangeroo.Rows
      cnt = WorksheetFunction.CountA(rngRow.Cells)
      With wb.Sheets("Output").Cells(r, 1).Resize(cnt)
      .Value = rngRow.Cells(1).Value
      .Offset(, 1).Value = Application.Transpose(rngRow.Resize(, cnt).Value)
      End With
      r = r + cnt
      Next

      Vendor = wb.Sheets("Output").Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

      wb.Close False
      Application.ScreenUpdating = True

      For Each rng In DescRng

      If Cells(rng.Row, VendorCol.Column).Value = "" Then

      For j = LBound(Vendor) To UBound(Vendor)

      If InStr(1, rng.Value, Vendor(j, 2), vbTextCompare) > 0 Then
      myVendor(rng.Row - FirstRow.Row + 1, 1) = Vendor(j, 1)

      Exit For

      End If

      Next j

      End If

      Next rng

      VendorRng.Resize(UBound(myVendor) - LBound(myVendor) + 1, 1) = myVendor

      Exit Sub

      BadEntry:

      msg = "You have clicked on cancel for one of the prompts."
      msg = msg & vbNewLine
      msg = msg & "Do you wish to try again?"
      ans = MsgBox(msg, vbRetryCancel + vbExclamation)
      If ans = vbRetry Then Resume TryAgain

      End Sub






      excel vba excel-vba






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 16:19









      SamSam

      808




      808
























          1 Answer
          1






          active

          oldest

          votes


















          2














          When you define a range you must add a sheet reference to every Range/Cells otherwise the active sheet is assumed.



          Your code is equivalent to



          Vendor = wb.Sheets("Output").Range(activesheet.Cells(1, 1), activesheet.Cells(activesheet.Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


          and the error arises because you are referencing two different sheets (unless Output is the active sheet).



          The neatest way around this is to use With (btw can simplify your expression too I think):



          With wb.Sheets("Output")
          Vendor = .Range(.Cells(1, 1), .Cells(.Rows.Count, 2).End(xlUp)).Value2
          End With





          share|improve this answer





















          • 1





            @BigBen - fair enough, I usually overlook it but can't do any harm and could avoid an error.

            – SJR
            Nov 23 '18 at 16:29











          • Still errors at Vendor = .Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

            – Sam
            Nov 23 '18 at 16:39











          • oops just realized my mistake

            – Sam
            Nov 23 '18 at 16:41












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450006%2fvba-cant-import-range-into-array-when-the-code-is-run-with-f5-but-it-will-if-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          When you define a range you must add a sheet reference to every Range/Cells otherwise the active sheet is assumed.



          Your code is equivalent to



          Vendor = wb.Sheets("Output").Range(activesheet.Cells(1, 1), activesheet.Cells(activesheet.Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


          and the error arises because you are referencing two different sheets (unless Output is the active sheet).



          The neatest way around this is to use With (btw can simplify your expression too I think):



          With wb.Sheets("Output")
          Vendor = .Range(.Cells(1, 1), .Cells(.Rows.Count, 2).End(xlUp)).Value2
          End With





          share|improve this answer





















          • 1





            @BigBen - fair enough, I usually overlook it but can't do any harm and could avoid an error.

            – SJR
            Nov 23 '18 at 16:29











          • Still errors at Vendor = .Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

            – Sam
            Nov 23 '18 at 16:39











          • oops just realized my mistake

            – Sam
            Nov 23 '18 at 16:41
















          2














          When you define a range you must add a sheet reference to every Range/Cells otherwise the active sheet is assumed.



          Your code is equivalent to



          Vendor = wb.Sheets("Output").Range(activesheet.Cells(1, 1), activesheet.Cells(activesheet.Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


          and the error arises because you are referencing two different sheets (unless Output is the active sheet).



          The neatest way around this is to use With (btw can simplify your expression too I think):



          With wb.Sheets("Output")
          Vendor = .Range(.Cells(1, 1), .Cells(.Rows.Count, 2).End(xlUp)).Value2
          End With





          share|improve this answer





















          • 1





            @BigBen - fair enough, I usually overlook it but can't do any harm and could avoid an error.

            – SJR
            Nov 23 '18 at 16:29











          • Still errors at Vendor = .Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

            – Sam
            Nov 23 '18 at 16:39











          • oops just realized my mistake

            – Sam
            Nov 23 '18 at 16:41














          2












          2








          2







          When you define a range you must add a sheet reference to every Range/Cells otherwise the active sheet is assumed.



          Your code is equivalent to



          Vendor = wb.Sheets("Output").Range(activesheet.Cells(1, 1), activesheet.Cells(activesheet.Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


          and the error arises because you are referencing two different sheets (unless Output is the active sheet).



          The neatest way around this is to use With (btw can simplify your expression too I think):



          With wb.Sheets("Output")
          Vendor = .Range(.Cells(1, 1), .Cells(.Rows.Count, 2).End(xlUp)).Value2
          End With





          share|improve this answer















          When you define a range you must add a sheet reference to every Range/Cells otherwise the active sheet is assumed.



          Your code is equivalent to



          Vendor = wb.Sheets("Output").Range(activesheet.Cells(1, 1), activesheet.Cells(activesheet.Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2


          and the error arises because you are referencing two different sheets (unless Output is the active sheet).



          The neatest way around this is to use With (btw can simplify your expression too I think):



          With wb.Sheets("Output")
          Vendor = .Range(.Cells(1, 1), .Cells(.Rows.Count, 2).End(xlUp)).Value2
          End With






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 16:29

























          answered Nov 23 '18 at 16:27









          SJRSJR

          13.6k31219




          13.6k31219








          • 1





            @BigBen - fair enough, I usually overlook it but can't do any harm and could avoid an error.

            – SJR
            Nov 23 '18 at 16:29











          • Still errors at Vendor = .Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

            – Sam
            Nov 23 '18 at 16:39











          • oops just realized my mistake

            – Sam
            Nov 23 '18 at 16:41














          • 1





            @BigBen - fair enough, I usually overlook it but can't do any harm and could avoid an error.

            – SJR
            Nov 23 '18 at 16:29











          • Still errors at Vendor = .Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

            – Sam
            Nov 23 '18 at 16:39











          • oops just realized my mistake

            – Sam
            Nov 23 '18 at 16:41








          1




          1





          @BigBen - fair enough, I usually overlook it but can't do any harm and could avoid an error.

          – SJR
          Nov 23 '18 at 16:29





          @BigBen - fair enough, I usually overlook it but can't do any harm and could avoid an error.

          – SJR
          Nov 23 '18 at 16:29













          Still errors at Vendor = .Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

          – Sam
          Nov 23 '18 at 16:39





          Still errors at Vendor = .Range(Cells(1, 1), Cells(Cells(Rows.Count, 2).End(xlUp).Row, 2)).Value2

          – Sam
          Nov 23 '18 at 16:39













          oops just realized my mistake

          – Sam
          Nov 23 '18 at 16:41





          oops just realized my mistake

          – Sam
          Nov 23 '18 at 16:41




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450006%2fvba-cant-import-range-into-array-when-the-code-is-run-with-f5-but-it-will-if-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini