Repeated result based on counter and condition formula

moks844

New Member
Joined
Mar 15, 2016
Messages
14
Hi

I use 2 tables, one for entering data and another one for hyperlinking external files based on the data in table No.1

I will show the tables then follow it with the question.

Table 1
No.DescriptionFromToDateAttachment Desc.No. of Attach
33ExcavationsContr1OwnerEmployment contract.pdf
Bill of quantitaty
2
34MechanicalContr2OwnerBOQ.xlsx
Shop Drawings.dwg
Work plan.xlsx
3
35ElectricalContr2OwnerBOQ.xlsx
Shop Drawings.dwg
Work plan.xlsx
3

<tbody>
</tbody>

Table 2
No. from Table1DescriptionFile nameExtensionHyperlink

<tbody>
</tbody>

Now I'm looking for a formula or Macro so in Table 2 / column A the value will represent the first column in table 1 (Column A) and repeated as counted in the Table 1 / No. of attachments Column (Column G)

I hope I could explain my question looking to have your helps.
Thanks.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You could put the expected result and explain how you got to the result.
 
Upvote 0
You could put the expected result and explain how you got to the result.
OK what I want is that Table 2 have the following results:
No. from Table 1DescriptionFile nameExtensionHyperlink
33Employment contractpdf
33Bill of quantitatyxlsx
34BOQxlsx
34Shop Drawingsdwg
34Work planxlsx
35BOQxlsx
35Shop Drawingsdwg
35Work planxlsx

<tbody>
</tbody>
 
Upvote 0
The hyperlink is directed to the same folder where you put the file with the macro.
I suppose that the names of the files are separated by an enter (chr 10) and that all the files have an extension.

This will be the result.


<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:155.88px;" /><col style="width:126.42px;" /><col style="width:173.94px;" /><col style="width:80.79px;" /><col style="width:255.68px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td><td >C</td><td >D</td><td >E</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >No. from Table 1</td><td >Description</td><td >File name</td><td >Extension</td><td >Hyperlink</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="color:#0000ff; text-decoration:underline; text-align:right; ">33</td><td >Excavations</td><td >Employment contract</td><td >pdf</td><td style="color:#0000ff; text-decoration:underline; ">Employment contract.pdf</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:right; ">33</td><td >Excavations</td><td >Bill of quantitaty</td><td >xlsx</td><td style="color:#0000ff; text-decoration:underline; ">Bill of quantitaty.xlsx</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="text-align:right; ">34</td><td >Mechanical</td><td >BOQ</td><td >xlsx</td><td style="color:#0000ff; text-decoration:underline; ">BOQ.xlsx</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:right; ">34</td><td >Mechanical</td><td >Shop Drawings</td><td >dwg</td><td style="color:#0000ff; text-decoration:underline; ">Shop Drawings.dwg</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td style="text-align:right; ">34</td><td >Mechanical</td><td >Work plan</td><td >xlsx</td><td style="color:#0000ff; text-decoration:underline; ">Work plan.xlsx</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="text-align:right; ">35</td><td >Electrical</td><td >BOQ</td><td >xlsx</td><td style="color:#0000ff; text-decoration:underline; ">BOQ.xlsx</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td style="text-align:right; ">35</td><td >Electrical</td><td >Shop Drawings</td><td >dwg</td><td style="color:#0000ff; text-decoration:underline; ">Shop Drawings.dwg</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td style="text-align:right; ">35</td><td >Electrical</td><td >Work plan</td><td >xlsx</td><td style="color:#0000ff; text-decoration:underline; ">Work plan.xlsx</td></tr></table>
Code:
Sub Repeated_result()
    Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, lr As Long, wPath As String
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    sh2.Rows("2:" & Rows.Count).ClearContents
    wPath = ThisWorkbook.Path & "\"
    
    For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
        For Each f In Split(c.Offset(, 5), Chr(10))
            lr = Range("A" & Rows.Count).End(xlUp).Row + 1
            sh2.Range("A" & lr).Value = c.Value
            sh2.Range("B" & lr).Value = c.Offset(, 1).Value
            sh2.Range("C" & lr).Value = Left(f, InStrRev(f, ".") - 1)
            sh2.Range("D" & lr).Value = Mid(f, InStrRev(f, ".") + 1)
            sh2.Range("E" & lr).Value = f
            sh2.Hyperlinks.Add Anchor:=sh2.Range("E" & lr), Address:=wPath & f
        Next
    Next
End Sub
 
Upvote 0
The hyperlink is directed to the same folder where you put the file with the macro.
I suppose that the names of the files are separated by an enter (chr 10) and that all the files have an extension.

This will be the result.


ABCDE
1No. from Table 1DescriptionFile nameExtensionHyperlink
233ExcavationsEmployment contractpdfEmployment contract.pdf
333ExcavationsBill of quantitatyxlsxBill of quantitaty.xlsx
434MechanicalBOQxlsxBOQ.xlsx
534MechanicalShop DrawingsdwgShop Drawings.dwg
634MechanicalWork planxlsxWork plan.xlsx
735ElectricalBOQxlsxBOQ.xlsx
835ElectricalShop DrawingsdwgShop Drawings.dwg
935ElectricalWork planxlsxWork plan.xlsx

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:155.88px;"><col style="width:126.42px;"><col style="width:173.94px;"><col style="width:80.79px;"><col style="width:255.68px;"></colgroup><tbody>
</tbody>

Code:
Sub Repeated_result()
    Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, lr As Long, wPath As String
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    sh2.Rows("2:" & Rows.Count).ClearContents
    wPath = ThisWorkbook.Path & "\"
    
    For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
        For Each f In Split(c.Offset(, 5), Chr(10))
            lr = Range("A" & Rows.Count).End(xlUp).Row + 1
            sh2.Range("A" & lr).Value = c.Value
            sh2.Range("B" & lr).Value = c.Offset(, 1).Value
            sh2.Range("C" & lr).Value = Left(f, InStrRev(f, ".") - 1)
            sh2.Range("D" & lr).Value = Mid(f, InStrRev(f, ".") + 1)
            sh2.Range("E" & lr).Value = f
            sh2.Hyperlinks.Add Anchor:=sh2.Range("E" & lr), Address:=wPath & f
        Next
    Next
End Sub

I thank you very much for your great efforts.
Though I eventually guided into different approach using formulas. formulas could be better in understanding more than macros, thus just to spread the info, I found my solution in this article:
https://www.extendoffice.com/documents/excel/1897-excel-repeat-cell-value-x-times.html
 
Upvote 0
I am pleased to know that you found a solution, now you have macro and formula. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top