Searching multiple Excel sheets for the cheapest item











up vote
0
down vote

favorite












I am creating an estimation tool, where users can input part numbers and quantities, and the tool will output the lowest cost for that part number based on searches through multiple databases.



I accomplish this by loading data into multiple sheets and then evaluate all possibilities on one sheet.



I believe my code is struggling, because I have multiple instances of this loop below which I'm hoping someone can help me improve. The code below runs 3 separate times, for 3 different sheets. There can be up-to 1000 part numbers run at a time. Once the tool has been run, to start over I delete all the sheets, so each sheet is created every time the macro runs.



'Add Content to Summary

'MPN and Qty add to Summary page
Sheet22.Visible = True
Sheet1.Select
Range("A2").Select

Sheet22.Select
Range("A2").Select

For i = 1 To 3
Sheet1.Select
If Len(ActiveCell.Value) > 0 Then

xmpn = ActiveCell.Value
xqty = ActiveCell.Offset(0, 1).Value

Sheet22.Select
ActiveCell.Value = xmpn
ActiveCell.Offset(0, 1).Value = xqty
ActiveCell.Offset(1, 0).Select
Sheet1.Select
ActiveCell.Offset(1, 0).Select

Else
i = 10
End If
i = i - 1
Next


If there are 1000 part number entries, this code can take about 45 seconds to run, and causes excel to show not responding. Any help or suggestions to improve would be greatly appreciated.










share|improve this question









New contributor




Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • You should probably search directly in the database.
    – krish KM
    Nov 23 at 19:40






  • 1




    It would be best to work with the data in memory. But if you do write it to the worksheet this video will help immensely: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset). You should also watch: Excel VBA Introduction Part 25 - Arrays
    – TinMan
    2 days ago










  • @krishKM - Yes I am searching directly in 6 different databases, however 1 of them is through an excel add in which connects via an excel formula. and the other 2 instances of this loop are for the summary page and the evaluation page.
    – Aaron Bates
    12 hours ago










  • @TinMan - I will check out those videos, thanks for the suggestion!
    – Aaron Bates
    12 hours ago















up vote
0
down vote

favorite












I am creating an estimation tool, where users can input part numbers and quantities, and the tool will output the lowest cost for that part number based on searches through multiple databases.



I accomplish this by loading data into multiple sheets and then evaluate all possibilities on one sheet.



I believe my code is struggling, because I have multiple instances of this loop below which I'm hoping someone can help me improve. The code below runs 3 separate times, for 3 different sheets. There can be up-to 1000 part numbers run at a time. Once the tool has been run, to start over I delete all the sheets, so each sheet is created every time the macro runs.



'Add Content to Summary

'MPN and Qty add to Summary page
Sheet22.Visible = True
Sheet1.Select
Range("A2").Select

Sheet22.Select
Range("A2").Select

For i = 1 To 3
Sheet1.Select
If Len(ActiveCell.Value) > 0 Then

xmpn = ActiveCell.Value
xqty = ActiveCell.Offset(0, 1).Value

Sheet22.Select
ActiveCell.Value = xmpn
ActiveCell.Offset(0, 1).Value = xqty
ActiveCell.Offset(1, 0).Select
Sheet1.Select
ActiveCell.Offset(1, 0).Select

Else
i = 10
End If
i = i - 1
Next


If there are 1000 part number entries, this code can take about 45 seconds to run, and causes excel to show not responding. Any help or suggestions to improve would be greatly appreciated.










share|improve this question









New contributor




Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • You should probably search directly in the database.
    – krish KM
    Nov 23 at 19:40






  • 1




    It would be best to work with the data in memory. But if you do write it to the worksheet this video will help immensely: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset). You should also watch: Excel VBA Introduction Part 25 - Arrays
    – TinMan
    2 days ago










  • @krishKM - Yes I am searching directly in 6 different databases, however 1 of them is through an excel add in which connects via an excel formula. and the other 2 instances of this loop are for the summary page and the evaluation page.
    – Aaron Bates
    12 hours ago










  • @TinMan - I will check out those videos, thanks for the suggestion!
    – Aaron Bates
    12 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am creating an estimation tool, where users can input part numbers and quantities, and the tool will output the lowest cost for that part number based on searches through multiple databases.



