I am trying to create a macro that will export new records from access to an Excel file each week. Currently my code has it adding a new sheet to the Excel file, but how can I get it to just update a sheet that's already in the file?
Const ConStrAccess As String = "xxxx;"
Sub CopyDataFomDatabase()
Dim BrokerConn As ADODB.Connection
Dim BrokerData As ADODB.Recordset
Dim BrokerField As ADODB.Field
Set BrokerConn = New ADODB.Connection
Set BrokerData = New ADODB.Recordset
BrokerConn.ConnectionString = ConStrAccess
BrokerConn.Open
On Error GoTo CloseConnection
With BrokerData
.ActiveConnection = BrokerConn
.Source = "SELECT * FROM xxxxx;"
.LockType = adLockReadOnly
.CursorType = adOpenForwardOnly
.Open
End With
On Error GoTo CloseRecordset
Worksheets.Add
For Each BrokerField In BrokerData.Fields
ActiveCell.Value = BrokerField.Name
ActiveCell.Offset(0, 1).Select
Next BrokerField
Range("A1").Select
Range("A2").CopyFromRecordset BrokerData
Range("A1").CurrentRegion.EntireColumn.AutoFit
CloseRecordset:
BrokerData.Close
CloseConnection:
BrokerConn.Close