Solved

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

Posted on 2016-10-03
3
81 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 100 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 51

Accepted Solution

by:
Rgonzo1971 earned 400 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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…

717 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