CoutIf formula in VBA, loop and select columns

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hi

I tried to modify my code such that it can work on a countif formula:

Code:
Const strFormula    As String = "=COUNTIF( @@ , >0)"

This codeline is bugging:
Code:
rngFormula.Formula = Replace(strFormula, "@@", strColumns)

Here is the code:

Code:
Sub risk_factor()


Dim lr As Long, i As Long, lrX As Long
Dim wb As Workbook
Dim ws1 As Worksheet, wsX As Worksheet, ws3 As Worksheet
Dim strColumns As String, sName As String, strAddress As String
Dim rngChk          As Range
Dim rng             As Range    'Loop Counter
Dim rngFormula      As Range
Dim rngFill         As Range
 
Const strFormula    As String = "=COUNTIF( @@ , >0)"
    
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Master")
    Set ws1 = wb.Sheets("Graphics")
    Set ws3 = wb.Sheets("All")
        
    For Each wsX In ThisWorkbook.Worksheets
        If IsError(Application.Match(wsX.Name, Array("Graphics", "Master", "All", "Bonds"), 0)) Then
        
        strColumns = ""
        j = j + 1
        
        lr = ws.Cells(8, j + 2).End(xlDown).Row
                
                sName = ws1.Cells(j + 6, 15).Value
                Set wsX = wb.Worksheets(sName)
                
                    wsX.Activate
                    
                If wsX.Cells(1, 4).Value <> 0 Then ' If risk column is missing, then add it, if not go next
                    wsX.Cells(1, 4).Activate
                    ActiveCell.EntireColumn.Insert
                End If
                
                For i = 7 To lr 'This loop goes through all strategies listed in Graphic Sheet
                    lrX = wsX.Range("O6").End(xlDown).Row
 
                    stock = ws.Cells(i, j + 2)
                    Set rng = wsX.Rows(4).Find(stock)
 
                        With wsX
                            .Activate
                            Set rngFormula = .Range("D5")
                            Set rngFill = .Range(Cells(5, 4), Cells(lr, 4))
                        End With
                        
                        If rng Is Nothing Then
                         ' No columns for this month in Master
                        Else
                            strColumns = strColumns & Replace(rng.Address, "$", "") & ", "  'Actual column
                            'rx = wsX.Cells(
                        End If
                Next i
                 
                    strColumns = Left(strColumns, Len(strColumns) - 2)
                    rngFormula.Formula = Replace(strFormula, "@@", strColumns)
                    rngFormula.AutoFill rngFill
            End If
        Next wsX
    
    
End Sub

Kind regards
Espen
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,196,476
Messages
6,015,435
Members
441,894
Latest member
Zululander

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