VBA Countif non-blank cells and loop

stratthaslam

Board Regular
Joined
Feb 4, 2009
Messages
63
Good day everyone,

I would appreciate your help. I have hit a snag in creating a macro. I need something that will use countifs (or sumproduct) non-blank rows in the last non-blank column if the meet two requirements and place the result in the column to the right of the last column. So in B2 place the results of a countifs formula based on the value of A2. Below is what I have so far. I hope I explained this well enough.

Thank you for your help

Code:
Sub Macro3()
Dim LastColumn As Integer
Dim NextColumn As Integer
'Find last column with text
If WorksheetFunction.CountA(Cells) > 0 Then
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
End If
'Copy list into next non-blank column
Sheets("Lists").Select
Columns("B:B").Select
Selection.Copy
Sheets("Top 5").Select
Columns(LastColumn + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Countif Loop
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For I = 3 To FinalRow
If Cells(I, LastColumn).Value = "*" Then
Cells(I, LastColumn + 1).Value = Application.WorksheetFunction.CountIfs(Worksheets("Data").Range("e:e"), Cells(I, LastColumn), Worksheets("Data").Range("a:a"), Cells(2, LastColumn))
End If
Next I
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,213,551
Messages
6,114,267
Members
448,558
Latest member
aivin

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