Help with VBA Code

Cervesero

New Member
Joined
Jul 9, 2010
Messages
2
I Have a macro that reads excel file names that are stored in a particular location.

The macro is supposed to


  • Open each file name specified on each row of predefined column
  • Cycle through the number of worksheets in the opened file.
  • Remove any shapes (pictures)
  • Save the modified file in a new location.
The problem that I am having is that the line of code Selection.Delete is not deleting the images.

Everything Else Works.

Please find code below.

Sub Macro1()


'------------Integers--------------

Dim Number_of_Worksheets As Integer
Dim File_Name_Row As Integer
Dim File_Name_Col As Integer


'------------File System Object --------------
Dim WExcel As Excel.Application, WBook As Excel.Workbook, WSheet As Excel.Worksheet
Dim text As String



File_Name_Col = 1
File_Name_Row = 2


Do While Sheet2.Cells(File_Name_Row, File_Name_Col) <> "" 'Do all tags in sheet1 - TAG


Set WExcel = New Excel.Application
Set WBook = WExcel.Workbooks.Open(Sheet1.Cells(19, 1) & "\" & Sheet2.Cells(File_Name_Row, File_Name_Col))
Set WSheet = WBook.ActiveSheet
'the command below is to see the excel file visibly
WExcel.Visible = True

Number_of_Worksheets = WBook.Sheets.Count



For csht = 1 To Number_of_Worksheets 'worksheet or sheets

WBook.Sheets(csht).Activate
WBook.Sheets(csht).Shapes.SelectAll
Selection.Delete


Next csht

'Save New File
WBook.Activate
WBook.SaveAs Filename:=Sheet1.Cells(21, 1) & "\NEW" & Sheet2.Cells(File_Name_Row, File_Name_Col)
WBook.Close

File_Name_Row = File_Name_Row + 1

Loop



End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
welcome to the board... sorry dont have time to go thru every line but you dont have to select an object to delete it - here is a quick sample on how an object is deleted

Code:
ThisWorkbook.Sheets("Sheet1").Activate
ActiveSheet.Shapes("Rectangle 1").Delete

this (edited to suit your needs) goes instead of this part
WBook.Sheets(csht).Activate
WBook.Sheets(csht).Shapes.SelectAll
Selection.Delete

HTH - maybe someone can add smth to this
 
Upvote 0
Thanks for the quick reply.

The problem with the code suggested, is that the macro does not know the name of the object , ie Each worksheet might have different names for the shape.


ThisWorkbook.Sheets("Sheet1").Activate
ActiveSheet.Shapes("Rectangle 1").Delete</pre>
Cheers
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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