Method or Data member not found

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
in this macro, I am using the CountIf feature to count the # of letter "B" in column K when "CS55" is in column K
ws1 is defined, why the error message ""method or data member not found" ?
VBA Code:
n = ws1.CountIf(Range(lrNew, "K"), "*B*")

full code below
VBA Code:
Sub PaintCS55()
    Dim ws1 As Worksheet, lrNew As Long, n As Long, n1 As Long, n2 As Long
    lrNew = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    sr = 2

    If Cells(lrNew, "K").Value Like "*CS55*" Then
     n = ws1.CountIf(Range(lrNew, "K"), "*B*")
    End If

    If Cells(lrNew, "K").Value Like "*CS55**Black*" Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**Black*")
         n2 = n1 * 0.000666 * 7.48 * 2
    End If
    If Cells(lrNew, "K").Value Like "*CS55*" And _
       n = 1 Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n2 = n1 * 0.000666 * 7.48
    End If
    If Cells(lrNew, "K").Value Like "*CS55*" And _
       n = 2 Then
         n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n2 = n1 * 0.000666 * 7.48 * 2
    End If

    lrNew = lrNew + 1
    Cells(lrNew, "A") = Cells(lr, "A")
    Cells(lrNew, "B") = "."
    Cells(lrNew, "C") = n2
    Cells(lrNew, "D") = "F50504"
    Cells(lrNew, "I") = "Purchased"
    Cells(lrNew, "K") = "RFS Gasket"
    If Cells(lrNew, "C").Value Like "*0*" Then
        Rows(lrNew).Delete
    End If
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I put some comments in the code for better understanding.
I haven't tried all the code, maybe you have other problems, but I updated the code to reference the ws1 object:

Rich (BB code):
Sub PaintCS55()
  'Here you declare the variable ws1 of type Worksheet
  Dim ws1 As Worksheet
  
  Dim lrNew As Long, n As Long, n1 As Long, n2 As Long, sr As Long
  
  'But you should Set the variable as object and tell it which sheet
  Set ws1 = ActiveSheet    'or sheets("Sheet1")   or the name of your sheet
  
  'Then you can use the variable ws1 as a reference to the sheet in each Range or Cells object:
  lrNew = ws1.Range("K" & Rows.Count).End(xlUp).Row
  sr = 2
  
  'But the CountIf function is a function of WorksheetFunction:
  If ws1.Cells(lrNew, "K").Value Like "*CS55*" Then
    n = WorksheetFunction.CountIf(ws1.Range("K" & sr & ":K" & lrNew), "*B*")
  End If
  
  If ws1.Cells(lrNew, "K").Value Like "*CS55**Black*" Then
    n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**Black*")
    n2 = n1 * 0.000666 * 7.48 * 2
  End If
  
  If ws1.Cells(lrNew, "K").Value Like "*CS55*" And _
      n = 1 Then
    n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**B*")
    n2 = n1 * 0.000666 * 7.48
  End If
  
  If ws1.Cells(lrNew, "K").Value Like "*CS55*" And _
      n = 2 Then
    n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**B*")
    n2 = n1 * 0.000666 * 7.48 * 2
  End If

  lrNew = lrNew + 1
  ws1.Cells(lrNew, "A") = ws1.Cells(lrNew, "A")
  ws1.Cells(lrNew, "B") = "."
  ws1.Cells(lrNew, "C") = n2
  ws1.Cells(lrNew, "D") = "F50504"
  ws1.Cells(lrNew, "I") = "Purchased"
  ws1.Cells(lrNew, "K") = "RFS Gasket"
  If ws1.Cells(lrNew, "C").Value Like "*0*" Then
    Rows(lrNew).Delete
  End If
End Sub
 
Upvote 0
I put some comments in the code for better understanding.
I haven't tried all the code, maybe you have other problems, but I updated the code to reference the ws1 object:

