Solved

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

Posted on 2016-10-03
3
66 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
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 50

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

856 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