I accomplish this by loading data into multiple sheets and then evaluate all possibilities on one sheet.



I believe my code is struggling, because I have multiple instances of this loop below which I'm hoping someone can help me improve. The code below runs 3 separate times, for 3 different sheets. There can be up-to 1000 part numbers run at a time. Once the tool has been run, to start over I delete all the sheets, so each sheet is created every time the macro runs.



'Add Content to Summary

'MPN and Qty add to Summary page
Sheet22.Visible = True
Sheet1.Select
Range("A2").Select

Sheet22.Select
Range("A2").Select

For i = 1 To 3
Sheet1.Select
If Len(ActiveCell.Value) > 0 Then

xmpn = ActiveCell.Value
xqty = ActiveCell.Offset(0, 1).Value

Sheet22.Select
ActiveCell.Value = xmpn
ActiveCell.Offset(0, 1).Value = xqty
ActiveCell.Offset(1, 0).Select
Sheet1.Select
ActiveCell.Offset(1, 0).Select

Else
i = 10
End If
i = i - 1
Next


If there are 1000 part number entries, this code can take about 45 seconds to run, and causes excel to show not responding. Any help or suggestions to improve would be greatly appreciated.










share|improve this question









New contributor




Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am creating an estimation tool, where users can input part numbers and quantities, and the tool will output the lowest cost for that part number based on searches through multiple databases.



I accomplish this by loading data into multiple sheets and then evaluate all possibilities on one sheet.



I believe my code is struggling, because I have multiple instances of this loop below which I'm hoping someone can help me improve. The code below runs 3 separate times, for 3 different sheets. There can be up-to 1000 part numbers run at a time. Once the tool has been run, to start over I delete all the sheets, so each sheet is created every time the macro runs.



'Add Content to Summary

'MPN and Qty add to Summary page
Sheet22.Visible = True
Sheet1.Select
Range("A2").Select

Sheet22.Select
Range("A2").Select

For i = 1 To 3
Sheet1.Select
If Len(ActiveCell.Value) > 0 Then

xmpn = ActiveCell.Value
xqty = ActiveCell.Offset(0, 1).Value

Sheet22.Select
ActiveCell.Value = xmpn
ActiveCell.Offset(0, 1).Value = xqty
ActiveCell.Offset(1, 0).Select
Sheet1.Select
ActiveCell.Offset(1, 0).Select

Else
i = 10
End If
i = i - 1
Next


If there are 1000 part number entries, this code can take about 45 seconds to run, and causes excel to show not responding. Any help or suggestions to improve would be greatly appreciated.







performance vba excel






share|improve this question









New contributor




Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 5 mins ago









200_success

127k15148411




127k15148411






New contributor




Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 23 at 17:53









Aaron Bates

111




111




New contributor




Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • You should probably search directly in the database.
    – krish KM
    Nov 23 at 19:40






  • 1




    It would be best to work with the data in memory. But if you do write it to the worksheet this video will help immensely: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset). You should also watch: Excel VBA Introduction Part 25 - Arrays
    – TinMan
    2 days ago










  • @krishKM - Yes I am searching directly in 6 different databases, however 1 of them is through an excel add in which connects via an excel formula. and the other 2 instances of this loop are for the summary page and the evaluation page.
    – Aaron Bates
    12 hours ago










  • @TinMan - I will check out those videos, thanks for the suggestion!
    – Aaron Bates
    12 hours ago


















  • You should probably search directly in the database.
    – krish KM
    Nov 23 at 19:40






  • 1




    It would be best to work with the data in memory. But if you do write it to the worksheet this video will help immensely: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset). You should also watch: Excel VBA Introduction Part 25 - Arrays
    – TinMan
    2 days ago










  • @krishKM - Yes I am searching directly in 6 different databases, however 1 of them is through an excel add in which connects via an excel formula. and the other 2 instances of this loop are for the summary page and the evaluation page.
    – Aaron Bates
    12 hours ago










  • @TinMan - I will check out those videos, thanks for the suggestion!
    – Aaron Bates
    12 hours ago
















