'i want to run macro code in specific sheet

i want to excute below code in specific sheet, without activating that sheet.

since do while references are in sheet1,code should work in sheet1. when I working on sheet2 code dosenot work.

Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use


Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
'Dim sht As Worksheet


Set cn = New ADODB.Connection
cn.Open "PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=D:\Trading\Option Analysis.accdb;PERSIST SECURITY INFO=FALSE;Jet OLEDB:System database=C:\Users\kishor\AppData\Roaming\Microsoft\Access\System1.mdw;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "CE", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 2 ' the start row in the worksheet



    Do While Len(Range("A" & r).Formula) > 0
    ' repeat until first empty cell in column A
        With rs
        .AddNew ' create a new record
        ' add values to each field in the record
        .Fields("Date") = Range("A" & r).Value
        .Fields("Time") = Range("B" & r).Value
        .Fields("LTP") = Range("C" & r).Value
        .Fields("Chg") = Range("D" & r).Value
        .Fields("OI") = Range("E" & r).Value
        .Fields("Volume") = Range("F" & r).Value
        .Fields("Strike_Price") = Range("G" & r).Value
        .Fields("Option_Type") = Range("H" & r).Value
        .Fields("OI_Change") = Range("I" & r).Value
        .Fields("IV") = Range("J" & r).Value
        .Fields("Expiry") = Range("K" & r).Value


        ' add more fields if necessary...
        .Update ' stores the new record
        End With
  
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing


Set rs = New ADODB.Recordset
rs.Open "PE", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 2 ' the start row in the worksheet



Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("Date") = Range("O" & r).Value
.Fields("Time") = Range("P" & r).Value
.Fields("LTP") = Range("Q" & r).Value
.Fields("Chg") = Range("R" & r).Value
.Fields("OI") = Range("S" & r).Value
.Fields("Volume") = Range("T" & r).Value
.Fields("Strike_Price") = Range("U" & r).Value
.Fields("Option_Type") = Range("V" & r).Value
.Fields("OI_Change") = Range("W" & r).Value
.Fields("IV") = Range("X" & r).Value
.Fields("Expiry") = Range("Y" & r).Value

' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing

Set rs = New ADODB.Recordset
rs.Open "Spot", cn, adOpenKeyset, adLockOptimistic, adCmdTable
rs.AddNew ' create a new record
' add values to each field in the record
rs.Fields("Date") = Range("AB" & 2).Value
rs.Fields("Time") = Range("AC" & 2).Value
rs.Fields("Spot") = Range("AD" & 2).Value
rs.Fields("OI_SUM") = Range("AD" & 3).Value

rs.Update
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing



Call datatimer
End Sub

i want to excute below code in specific sheet, without activating that sheet.

since do while references are in sheet1,code should work in sheet1. when I working on sheet2 code dosenot work. request for support from experts



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source