# Help with visual basic code for my data

#### wesrockin

##### New Member
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.
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 =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

#### Ahnold

##### Well-known Member
A pivot table will definitly give you the results you are looking for. Will that work for you?

#### wesrockin

##### New Member
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!!!

#### Ahnold

##### Well-known Member
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")
Set myField = myTable.PivotFields("bleed")
myField.PivotItems("(blank)").Visible = False
myField.Orientation = xlDataField
myField.Calculation = xlPercentOfRow
End Sub``````

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

1,168,025
Messages
5,856,890
Members
431,837
Latest member
megantang

### 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.

### Which adblocker are you using?

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

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