Rich (BB code):
Sub PaintCS55()
  'Here you declare the variable ws1 of type Worksheet
  Dim ws1 As Worksheet
 
  Dim lrNew As Long, n As Long, n1 As Long, n2 As Long, sr As Long
 
  'But you should Set the variable as object and tell it which sheet
  Set ws1 = ActiveSheet    'or sheets("Sheet1")   or the name of your sheet
 
  'Then you can use the variable ws1 as a reference to the sheet in each Range or Cells object:
  lrNew = ws1.Range("K" & Rows.Count).End(xlUp).Row
  sr = 2
 
  'But the CountIf function is a function of WorksheetFunction:
  If ws1.Cells(lrNew, "K").Value Like "*CS55*" Then
    n = WorksheetFunction.CountIf(ws1.Range("K" & sr & ":K" & lrNew), "*B*")
  End If
 
  If ws1.Cells(lrNew, "K").Value Like "*CS55**Black*" Then
    n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**Black*")
    n2 = n1 * 0.000666 * 7.48 * 2
  End If
 
  If ws1.Cells(lrNew, "K").Value Like "*CS55*" And _
      n = 1 Then
    n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**B*")
    n2 = n1 * 0.000666 * 7.48
  End If
 
  If ws1.Cells(lrNew, "K").Value Like "*CS55*" And _
      n = 2 Then
    n1 = WorksheetFunction.SumIfs(ws1.Range("C" & sr & ":C" & lrNew), ws1.Range("K" & sr & ":K" & lrNew), "*CS55**B*")
    n2 = n1 * 0.000666 * 7.48 * 2
  End If

  lrNew = lrNew + 1
  ws1.Cells(lrNew, "A") = ws1.Cells(lrNew, "A")
  ws1.Cells(lrNew, "B") = "."
  ws1.Cells(lrNew, "C") = n2
  ws1.Cells(lrNew, "D") = "F50504"
  ws1.Cells(lrNew, "I") = "Purchased"
  ws1.Cells(lrNew, "K") = "RFS Gasket"
  If ws1.Cells(lrNew, "C").Value Like "*0*" Then
    Rows(lrNew).Delete
  End If
End Sub
thanks ! The other parts of the code works fine, they've been tested many times

I modified the code and a new error popped up
Method "Range" of "_global" object failed

VBA Code:
n = WorksheetFunction.CountIf(Range(lr, "K"), "*B*")

I am not sure exactly what is causing this issue...
 
Upvote 0
You need either Cells(lr, "K") or Range("K" & lr) though it seems overkill to use Countif on one cell.
 
Upvote 0
You need either Cells(lr, "K") or Range("K" & lr) though it seems overkill to use Countif on one cell.
If CountIf is an overkill, can I use something like below

VBA Code:
n = Evaluate("LEN(" & adr  & "-LEN(SUBSTITUTE(" & adr  & ",""B"","""")")

length of the string with B - length of the string without B = the # of B ?
 
Upvote 0
You could but that would be even more overkill. ;) See Joe's suggestion.
 
Upvote 0
I agree. It seems like the InStr function would work nicely here: MS Excel: How to use the INSTR Function (VBA)

For example:
VBA Code:
If InStr(Cells(lr, "K"), "B") > 0 Then
thanks Joe, I modified the code to utilize InStr, did a test run and the amount was calculated as 0..
When I tried to debug, stepping throught the code, it is skipping the code in bold below....

VBA Code:
Sub PaintCS55()
    Dim lrNew As Long, n1 As Long, n2 As Long, adr As String
    
    lrNew = Range("A" & Rows.Count).End(xlUp).Row
    sr = 2

    
    If Cells(lrNew, "K").Value Like "*CS55**B*" And _
       InStr(Cells(lr, "K"), "B") = 1 Then
         [B]n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n2 = n1 * 0.000666 * 7.48[/B]
    End If
    
    If Cells(lrNew, "K").Value Like "*CS55**B*" And _
       InStr(Cells(lr, "K"), "B") = 2 Then
         [B]n1 = WorksheetFunction.SumIfs(Range("C" & sr & ":C" & lr), Range("K" & sr & ":K" & lr), "*CS55**B*")
         n2 = n1 * 0.000666 * 7.48 * 2[/B]
    End If

    lrNew = lrNew + 1
    Cells(lrNew, "A") = Cells(lr, "A")
    Cells(lrNew, "B") = "."
    Cells(lrNew, "C") = n2
    Cells(lrNew, "D") = "F62655"
    Cells(lrNew, "I") = "Purchased"
    Cells(lrNew, "K") = "CS55 Black"
    If Cells(lrNew, "C").Value Like "*0*" Then
        Rows(lrNew).Delete
    End If
End Sub
 
Upvote 0
I don't understand your code. Countif was only checking if a B was in the cell in column K. If you need to calculate how many Bs are in that one cell in column K, then you would need something like your SUBSTITUTE formula, or Replace in VBA.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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