codexsampler
New Member
- Joined
- Nov 7, 2011
- Messages
- 8
Hello,
The macro I am using for Excel 2007 moves the column Oranges next to Kiwis based on the name Oranges and Kiwis found in Row1. The problem I have is that there are countif statements that need to be able to work no matter the location the Oranges and Kiwis columns are.
At this point in the macro an inserted row has been made between the types of fruit and the random number row. Is there a way that the countif range can stop at this separator so as not to cause circular referencing?
Below is the section of code that I tried to modify. (Arrow is start of my attempt at the countif modification.)
Excel 2007
Excel 2007
Excel 2007
Thanks for your help,
codexsampler
The macro I am using for Excel 2007 moves the column Oranges next to Kiwis based on the name Oranges and Kiwis found in Row1. The problem I have is that there are countif statements that need to be able to work no matter the location the Oranges and Kiwis columns are.
At this point in the macro an inserted row has been made between the types of fruit and the random number row. Is there a way that the countif range can stop at this separator so as not to cause circular referencing?
Below is the section of code that I tried to modify. (Arrow is start of my attempt at the countif modification.)
Code:
Range("A1").Select
ActiveCell.Select
Selection.AutoFilter
With ActiveWindow
.SplitColumn = 1
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
ActiveSheet.UsedRange.Columns.AutoFit
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
ActiveCell.EntireRow.Insert Shift:=xlDown
Dim s As Long, t As Long
s = 0: t = 0
On Error Resume Next
s = Application.Match("Oranges", Rows(1), 0)
t = Application.Match("Kiwis", Rows(1), 0)
On Error GoTo 0
If t = 0 Or s = 0 Then
MsgBox "Either/or both 'Oranges' and/or 'Kiwis' do not exist - macro terminated!"
Exit Sub
End If
Application.ScreenUpdating = False
If s = t - 1 Then
'do nothing
Else
Columns(t).Insert
Columns(t).Value = Columns(s).Value
Columns(s).Delete
End If
Application.ScreenUpdating = True
Range("A1").Select
Cells.Find(What:="Status", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.End(xlDown).Select
ActiveCell.Offset(7, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "Naval"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "Golden"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "Percent"
ActiveCell.Offset(1, -2).Range("A1").Select
' ----> Start of countif statements
Dim x As Long, u As Long, v As Long, a As Long, b As Long, p As Long, c As Long
a = 0: b = 0: u = 0: v = 0
On Error Resume Next
a = Application.Match("Oranges", Rows(1))
b = Application.Match("Kiwis", Rows(1))
p = Columns(a).End(x1Down).Row
v = Columns(b).End(x1Down).Row
x = Range("p" & Rows.Count).End(xlUp).Row
If x< 1 Then x = 1
ActiveCell.Formula = Application.WorksheetFunction.CountIf(Range("p" & x), "Naval") - 1
ActiveCell.Offset(0, 1).Range("A1").Select
x = Range("v" & Rows.Count).End(xlUp).Row
If x< 1 Then x = 1
ActiveCell.Formula = Application.WorksheetFunction.CountIf(Range("v" & x), "Golden") - 1
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-1]"
ActiveCell.Offset(0, 0).Select
Selection.NumberFormat = "0.00%"
Excel Workbook | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | # | Apples | Tangerines | Bananas | Oranges | Kiwis | Cherries | Grapes | ||
2 | 1 | Granny Smith | Minneola | Manzano | Blood | Golden | Lambert | Bronx | ||
3 | 2 | Red Delicious | Fairchild | Red | Naval | Green | Montmorency | Concord | ||
4 | 3 | Braeburn | Algerian | Plantain | Valencia | Red | Rainier | Emperor | ||
5 | 4 | Honeycrisp | Clementine | Cavendish | Cara Cara | Berry | Royal Ann | Perlette | ||
6 | 5 | Red Delicious | Minneola | Red | Naval | Green | Montmorency | Concord | ||
7 | 6 | Red Delicious | Clementine | Cavendish | Blood | Red | Rainier | Emperor | ||
8 | 7 | Granny Smith | Algerian | Plantain | Naval | Berry | Lambert | Bronx | ||
9 | 8 | Red Delicious | Fairchild | Manzano | Valencia | Golden | Royal Ann | Perlette | ||
10 | 9 | Braeburn | Clementine | Cavendish | Blood | Green | Rainier | Bronx | ||
11 | 10 | Granny Smith | Algerian | Red | Cara Cara | Berry | Montmorency | Concord | ||
12 | 11 | Braeburn | Minneola | Plantain | Valencia | Red | Rainier | Emperor | ||
13 | 12 | Red Delicious | Fairchild | Cavendish | Blood | Golden | Lambert | Perlette | ||
14 | 13 | Honeycrisp | Minneola | Manzano | Cara Cara | Berry | Royal Ann | Bronx | ||
15 | 14 | Granny Smith | Clementine | Red | Naval | Golden | Montmorency | Concord | ||
16 | 15 | Red Delicious | Fairchild | Manzano | Cara Cara | Red | Royal Ann | Perlette | ||
17 | 16 | Honeycrisp | Algerian | Plantain | Valencia | Green | Lambert | Emperor | ||
18 | 17 | Honeycrisp | Minneola | Plantain | Blood | Berry | Montmorency | Bronx | ||
19 | 18 | Granny Smith | Clementine | Cavendish | Cara Cara | Golden | Rainier | Concord | ||
20 | 19 | Red Delicious | Fairchild | Manzano | Cara Cara | Green | Lambert | Emperor | ||
21 | 20 | Granny Smith | Minneola | Red | Blood | Berry | Rainier | Perlette | ||
22 | 21 | Granny Smith | Algerian | Manzano | Valencia | Golden | Royal Ann | Bronx | ||
23 | 22 | Braeburn | Minneola | Cavendish | Naval | Red | Montmorency | Concord | ||
24 | ||||||||||
25 | 51 | 65 | 87 | 21 | 56 | 48 | 49 | |||
26 | 211 | 596 | 485 | 700 | 215 | 65 | 87 | |||
27 | ||||||||||
28 | Naval | Golden | Percent | |||||||
29 | 5 | 6 | 83.33% | |||||||
Sheet1 the number of Naval in Oranges is counted and the Number of Golden in Kiwis is counted. |
Excel 2007
Cell Formulas | ||
---|---|---|
Range | Formula | |
E29 | =COUNTIF(E1:E23, "Naval") | |
F29 | =COUNTIF(F1:F23, "Golden") | |
G29 | =E29/F29 |
Excel Workbook | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | # | Apples | Bananas | Oranges | Kiwis | Cherries | Grapes | ||
2 | 1 | Granny Smith | Manzano | Blood | Golden | Lambert | Bronx | ||
3 | 2 | Red Delicious | Red | Naval | Green | Montmorency | Concord | ||
4 | 3 | Braeburn | Plantain | Valencia | Red | Rainier | Emperor | ||
5 | 4 | Honeycrisp | Cavendish | Cara Cara | Berry | Royal Ann | Perlette | ||
6 | 5 | Red Delicious | Red | Naval | Green | Montmorency | Concord | ||
7 | 6 | Red Delicious | Cavendish | Blood | Red | Rainier | Emperor | ||
8 | 7 | Granny Smith | Plantain | Naval | Berry | Lambert | Bronx | ||
9 | 8 | Red Delicious | Manzano | Valencia | Golden | Royal Ann | Perlette | ||
10 | 9 | Braeburn | Cavendish | Blood | Green | Rainier | Bronx | ||
11 | 10 | Granny Smith | Red | Cara Cara | Berry | Montmorency | Concord | ||
12 | 11 | Braeburn | Plantain | Valencia | Red | Rainier | Emperor | ||
13 | 12 | Red Delicious | Cavendish | Blood | Golden | Lambert | Perlette | ||
14 | 13 | Honeycrisp | Manzano | Cara Cara | Berry | Royal Ann | Bronx | ||
15 | 14 | Granny Smith | Red | Naval | Golden | Montmorency | Concord | ||
16 | 15 | Red Delicious | Manzano | Cara Cara | Red | Royal Ann | Perlette | ||
17 | 16 | Honeycrisp | Plantain | Valencia | Green | Lambert | Emperor | ||
18 | 17 | Honeycrisp | Plantain | Blood | Berry | Montmorency | Bronx | ||
19 | 18 | Granny Smith | Cavendish | Cara Cara | Golden | Rainier | Concord | ||
20 | 19 | Red Delicious | Manzano | Cara Cara | Green | Lambert | Emperor | ||
21 | 20 | Granny Smith | Red | Blood | Berry | Rainier | Perlette | ||
22 | 21 | Granny Smith | Manzano | Valencia | Golden | Royal Ann | Bronx | ||
23 | 22 | Braeburn | Cavendish | Naval | Red | Montmorency | Concord | ||
24 | |||||||||
25 | 51 | 87 | 21 | 56 | 48 | 49 | |||
26 | 211 | 485 | 700 | 215 | 65 | 87 | |||
27 | |||||||||
28 | Naval | Golden | Percent | ||||||
29 | 5 | 6 | 83.33% | ||||||
Sheet2 Oranges and Kiwis are one to left. |
Cell Formulas | ||
---|---|---|
Range | Formula | |
D29 | =COUNTIF(D1:D23, "Naval") | |
E29 | =COUNTIF(E1:E23, "Golden") | |
F29 | =D29/E29 |
Excel Workbook | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | |||
1 | # | Apples | Pumpkins | Cherries | Tangerines | Bananas | Oranges | Kiwis | Grapes | ||
2 | 1 | Granny Smith | Howden | Lambert | Minneola | Manzano | Blood | Golden | Bronx | ||
3 | 2 | Red Delicious | Kakai | Montmorency | Fairchild | Red | Naval | Green | Concord | ||
4 | 3 | Braeburn | Valenciano | Rainier | Algerian | Plantain | Valencia | Red | Emperor | ||
5 | 4 | Honeycrisp | Lumina | Royal Ann | Clementine | Cavendish | Cara Cara | Berry | Perlette | ||
6 | 5 | Red Delicious | Kakai | Montmorency | Minneola | Red | Naval | Green | Concord | ||
7 | 6 | Red Delicious | Lumina | Rainier | Clementine | Cavendish | Blood | Red | Emperor | ||
8 | 7 | Granny Smith | Howden | Lambert | Algerian | Plantain | Naval | Berry | Bronx | ||
9 | 8 | Red Delicious | Valenciano | Royal Ann | Fairchild | Manzano | Valencia | Golden | Perlette | ||
10 | 9 | Braeburn | Kakai | Rainier | Clementine | Cavendish | Blood | Green | Bronx | ||
11 | 10 | Granny Smith | Lumina | Montmorency | Algerian | Red | Cara Cara | Berry | Concord | ||
12 | 11 | Braeburn | Howden | Rainier | Minneola | Plantain | Valencia | Red | Emperor | ||
13 | 12 | Red Delicious | Kakai | Lambert | Fairchild | Cavendish | Blood | Golden | Perlette | ||
14 | 13 | Honeycrisp | Valenciano | Royal Ann | Minneola | Manzano | Cara Cara | Berry | Bronx | ||
15 | 14 | Granny Smith | Howden | Montmorency | Clementine | Red | Naval | Golden | Concord | ||
16 | 15 | Red Delicious | Valenciano | Royal Ann | Fairchild | Manzano | Cara Cara | Red | Perlette | ||
17 | 16 | Honeycrisp | Lumina | Lambert | Algerian | Plantain | Valencia | Green | Emperor | ||
18 | 17 | Honeycrisp | Howden | Montmorency | Minneola | Plantain | Blood | Berry | Bronx | ||
19 | 18 | Granny Smith | Kakai | Rainier | Clementine | Cavendish | Cara Cara | Golden | Concord | ||
20 | 19 | Red Delicious | Valenciano | Lambert | Fairchild | Manzano | Cara Cara | Green | Emperor | ||
21 | 20 | Granny Smith | Lumina | Rainier | Minneola | Red | Blood | Berry | Perlette | ||
22 | 21 | Granny Smith | Howden | Royal Ann | Algerian | Manzano | Valencia | Golden | Bronx | ||
23 | 22 | Braeburn | Kakai | Montmorency | Minneola | Cavendish | Naval | Red | Concord | ||
24 | |||||||||||
25 | 51 | 48 | 48 | 65 | 87 | 21 | 56 | 49 | |||
26 | 211 | 312 | 65 | 596 | 485 | 700 | 215 | 87 | |||
27 | |||||||||||
28 | Naval | Golden | Percent | ||||||||
29 | 5 | 6 | 83.33% | ||||||||
Sheet3 Oranges and Kiwis are two to the right compared to Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
G29 | =COUNTIF(G1:G23, "Naval") | |
H29 | =COUNTIF(H1:H23, "Golden") | |
I29 | =G29/H29 |
Thanks for your help,
codexsampler