To delete data in Excel using VBA, you can use the ClearContents
method of a range object. Here’s an example of VBA code that demonstrates this process:
Sub DeleteDataInExcel() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with the name of your worksheet ' Specify the range of data to be deleted Dim deleteRange As Range Set deleteRange = ws.Range("A1:C10") ' Replace "A1:C10" with the range you want to delete ' Delete the contents of the range deleteRange.ClearContents End Sub
In this code, you need to modify the Set ws = ThisWorkbook.Worksheets("Sheet1")
line to specify the name of your worksheet. Replace "Sheet1"
with the actual name of your worksheet.
Additionally, modify the Set deleteRange = ws.Range("A1:C10")
line to specify the range of data you want to delete. Replace "A1:C10"
with the actual range address.
When you run this code, it selects the specified range on the worksheet and clears its contents, effectively deleting the data within that range.
Note: Be cautious when deleting data as it cannot be easily recovered. Make sure to take appropriate precautions and double-check the range before executing the deletion.