hi am using the following macro to update all sheets in a work book based on a main sheet, it does not shows any error and does not updates in all sheets.. it was given by one of my friend but it is not working when i tried but it was working at his home am using excel 2010 he uses excel 2007.
Private Sub btnupdate_click()
Sheets(”boys”).Select
Sheets(”boys”).Cells.Select
Selection.ClearContents
Sheets(”boys 40-50″).Select
Sheets(”boys 40-50″).Cells.Select
Selection.ClearContents
Sheets(”boys 30-40″).Select
Sheets(”boys 30-40″).Cells.Select
Selection.ClearContents
Sheets(”girls”).Select
Sheets(”girls”).Cells.Select
Selection.ClearContents
Sheets(”girls 40-50″).Select
Sheets(”girls 30-40″).Cells.Select
Selection.ClearContents
Dim strfilename As String
strfilename = ThisWorkbook.FullName
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
With cn
.Provider = “Microdoft.Jet.OLEDB.4.0″
.Connectionstring = “Data Source=” & strfilename & “;” & _
“Extended properties = Excel 8.0;”
.Open
End With
Dim rs As ADODB.Recordset
Dim row As Integer
‘boys
Set rs = New ADODB.Recordset
rs.Open “SELECT Name,Age,***,Weight FROM [Main database$] where name is NOT NULL and *** = ‘M’”, cn, adopendynamic, adLockBatchoptimistic
Sheets(”boys”).Select
Sheets(”boys”).Cells(1, 1) = “Name”
Sheets(”boys”).Cells(1, 2) = “Age”
Sheets(”boys”).Cells(1, 3) = “***”
Sheets(”boys”).Cells(1, 4) = “Weight”
row = 1
Do While Not rs.EOF
Sheets(”boys”).Cells(row + 1, 1) = rs.feilds(”Name”)
Sheets(”boys”).Cells(row + 1, 2) = rs.feilds(”Age”)
Sheets(”boys”).Cells(row + 1, 3) = rs.feilds(”***”)
Sheets(”boys”).Cells(row + 1, 4) = rs.feilds(”Weight”)
row = row + 1
rs.Movenext
Loop
‘boys with weight 40-50
Set rs = New ADODB.Recordset
rs.Open “SELECT Name,Age,***,Weight FROM [Main database$] where name is NOT NULL and *** = ‘M’ and weight>40 and Weight30 and Weight40 and weight30 and weight<=40", cn, adopendynamic, adLockBatchoptimistic
Sheets("girls 30-40").Select
Sheets("girls 30-40").Cells(1, 1) = "Name"
Sheets("girls 30-40").Cells(1, 2) = "Age"
Sheets("girls 30-40").Cells(1, 3) = "***"
Sheets("girls 30-40").Cells(1, 4) = "Weight"
row = 1
Do While Not rs.EOF
Sheets("girls 30-40").Cells(row + 1, 1) = rs.feilds("Name")
Sheets("girls 30-40").Cells(row + 1, 2) = rs.feilds("Age")
Sheets("girls 30-40").Cells(row + 1, 3) = rs.feilds("***")
Sheets("girls 30-40").Cells(row + 1, 4) = rs.feilds("Weight")
row = row + 1
rs.Movenext
Loop
Sheets("Main database").Sheet
MsgBox "upload successfully"
cn.Close
Set cn = Nothing
End Sub
Bookmarks