Breaking up a Range in 2 Cells with Variable number length.

Bytoro

New Member
Joined
Mar 4, 2023
Messages
3
Office Version
  1. 365
Hello,
I am trying to create a VB button with the below output. I would like to break up the range of numbers located in 2 cells with the name of 1 for each. As you can see, some might not be a range just 2 of the same numbers, indicating just one product id. 1 digit is the minimum and 9 digits is the max for each product id.


With the help of some Mr. Excel Dueling videos, i was able to make it work for a range with exact character counts but for a small protion of the data it is not flexbible enough to handle smaller ranges.


Thank you for any assistance! This is a great resource and community!

Book1.xlsx
ABCDEFGHIJKLMNOPQ
1NameNUNUNUNUNUProduct ID StartProduct ID ENDNameIndividual ID
2Item 1xxxxx101035499101035499Item 1101035499
3Item 1xxxxx101026465101026478Item 1101026465
4Item 2xxxxx10041006Item 1101026466
5Item 3xxxxx5502355023Item 1101026467
6Item 3xxxxx5452654828Item 1101026468
7Item 3xxxxx101026031101026032Item 21004
8Item 21005
9Item 21006
10Item 355023
11Item 354526
12Item 3101026031
13Item 3101026032
Sheet1
 
Last edited by a moderator:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Like this?
VBA Code:
Sub test()
  Dim length As Long, myArr As Variant, tmp As Variant, lRow As Long
  lRow = Cells(Rows.Count, 7).End(xlUp).Row
  tmp = Range("A2:H" & lRow)
  For i = 1 To UBound(tmp)
    length = length + tmp(i, 8) - tmp(i, 7) + 1
  Next
  ReDim myArr(1 To length, 1 To 2)
  lRow = 1
  For i = 1 To UBound(tmp)
    For j = 1 To tmp(i, 8) - tmp(i, 7) + 1
      myArr(lRow, 1) = tmp(i, 1)
      myArr(lRow, 2) = tmp(i, 7) + (j - 1)
      lRow = lRow + 1
    Next
  Next
  Range("P2").Resize(length, 2) = myArr
End Sub
 
Last edited by a moderator:
Upvote 1
Solution
Wow such a fast response! Thanks! The data set it about 6000 records and when i run it for the complete data set, i get "out of memory." I ran it for 1500 of them and it worked great. I got excited!. I am trying to see if this is a record number limitation or something with the data set. i can keep you posted if i find anything.
As you know, it loops all 6000 records for the first "for" statement records. I may be able to edit the range and just create 5 buttons. One for Each range.

I have done all the obvious things, closed programs, restarted. verified in task manager that i have sufficient memory. 32gb ram. i couldnt determine if cache needed to be cleared.
 
Upvote 0
Maybe..

Also there is a probabilty that the number of inserted rows may exceed the total number of rows in excel. It would be better to process in batches.
 
Upvote 0
I found a few errors in the data set and was able to run the full thing. One had a range of about 9 million. Thanks so much, this a big win for my day to day! Calling it solved.
 
Upvote 0
This may recude the calculation time insignificantly :)
VBA Code:
    For j = 0 To tmp(i, 8) - tmp(i, 7)
      myArr(lRow, 1) = tmp(i, 1)
      myArr(lRow, 2) = tmp(i, 7) + j
      lRow = lRow + 1
    Next
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,802
Members
449,095
Latest member
m_smith_solihull

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