You should probably search directly in the database.
– krish KM
Nov 23 at 19:40




You should probably search directly in the database.
– krish KM
Nov 23 at 19:40




1




1




It would be best to work with the data in memory. But if you do write it to the worksheet this video will help immensely: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset). You should also watch: Excel VBA Introduction Part 25 - Arrays
– TinMan
2 days ago




It would be best to work with the data in memory. But if you do write it to the worksheet this video will help immensely: Excel VBA Introduction Part 5 - Selecting Cells (Range, Cells, Activecell, End, Offset). You should also watch: Excel VBA Introduction Part 25 - Arrays
– TinMan
2 days ago












@krishKM - Yes I am searching directly in 6 different databases, however 1 of them is through an excel add in which connects via an excel formula. and the other 2 instances of this loop are for the summary page and the evaluation page.
– Aaron Bates
12 hours ago




@krishKM - Yes I am searching directly in 6 different databases, however 1 of them is through an excel add in which connects via an excel formula. and the other 2 instances of this loop are for the summary page and the evaluation page.
– Aaron Bates
12 hours ago












@TinMan - I will check out those videos, thanks for the suggestion!
– Aaron Bates
12 hours ago




@TinMan - I will check out those videos, thanks for the suggestion!
– Aaron Bates
12 hours ago










2 Answers
2






active

oldest

votes

















up vote
1
down vote













Based on the array video provided by TinMan, I now declared the data as an array, and then populate each tab referencing the array. The code is now instantaneous for 1000s records.



Thanks TinMan!!



'Set MpnQty array

Dim MpnQty() As Variant
Dim Dimension1 As Long, Dimension2 As Long

Sheet1.Activate

Dimension1 = Range("A2", Range("A2").End(xlDown)).Cells.Count - 1
Dimension2 = 1

ReDim MpnQty(0 To Dimension1, 0 To Dimension2)

For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
MpnQty(Dimension1, Dimension2) = Range("A2").Offset(Dimension1, Dimension2).Value
Next Dimension2
Next Dimension1

'Add MPN and Qty to Summary page

Sheet22.Visible = True
Sheet22.Activate

For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
Next Dimension2
Next Dimension





share|improve this answer








