Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
85 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 53

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

636 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