Results 1 to 7 of 7

Thread: Count Occurences of String in a Worksheet from MS Access
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Jul 2016
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Count Occurences of String in a Worksheet from MS Access

    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

  2. #2
    Board Regular
    Join Date
    May 2011
    Location
    Russia
    Posts
    2,834
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Count Occurences of String in a Worksheet from MS Access

    Code:
    wordCount = appXL.WorksheetFunction.CountIf(Range("A1:B10"), "zeeshan")

  3. #3
    New Member
    Join Date
    Jul 2016
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Count Occurences of String in a Worksheet from MS Access

    Thanks Sektor ... I did this change and now I get message "run time error '13' .. Type Mismatch"

  4. #4
    New Member
    Join Date
    Jul 2016
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Count Occurences of String in a Worksheet from MS Access

    and the same line gets selected where you suggested change ...
    Code:
    wordCount = appXL.WorksheetFunction.CountIf(Range("A1:B10"), "zeeshan")

  5. #5
    Board Regular
    Join Date
    May 2011
    Location
    Russia
    Posts
    2,834
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Count Occurences of String in a Worksheet from MS Access

    Forgot about worksheet
    Code:
    wordCount = appXL.WorksheetFunction.CountIf(wst.Range("A1:B10"), "zeeshan")

  6. #6
    New Member
    Join Date
    Jul 2016
    Posts
    4
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Count Occurences of String in a Worksheet from MS Access

    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 !

  7. #7
    Board Regular
    Join Date
    May 2011
    Location
    Russia
    Posts
    2,834
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Count Occurences of String in a Worksheet from MS Access

    You're always welcome!

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •