Creating many rows with binomial data

kajO

New Member
Joined
Oct 23, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a file that consists of thousands of rows of beehive data. It is currently set up so that each apiary owner has their own row that includes the number of hives they own and what percentage didn't survive. I need this data to be binomial. For instance, in the first row of the attached image, I would have 8 rows with ones for lost and 54 rows with zeros for alive. I need to find a way to avoid doing this all manually as it would likely take hundreds of hours but I don't have a clue where to start.
 

Attachments

  • Untitled.png
    Untitled.png
    19.5 KB · Views: 9
another approach
WinterAtRiskWinterLostWinterAliveWinterLOSSAlive/Lost
6285412.9%Alive 0 (54) / Lost 1 (8)
3659027524.7%Alive 0 (275) / Lost 1 (90)
6008052013.3%Alive 0 (520) / Lost 1 (80)
14007013305.0%Alive 0 (1330) / Lost 1 (70)
2102118910.0%Alive 0 (189) / Lost 1 (21)

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"WinterAtRisk", Int64.Type}, {"WinterLost", Int64.Type}, {"WinterAlive", Int64.Type}, {"WinterLOSS", Percentage.Type}}),
    AliveLost = Table.AddColumn(Type, "Alive/Lost", each Text.Format(" Alive #[alive] (#[alivet]) / Lost #[lost] (#[lostt])", [alive = 0, alivet = [WinterAtRisk]-[WinterLost], lost = 1, lostt = [WinterLost]]))
in
    AliveLost
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Noticed that the variables Ars and Ct are redundant. Ars can be removed. Replace the code in post #10 designed to handle overflow with the code below :
VBA Code:
Sub WriteBinomials_1()
'Assumptions: this macro will be run with the raw data sheet active
'The raw data w/headers will be in BV1:BY? with no other entries below the raw data in col BV
'Output will be written to a new sheet "Output" in cols A:E starting in A2
'output exceeding BlockCount rows will be redirected to cols G:K, M:Q, ...
Const BlockCount As Long = 1000000  'Number of rows in a block of output - not to exceed rows.count-1
Dim Rall As Range, Vall As Variant
Dim R() As Range, Rin As Range, Vin As Variant, Vout As Variant, dataSht As Worksheet, outputSht As Worksheet
Dim i As Long, m As Long, j As Long, k As Long, First As Long, Last As Long, Ct As Long
Set dataSht = ActiveSheet
Set Rall = dataSht.Range("BV2:BY" & dataSht.Cells(Rows.Count, "BV").End(xlUp).Row)
Vall = Rall.Value
If Val(Vall(1, 1)) > BlockCount Then
    MsgBox "WinterAtRisk values must be <= " & BlockCount
    Exit Sub
End If
For i = 1 To UBound(Vall, 1)
    S = S + Val(Vall(i, 1))
    If S > BlockCount Then
        First = Last + 1
        Last = i - 1
        Ct = Ct + 1
        ReDim Preserve R(1 To Ct)
        Set R(Ct) = dataSht.Range(Rall.Rows(First), Rall.Rows(Last))
        S = Val(Vall(i, 1))
    ElseIf i = UBound(Vall, 1) Then
        First = Last + 1
        Last = i
        Ct = Ct + 1
        ReDim Preserve R(1 To Ct)
        Set R(Ct) = dataSht.Range(Rall.Rows(First), Rall.Rows(Last))
    End If
Next i
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With
On Error Resume Next
Sheets("Output").Delete
On Error GoTo 0
Set outputSht = Sheets.Add
outputSht.Name = "Output"
    For i = 1 To Ct
        With outputSht.Range("A:E").Offset(0, (i - 1) * 6).Resize(1, 5)
            .ClearContents
            .Rows(1).Value = Array("Alive/Lost", "WinterAtRisk", "WinterLost", "WinterAlive", "WinterLOSS")
            .EntireColumn.AutoFit
        End With
    Next i
For i = 1 To Ct
    Set Rin = R(i)
    Vin = Rin.Value
    For m = 1 To UBound(Vin, 1)
        ReDim Vout(1 To Vin(m, 1), 1 To UBound(Vin, 2) + 1)
        For j = 1 To UBound(Vout, 1)
            If j <= Vin(m, 2) Then
                Vout(j, 1) = 1
                For k = 2 To 5
                    Vout(j, k) = Vin(m, k - 1)
                Next k
            Else
                Vout(j, 1) = 0
                For k = 2 To 5
                    Vout(j, k) = Vin(m, k - 1)
                Next k
            End If
        Next j
    outputSht.Cells(Rows.Count, 1 + (i - 1) * 6).End(xlUp).Offset(1).Resize(UBound(Vout, 1), 5).Value = Vout
    Erase Vout
    Next m
Next i
With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
End With
End Sub
 
Upvote 0
Solution
Thanks both of you, this has been incredibly helpful!
 
Upvote 0
Thanks both of you, this has been incredibly helpful!
You are welcome. Glad we could help and thanks for the reply. I'm curious, have you used the code from post #12 on your "file that consists of thousands of rows of beehive data" with success?
 
Upvote 0
You are welcome. Glad we could help and thanks for the reply. I'm curious, have you used the code from post #12 on your "file that consists of thousands of rows of beehive data" with success?
I have and everything seems to be in order
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

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