Forum Discussion
Alexgreggio
Feb 26, 2023Copper Contributor
Populating a table based on a specific set of intervals and structure
Hello, I need some help with populating a table in a specific way. My input will be an array composed of Hole ID (Name of drill hole), the depth of each drill hole and then the depth of the senso...
- Feb 26, 2023
A macro solution:
Sub CreateList() Dim ws As Worksheet Dim wt As Worksheet Dim r As Long Dim m As Long Dim c As Long Dim n As Long Dim l As Double Dim t As Long Dim h As String Dim d As Double Dim s As Double Dim x As Double Application.ScreenUpdating = False Set ws = ActiveSheet m = ws.Cells(1, 1).End(xlDown).Row n = ws.Cells(1, 1).End(xlToRight).Column l = ws.Cells(2, 8).Value / 2 Set wt = Worksheets.Add(After:=ws) For r = 2 To m h = ws.Cells(r, 1).Value d = ws.Cells(r, 2).Value x = 0 c = 3 Do While ws.Cells(r, c).Value <> "" s = ws.Cells(r, c).Value t = t + 1 wt.Cells(t, 1).Value = h wt.Cells(t, 2).Value = x wt.Cells(t, 3).Value = s - l t = t + 1 wt.Cells(t, 1).Value = h wt.Cells(t, 2).Value = s - l wt.Cells(t, 3).Value = s + l wt.Cells(t, 4).Value = ws.Cells(1, c).Value x = s + l c = c + 1 Loop If d > x Then t = t + 1 wt.Cells(t, 1).Value = h wt.Cells(t, 2).Value = x wt.Cells(t, 3).Value = d End If Next r Application.ScreenUpdating = True End Sub
HansVogelaar
Feb 26, 2023MVP
A macro solution:
Sub CreateList()
Dim ws As Worksheet
Dim wt As Worksheet
Dim r As Long
Dim m As Long
Dim c As Long
Dim n As Long
Dim l As Double
Dim t As Long
Dim h As String
Dim d As Double
Dim s As Double
Dim x As Double
Application.ScreenUpdating = False
Set ws = ActiveSheet
m = ws.Cells(1, 1).End(xlDown).Row
n = ws.Cells(1, 1).End(xlToRight).Column
l = ws.Cells(2, 8).Value / 2
Set wt = Worksheets.Add(After:=ws)
For r = 2 To m
h = ws.Cells(r, 1).Value
d = ws.Cells(r, 2).Value
x = 0
c = 3
Do While ws.Cells(r, c).Value <> ""
s = ws.Cells(r, c).Value
t = t + 1
wt.Cells(t, 1).Value = h
wt.Cells(t, 2).Value = x
wt.Cells(t, 3).Value = s - l
t = t + 1
wt.Cells(t, 1).Value = h
wt.Cells(t, 2).Value = s - l
wt.Cells(t, 3).Value = s + l
wt.Cells(t, 4).Value = ws.Cells(1, c).Value
x = s + l
c = c + 1
Loop
If d > x Then
t = t + 1
wt.Cells(t, 1).Value = h
wt.Cells(t, 2).Value = x
wt.Cells(t, 3).Value = d
End If
Next r
Application.ScreenUpdating = True
End Sub
- AlexgreggioFeb 26, 2023Copper ContributorThank you Hans!!
I have tried it and it works like a charm! I might a few questions as I play along with it but at the moment it does exactly what I wanted! Thank you so much!!