Solved

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

Posted on 2016-10-03
3
44 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 80

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 48

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now