Overwrite same worksheet macro

marstonk

Active Member
Joined
Jan 30, 2003
Messages
273
Hi,

I have a simple macro in access which sends new data to excel eachtime it runs. I want it to overwrite the existing data but I am having problems wioth this. It will only work if I delete the worksheet it generates in excel save close and then run the macro. Does anyone have a solution so that it overwrites the worksheet.

Thanks
Kate
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Here is some code that you can use to erase the existing contents of the worksheet and then put the new values in. If you are only wanting to overwrite certain cells use the Range function to identify them before clearing.

I have to give some credit to Dave Swanton, I took some of his code from
a previous post.

Public Sub DeleteXLWorksheet()

' Attribution to Dave Swanton for some of the code, from
' a previous post

Dim objXL As Excel.Application, wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set objXL = CreateObject("excel.application")

Set wb = objXL.Workbooks.Open("Your Filename goes here")

objXL.Visible = True

For Each ws In wb.Worksheets
If ws.Name = "Your Sheet name" Then
With wb.Sheets(ws.Name)
Cells.Select 'Select all of the cells
Selection.ClearContents 'Clear the contents
Selection.NumberFormat = "General" 'Reset all the formatting
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Selection.Interior.ColorIndex = xlNone
Range("a1").Select 'Move to upper left corner
End With

End If
Next ws

wb.Save

Set objXL = Nothing

End Sub
 
Upvote 0
Hi bzitt,

Thanks for your reply. The excel file I am using is password protected - do know the code to type the password in so it is not read only or anyway to override this when I run the macro.

Very much a beginner!!!

kitty
 
Upvote 0
Set wb = objXL.Workbooks.Open("Your Filename goes here")

One of the parameters of this command is password. To open a password protected file, you will need to provide this value. Check the Excel VBA help on the Open method for details.
HTH.
 
Upvote 0

Forum statistics

Threads
1,221,805
Messages
6,162,081
Members
451,738
Latest member
gaseremad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top