Help with visual basic code for my data

wesrockin

New Member
Joined
Feb 21, 2004
Messages
18
VB help please....
in the follow in spreadsheet I would like to have an inection level expressed as a percent of trees which have a 2 in the bleed column. one of the tricky things is that some nodes have less that 4 trees. So I would need to divide by 2 or 3 in those cases instead of 4 . So the bleed column is eith 1, 2 or blank and there are always 4 rows for each node. I need one infection level to coorespond to the each numbered node in column B.
If possible I would like to write it in visual basic instead of a complicated if/then statement.
Thanks for any help you can provide.
fuel_worksheets_2_23_04_perm2.xls
ABCDEFGHIJ
1transectnodeidtreespeciesdist(m)dbh1(in)dbh2(in)bleedinfection
21111193clo18.991
31119532.912.51
411198bo10.723.82
511200bo722.51
61221191bo9.281
711190clo7392.9
811189clo15.38.5
911192bo1.76.61
101331188bo1.711.42
1111187bo1.73.11
1211186clo5.1222
1311185bo10.88.11
141441181clo15.69.81
1511182clo9.37.21
1611183bo9.78.51
1711184clo8.27.11
Sheet2
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
A pivot table will definitly give you the results you are looking for. Will that work for you?
 
Upvote 0
I will have to learn how to use a pivot table ... thanks for the tip... I am also curious how you might write a macro for that.

Thanks!!!
 
Upvote 0
I use this site to practise code writing. Let me know if this works for you and if not what modifications need to be made.

Code:
Sub Macro1()
    Dim myTable As PivotTable
    Dim myField As PivotField
    Dim myRange As Range
    On Error Resume Next
    With Range("A2", Range("A2").End(xlDown)).Offset(0, 1).Resize(, 2)
        .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
        .Copy
        .PasteSpecial xlPasteValues
    End With
    Application.CutCopyMode = False
    Err.Clear
    Set myRange = Range("A2").CurrentRegion
    ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
        myRange, TableDestination:=Range("L1"), TableName:= _
        "PivotTable1"
    Set myTable = ActiveSheet.PivotTables("PivotTable1")
    myTable.AddFields "node", "bleed", "transect"
    Set myField = myTable.PivotFields("bleed")
    myField.PivotItems("(blank)").Visible = False
    myField.Orientation = xlDataField
    myField.Calculation = xlPercentOfRow
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,943
Messages
6,175,547
Members
452,652
Latest member
eduedu

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