Consulting

Results 1 to 3 of 3

Thread: Run macro in all worksheets

  1. #1
    VBAX Regular
    Joined
    Oct 2013
    Posts
    10
    Location

    Question Run macro in all worksheets

    Hello guys.

    Could you help me with this case. I'm not getting the macro to run on all worksheets.

    I made many attempts but without sucess...

    Follow the code and the workbook. GERAL.zip
    Sub BuscaSQL1()
    'Selection.ClearContents
        Dim ConecaoPlan As New ADODB.Connection
        Dim rsConsulta As New ADODB.Recordset
        Dim Caminho As String
        Dim Roda As Integer
        Dim sql As String
        'Dim ws_count As Integer
        'Dim I As Integer
        '''Dim sheet As Worksheet
        '''Dim book As Workbook
        ''''Dim ws As Worksheet
        
        Caminho = ActiveWorkbook.Path & "\2110(2).xlsm"
        ConecaoPlan.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Caminho & ";Extended Properties=Excel 8.0;"  '";Extended Properties=Excel 12.0 Xml;HDR=YES;"Extended Properties=Excel 8.0
        ConecaoPlan.Open
        
        'ws_count = ActiveWorkbook.Worksheets.Count
        'For I = 1 To ws_count
        
        ''For Each sheet In ThisWorkbook.Sheets
        '''For Each sheet In book.Worksheets
        
        ''''For Each ws In Sheets
        
        'Primeira Parte
        Range("B10").Select
        
        Do While ActiveCell.Value <> "Total AR"
            sql = "Select * From [MontaBase$] Where Dado1 Like '" & ActiveCell.Value & "'"
            rsConsulta.Open sql, ConecaoPlan, adOpenKeyset, adLockOptimistic
        If rsConsulta.RecordCount > 0 Then
               For Roda = 1 To (rsConsulta.RecordCount * 5) Step 5
                    Cells(ActiveCell.Row, ActiveCell.Column + Roda).Value = rsConsulta!Dado2
                    rsConsulta.MoveNext
                Next
            End If
            rsConsulta.Close
            Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
        Loop
        'Segunda Parte
        Range("B23").Select
        
        Do While ActiveCell.Value <> "Cash Receipts"
            sql = "Select * From [MontaBase$] Where Dado3 Like '" & ActiveCell.Value & "'"
            rsConsulta.Open sql, ConecaoPlan, adOpenKeyset, adLockOptimistic
        If rsConsulta.RecordCount > 0 Then
               For Roda = 1 To (rsConsulta.RecordCount * 5) Step 5
                    Cells(ActiveCell.Row, ActiveCell.Column + Roda).Value = rsConsulta!Dado4
                    rsConsulta.MoveNext
                Next
            End If
            rsConsulta.Close
            Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
        Loop
    
        'Terceira Parte
        Range("B30").Select
        
        Do While ActiveCell.Value = " "
            sql = "Select * From [MontaBase$] Where Dado5 Like '" & ActiveCell.Value & "'"
            rsConsulta.Open sql, ConecaoPlan, adOpenKeyset, adLockOptimistic
        If rsConsulta.RecordCount > 0 Then
               For Roda = 3 To ((3 + rsConsulta.RecordCount) * 4) Step 5
                    Cells(ActiveCell.Row, ActiveCell.Column + Roda).Value = rsConsulta!Dado6
                    rsConsulta.MoveNext
                Next
            End If
            rsConsulta.Close
            Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
        Loop
        'Next I
        ''Next sht
        '''Next sheet
        ''''Next ws
        Set ConecaoPlan = Nothing
        Set rsConsulta = Nothing
    End Sub

  2. #2
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    210
    Location
    Those ALL work, but you must make XL go to the sheet after you assign it.

    For Each ws In Sheets
       ws.select
    next
    Last edited by ranman256; 05-13-2014 at 07:10 AM. Reason: format

  3. #3
    VBAX Regular
    Joined
    Oct 2013
    Posts
    10
    Location
    True true...

    tks for your help !

Posting Permissions

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