Count Occurences of String in a Worksheet from MS Access

zeeshanaslamdurrani

New Member
Joined
Jul 4, 2016
Messages
4
Hi,

I want to search occurences of string "zeeshan" in a range in MS Excel through VBA from MS Access button click.

Below is my code ... I am getting compile error : "method or member not found".
With this error below line gets selected
wordCount = Application.WorksheetFunction.CountIf(Range("A1:B10"), "zeeshan")


Code

Code:
Private Sub Command2_Click()


    Dim appXL As Object 'Excel.Application
    Dim wbk As Object 'Excel.Workbook
    Dim wst As Object 'Excel.Worksheet
    Dim Timer As Integer


    Set appXL = CreateObject("Excel.Application")
  appXL.Visible = True 'If you want to see the excel sheet - enable this row (good for debugging)
    Set wbk = appXL.Workbooks.Add
    Set wst = wbk.Worksheets(1)


    With wst
'In the following row, after the word 'key=' until the '&gid' - put the code-number of the google-doc spreadsheet, which you extract from the link you get for the spreadsheet google-doc (looks like: 'KeXnteS6n6...')
        .QueryTables.Add Connection:= _
            "URL;https://spreadsheets.google.com/tq?tqx=out:html&tq=&key=1e6DNpw3y5NrMR9cNLmIZdPYO79WLui7mua5I-5pEyKo&gid=1" _
            , Destination:=.Range("$A$1")
        .Name = "serial"
'The following fields are available if enabling Excel library (See above)
'        .FieldNames = True
'        .RowNumbers = False
'        .FillAdjacentFormulas = False
'        .PreserveFormatting = True
'        .RefreshOnFileOpen = False
'        .BackgroundQuery = True
'        .RefreshStyle = xlInsertDeleteCells
'        .SavePassword = False
'        .SaveData = True
'        .AdjustColumnWidth = True
'        .RefreshPeriod = 0
'        .WebSelectionType = xlEntirePage
'        .WebFormatting = xlWebFormattingNone
'        .WebPreFormattedTextToColumns = True
'        .WebConsecutiveDelimitersAsOne = True
'        .WebSingleBlockTextImport = False
'        .WebDisableDateRecognition = False
'        .WebDisableRedirections = False
'        .Refresh BackgroundQuery:=False


        .QueryTables(1).Refresh
    End With


    'Wait for google-doc data to be downloaded.
    Timer = 0
    Do While Left(wst.Cells(1, 1), 12) = "ExternalData" And Timer < 40
        
        Sleep 600   ' Wait 0.25 sec before re-checking data
        Timer = Timer + 1
    Loop


  '  MsgBox "The value of cell A1 is: " & wst.Cells(1, 1)

Dim wordCount As Long
wordCount = Application.WorksheetFunction.CountIf(Range("A1:B10"), "zeeshan")


MsgBox "Total occurences of string are " & wordCount


  'wbk.Close SaveChanges:=False 'Don't save excel sheet
  wbk.Close SaveChanges:=True, fileName:="C:\M\b.xls" 'Save excel sheet  
 MsgBox "data saved in a file"


Dim aFile As String
aFile = "C:\M\b.xls"
If Len(Dir$(aFile)) > 0 Then
     
     Kill aFile
     MsgBox "file successfully deleted"
End If




  appXL.Quit


End Sub
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Sektor

Well-known Member
Joined
May 6, 2011
Messages
2,834
Code:
wordCount = [COLOR=#ff0000][SIZE=3][B]appXL[/B][/SIZE][/COLOR].WorksheetFunction.CountIf(Range("A1:B10"), "zeeshan")
 

zeeshanaslamdurrani

New Member
Joined
Jul 4, 2016
Messages
4
and the same line gets selected where you suggested change ...
Code:
wordCount = appXL.WorksheetFunction.CountIf(Range("A1:B10"), "zeeshan")
 

Sektor

Well-known Member
Joined
May 6, 2011
Messages
2,834
Forgot about worksheet :)
Code:
wordCount = [COLOR=#ff0000][SIZE=3][B]appXL[/B][/SIZE][/COLOR].WorksheetFunction.CountIf([B][COLOR=#ff0000]wst.[/COLOR][/B]Range("A1:B10"), "zeeshan")
 

zeeshanaslamdurrani

New Member
Joined
Jul 4, 2016
Messages
4
Wow ! Sektor you are so quick ... bundle of thanks and best wishes ... it is really useful ... thanks once again. This was my first post on this forum and I got reply in 1 minute. Wow ! amazing work Sektor ! :)
 

Watch MrExcel Video

Forum statistics

Threads
1,095,223
Messages
5,443,183
Members
405,219
Latest member
CraneS

This Week's Hot Topics

  • Copy entire row if CountA <>0 to another sheet
    [B]I want to copy entire row if CountA <>0 for column J7:AM7 (headers on J6:AM6) and so on till the last used cell is column D and paste the...
  • Select last used Row in Table
    I have created a Table in a Worksheet which is locked to prevent user errors and protect formula. Some of the cells require freetext entries which...
  • excel workbook: do not allow certain file name
    Hello all, Don't think this has ever been asked before, but how do I restrict file save [Before_Save Event] if the name of the file being saved...
  • fixing problem autofilter
    hello i need help about my code when i search by code in textbox it doesn't show anything this is my data [ATTACH type="full"...
  • “Weight”
    Hi, i’ve got a long sheet filled with weights such as kg,g,L & ml. i can build a formula to convert kg into g and liter into ml. How ever, my...
  • How to capitalize everything before a certain character?
    In column A, I have some text: Hello good day.mp3 Hello good day.flac etc. I'd like to capitalize everything before the period. I don't need the...
Top