?
Solved

Number Range Field in Excel Expanded into identical rows for the length of the number Range

Posted on 2016-10-03
3
Medium Priority
?
83 Views
Last Modified: 2016-10-04
I have been doing this manually on a spreadsheet at my supervisors orders, but i was thinking this could possibly be done in a macro. Unfortunately I could not come close to figuring it out. In the first sheet in the workbook in ROW H there is a number range which represents the range of Strands that have the information in the remaining fields. What I was tasked to do is duplicate the rows based on the amounts in the number range. What my supervisor wants to see is on the second Sheet in the Workbook. Each Row is duplicated based on the amount of numbers in the range in column H. I hope I was able to explain it sufficiently.
CableTable_LEC3_working.xlsm
0
Comment
Question by:GravitaZ24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 81

Assisted Solution

by:byundt
byundt earned 400 total points
ID: 41827451
You have a protected worksheet and a protected workbook structure. As a result, a macro won't be able to modify your worksheet--unless you post a password.

Assuming that you unprotect your worksheet, and that the number after the dash in column H represents the number of row copies, here is a macro to do the duplication. The macro assumes that a number without a dash in column H also represents the number of row copies you want.

As written, the code converts the active worksheet (Starting point) into one that resembles Results in the attached workbook.
Sub RowDuper()
Dim rg As Range
Dim s As String
Dim i As Long, ii As Long, n As Long, nn As Long
Dim v As Variant
Application.ScreenUpdating = False
Set rg = Range("A1").CurrentRegion
n = rg.Rows.Count
For i = n To 2 Step -1
    s = rg.Cells(i, "H").Value
    If s <> "" Then
        nn = Split(s & "-" & s, "-")(1)
        If nn > 1 Then
            rg.Rows(i + 1).Resize(nn - 1, 1).EntireRow.Insert
            For ii = 2 To nn
                rg.Rows(i + ii - 1).Value = rg.Rows(i).Value
            Next
        End If
    End If
Next
End Sub

Open in new window

RowDuperQ28974104.xlsm
0
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 1600 total points
ID: 41827484
Hi,

pls try
Sub RowDuplicates()
Dim rg As Range
Dim s As String
Dim BgnN, EndN
Dim i As Long, n As Long, nn As Long
Dim v As Variant
Application.ScreenUpdating = False
Set rg = Range("A1").CurrentRegion
n = rg.Rows.Count
For i = n To 2 Step -1
    s = rg.Cells(i, "H").Value
    If s Like "*-*" Then
        BgnN = Split(s, "-")(0)
        EndN = Split(s, "-")(1)
        nn = EndN - BgnN
        rg.Rows(i + 1).Resize(nn, 1).EntireRow.Insert
        rg.Rows(i + 1).Resize(nn).Value = rg.Rows(i).Value
    End If
Next
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:GravitaZ24
ID: 41828987
Thank you! Rgonzo's worked exactly the way I wanted! I almost had byundt's solution working after tweaking it a bit.
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question