New contributor




Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    0
    down vote













    Your answer is a big step up from your original code but we can do better.



    Dimension2 = 1 does no do anything. Dimension2 is being initiated in the For loop.



    Selecting and Activating Objects



    It is rarely necessary to Select or Activate an Object. You should to fully qualify your Objects rather than it is to rely on the default active objects. Fully qualifying references will make your code more robust and easier to debug.




    Sheet22.Visible = True
    Sheet22.Activate



    Above you are temporarily making Sheet22 visible and active when all you need to do is qualify the range.




    For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
    For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
    Sheet22.Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
    Next Dimension2
    Next Dimension



    Consider using a temp variable to shorten the worksheet reference.




    Dim ws1 as Worksheet
    Set ws = Sheet1
    Dimension1 = ws1.Range("A2", ws1.Range("A2").End(xlDown)).Count - 1



    I prefer to use With Blocks




    With Sheet1 
    Dimension1 = .Range("A2", .Range("A2").End(xlDown)).Count - 1
    End With



    Note: I omitted Cells because it is the default property of Range.



    Ranges and Arrays



    Range("A2").Value returns a single scalar value because it contains only one cell. Range("A2:B2").Value is a multiple cell range which returns an array of values that can be directly assigned to a variant variable or another range.



    There are several nuances to resizing ranges, assigning ranges values to variants and assigning arrays to ranges. So practice!!



    Refactored Code



    Here is how I would write it:



    Dim Data() As Variant

    With Sheet1
    Data = .Range("A2", .Range("A2").End(xlDown)).Value
    End With

    With Sheet22
    Data = .Range("A2").Resize(UBound(Data), UBound(Data, 2)).Value = Data
    End With


    Keep in mind that Data = .Range("A2", .Range("A2").End(xlDown)).Value will cause an error if the range returns a single value.






    share|improve this answer





















      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      });
      });
      }, "mathjax-editing");

      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: "196"
      };
      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',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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
      });


      }
      });






      Aaron Bates is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208300%2fsearching-multiple-excel-sheets-for-the-cheapest-item%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      Based on the array video provided by TinMan, I now declared the data as an array, and then populate each tab referencing the array. The code is now instantaneous for 1000s records.



      Thanks TinMan!!



      'Set MpnQty array

      Dim MpnQty() As Variant
      Dim Dimension1 As Long, Dimension2 As Long

      Sheet1.Activate

      Dimension1 = Range("A2", Range("A2").End(xlDown)).Cells.Count - 1
      Dimension2 = 1

      ReDim MpnQty(0 To Dimension1, 0 To Dimension2)

      For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
      For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
      MpnQty(Dimension1, Dimension2) = Range("A2").Offset(Dimension1, Dimension2).Value
      Next Dimension2
      Next Dimension1

      'Add MPN and Qty to Summary page

      Sheet22.Visible = True
      Sheet22.Activate

      For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
      For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
      Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
      Next Dimension2
      Next Dimension





      share|improve this answer








      New contributor




      Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















        up vote
        1
        down vote













        Based on the array video provided by TinMan, I now declared the data as an array, and then populate each tab referencing the array. The code is now instantaneous for 1000s records.



        Thanks TinMan!!



        'Set MpnQty array

        Dim MpnQty() As Variant
        Dim Dimension1 As Long, Dimension2 As Long

        Sheet1.Activate

        Dimension1 = Range("A2", Range("A2").End(xlDown)).Cells.Count - 1
        Dimension2 = 1

        ReDim MpnQty(0 To Dimension1, 0 To Dimension2)

        For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
        For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
        MpnQty(Dimension1, Dimension2) = Range("A2").Offset(Dimension1, Dimension2).Value
        Next Dimension2
        Next Dimension1

        'Add MPN and Qty to Summary page

        Sheet22.Visible = True
        Sheet22.Activate

        For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
        For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
        Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
        Next Dimension2
        Next Dimension





        share|improve this answer








        New contributor




        Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















          up vote
          1
          down vote










          up vote
          1
          down vote









          Based on the array video provided by TinMan, I now declared the data as an array, and then populate each tab referencing the array. The code is now instantaneous for 1000s records.



          Thanks TinMan!!



          'Set MpnQty array

          Dim MpnQty() As Variant
          Dim Dimension1 As Long, Dimension2 As Long

          Sheet1.Activate

          Dimension1 = Range("A2", Range("A2").End(xlDown)).Cells.Count - 1
          Dimension2 = 1

          ReDim MpnQty(0 To Dimension1, 0 To Dimension2)

          For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
          For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
          MpnQty(Dimension1, Dimension2) = Range("A2").Offset(Dimension1, Dimension2).Value
          Next Dimension2
          Next Dimension1

          'Add MPN and Qty to Summary page

          Sheet22.Visible = True
          Sheet22.Activate

          For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
          For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
          Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
          Next Dimension2
          Next Dimension





          share|improve this answer








          New contributor




          Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          Based on the array video provided by TinMan, I now declared the data as an array, and then populate each tab referencing the array. The code is now instantaneous for 1000s records.



          Thanks TinMan!!



          'Set MpnQty array

          Dim MpnQty() As Variant
          Dim Dimension1 As Long, Dimension2 As Long

          Sheet1.Activate

          Dimension1 = Range("A2", Range("A2").End(xlDown)).Cells.Count - 1
          Dimension2 = 1

          ReDim MpnQty(0 To Dimension1, 0 To Dimension2)

          For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
          For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
          MpnQty(Dimension1, Dimension2) = Range("A2").Offset(Dimension1, Dimension2).Value
          Next Dimension2
          Next Dimension1

          'Add MPN and Qty to Summary page

          Sheet22.Visible = True
          Sheet22.Activate

          For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
          For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
          Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
          Next Dimension2
          Next Dimension






          share|improve this answer








          New contributor




          Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 8 hours ago









          Aaron Bates

          111




          111




          New contributor




          Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Aaron Bates is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.
























              up vote
              0
              down vote













              Your answer is a big step up from your original code but we can do better.



              Dimension2 = 1 does no do anything. Dimension2 is being initiated in the For loop.



              Selecting and Activating Objects



              It is rarely necessary to Select or Activate an Object. You should to fully qualify your Objects rather than it is to rely on the default active objects. Fully qualifying references will make your code more robust and easier to debug.




              Sheet22.Visible = True
              Sheet22.Activate



              Above you are temporarily making Sheet22 visible and active when all you need to do is qualify the range.




              For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
              For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
              Sheet22.Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
              Next Dimension2
              Next Dimension



              Consider using a temp variable to shorten the worksheet reference.




              Dim ws1 as Worksheet
              Set ws = Sheet1
              Dimension1 = ws1.Range("A2", ws1.Range("A2").End(xlDown)).Count - 1



              I prefer to use With Blocks




              With Sheet1 
              Dimension1 = .Range("A2", .Range("A2").End(xlDown)).Count - 1
              End With



              Note: I omitted Cells because it is the default property of Range.



              Ranges and Arrays



              Range("A2").Value returns a single scalar value because it contains only one cell. Range("A2:B2").Value is a multiple cell range which returns an array of values that can be directly assigned to a variant variable or another range.



              There are several nuances to resizing ranges, assigning ranges values to variants and assigning arrays to ranges. So practice!!



              Refactored Code



              Here is how I would write it:



              Dim Data() As Variant

              With Sheet1
              Data = .Range("A2", .Range("A2").End(xlDown)).Value
              End With

              With Sheet22
              Data = .Range("A2").Resize(UBound(Data), UBound(Data, 2)).Value = Data
              End With


              Keep in mind that Data = .Range("A2", .Range("A2").End(xlDown)).Value will cause an error if the range returns a single value.






              share|improve this answer

























                up vote
                0
                down vote













                Your answer is a big step up from your original code but we can do better.



                Dimension2 = 1 does no do anything. Dimension2 is being initiated in the For loop.



                Selecting and Activating Objects



                It is rarely necessary to Select or Activate an Object. You should to fully qualify your Objects rather than it is to rely on the default active objects. Fully qualifying references will make your code more robust and easier to debug.




                Sheet22.Visible = True
                Sheet22.Activate



                Above you are temporarily making Sheet22 visible and active when all you need to do is qualify the range.




                For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
                For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
                Sheet22.Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
                Next Dimension2
                Next Dimension



                Consider using a temp variable to shorten the worksheet reference.




                Dim ws1 as Worksheet
                Set ws = Sheet1
                Dimension1 = ws1.Range("A2", ws1.Range("A2").End(xlDown)).Count - 1



                I prefer to use With Blocks




                With Sheet1 
                Dimension1 = .Range("A2", .Range("A2").End(xlDown)).Count - 1
                End With



                Note: I omitted Cells because it is the default property of Range.



                Ranges and Arrays



                Range("A2").Value returns a single scalar value because it contains only one cell. Range("A2:B2").Value is a multiple cell range which returns an array of values that can be directly assigned to a variant variable or another range.



                There are several nuances to resizing ranges, assigning ranges values to variants and assigning arrays to ranges. So practice!!



                Refactored Code



                Here is how I would write it:



                Dim Data() As Variant

                With Sheet1
                Data = .Range("A2", .Range("A2").End(xlDown)).Value
                End With

                With Sheet22
                Data = .Range("A2").Resize(UBound(Data), UBound(Data, 2)).Value = Data
                End With


                Keep in mind that Data = .Range("A2", .Range("A2").End(xlDown)).Value will cause an error if the range returns a single value.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Your answer is a big step up from your original code but we can do better.



                  Dimension2 = 1 does no do anything. Dimension2 is being initiated in the For loop.



                  Selecting and Activating Objects



                  It is rarely necessary to Select or Activate an Object. You should to fully qualify your Objects rather than it is to rely on the default active objects. Fully qualifying references will make your code more robust and easier to debug.




                  Sheet22.Visible = True
                  Sheet22.Activate



                  Above you are temporarily making Sheet22 visible and active when all you need to do is qualify the range.




                  For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
                  For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
                  Sheet22.Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
                  Next Dimension2
                  Next Dimension



                  Consider using a temp variable to shorten the worksheet reference.




                  Dim ws1 as Worksheet
                  Set ws = Sheet1
                  Dimension1 = ws1.Range("A2", ws1.Range("A2").End(xlDown)).Count - 1



                  I prefer to use With Blocks




                  With Sheet1 
                  Dimension1 = .Range("A2", .Range("A2").End(xlDown)).Count - 1
                  End With



                  Note: I omitted Cells because it is the default property of Range.



                  Ranges and Arrays



                  Range("A2").Value returns a single scalar value because it contains only one cell. Range("A2:B2").Value is a multiple cell range which returns an array of values that can be directly assigned to a variant variable or another range.



                  There are several nuances to resizing ranges, assigning ranges values to variants and assigning arrays to ranges. So practice!!



                  Refactored Code



                  Here is how I would write it:



                  Dim Data() As Variant

                  With Sheet1
                  Data = .Range("A2", .Range("A2").End(xlDown)).Value
                  End With

                  With Sheet22
                  Data = .Range("A2").Resize(UBound(Data), UBound(Data, 2)).Value = Data
                  End With


                  Keep in mind that Data = .Range("A2", .Range("A2").End(xlDown)).Value will cause an error if the range returns a single value.






                  share|improve this answer












                  Your answer is a big step up from your original code but we can do better.



                  Dimension2 = 1 does no do anything. Dimension2 is being initiated in the For loop.



                  Selecting and Activating Objects



                  It is rarely necessary to Select or Activate an Object. You should to fully qualify your Objects rather than it is to rely on the default active objects. Fully qualifying references will make your code more robust and easier to debug.




                  Sheet22.Visible = True
                  Sheet22.Activate



                  Above you are temporarily making Sheet22 visible and active when all you need to do is qualify the range.




                  For Dimension1 = LBound(MpnQty, 1) To UBound(MpnQty, 1)
                  For Dimension2 = LBound(MpnQty, 2) To UBound(MpnQty, 2)
                  Sheet22.Range("A2").Offset(Dimension1, Dimension2).Value = MpnQty(Dimension1, Dimension2)
                  Next Dimension2
                  Next Dimension



                  Consider using a temp variable to shorten the worksheet reference.




                  Dim ws1 as Worksheet
                  Set ws = Sheet1
                  Dimension1 = ws1.Range("A2", ws1.Range("A2").End(xlDown)).Count - 1



                  I prefer to use With Blocks




                  With Sheet1 
                  Dimension1 = .Range("A2", .Range("A2").End(xlDown)).Count - 1
                  End With



                  Note: I omitted Cells because it is the default property of Range.



                  Ranges and Arrays



                  Range("A2").Value returns a single scalar value because it contains only one cell. Range("A2:B2").Value is a multiple cell range which returns an array of values that can be directly assigned to a variant variable or another range.



                  There are several nuances to resizing ranges, assigning ranges values to variants and assigning arrays to ranges. So practice!!



                  Refactored Code



                  Here is how I would write it:



                  Dim Data() As Variant

                  With Sheet1
                  Data = .Range("A2", .Range("A2").End(xlDown)).Value
                  End With

                  With Sheet22
                  Data = .Range("A2").Resize(UBound(Data), UBound(Data, 2)).Value = Data
                  End With


                  Keep in mind that Data = .Range("A2", .Range("A2").End(xlDown)).Value will cause an error if the range returns a single value.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  TinMan

                  92519




                  92519






















                      Aaron Bates is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Aaron Bates is a new contributor. Be nice, and check out our Code of Conduct.













                      Aaron Bates is a new contributor. Be nice, and check out our Code of Conduct.












                      Aaron Bates is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208300%2fsearching-multiple-excel-sheets-for-the-cheapest-item%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







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin