Need help to simplify VB code

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
Can someone help to simplify following VB code
If I use something unnecessarily like "Select" etc.

Sub filename_cellvalue()

ActiveWorkbook.Save

For sh = 1 To Sheets.Count
Sheets(sh).Visible = -1
Next sh


Application.DisplayAlerts = False
Sheets(Array("Consolidated Report", "Welcome")).Select
Sheets("Consolidated Report").Activate
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True

Sheets("New Style").Select

ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete


Sheets("Garment Detail").Select

ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete


Sheets("Picture").Select
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete

Sheets("Operations").Select

ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete


Sheets("Machines Data").Select

ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete


Sheets("Layout").Select

ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete


Sheets("Report").Select
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete

Sheets("Summary").Select
ActiveSheet.Shapes.Range(Array("ColorA3")).Select
Selection.Delete

ActiveSheet.Shapes.Range(Array("Button 554")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Button 556")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Button 553")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Button 627")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Button 555")).Select
Selection.Delete

Sheets("Short").Select
ActiveWindow.SelectedSheets.Visible = False

Dim Path As String
Dim FileName As String
Application.DisplayAlerts = False

If Dir(ThisWorkbook.Path & "\Backup", vbDirectory) = vbNullString Then MkDir ThisWorkbook.Path & "\Backup"
Path = ThisWorkbook.Path & "\Backup" & ""

FileName = Range("O6")
ActiveWorkbook.SaveAs fileName:=Path & fileName & ".xlsb", FileFormat:=50
Application.DisplayAlerts = False


ChDir "C:\Users\ltpurc08\Desktop\Thread Consumption Software"
Workbooks.Open fileName:= _
"C:\Users\ltpurc08\Desktop\Thread Consumption Software\Thread Consumption.xlsb"
Windows("Thread Consumption.xlsb").Activate
ActiveWindow.ActivateNext

ActiveWorkbook.Close






End Sub
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
789
Office Version
2007
Platform
Windows
Hi there. You can pretty much lose all the selects. I'm curious as to why you are deleting the ColorA3 array twice on each sheet? Anyway, try this code:
Code:
[COLOR=#141414][FONT=Consolas][COLOR=#141414][FONT=Consolas]Application.DisplayAlerts = [/FONT][/COLOR][COLOR=#00C2FF][FONT=Consolas][B]False[/B][/FONT][/COLOR]
Sheets(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"Consolidated Report"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas], [/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"Welcome"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]))[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Delete
[/FONT][/COLOR][COLOR=#141414][FONT=Consolas][COLOR=#141414][FONT=Consolas]Application.DisplayAlerts = [/FONT][/COLOR][COLOR=#00C2FF][FONT=Consolas][B]True[/B][/FONT][/COLOR]
[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Sheets([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"New Style"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Range(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"ColorA3"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])).[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Delete[/FONT][/COLOR]

[COLOR=#141414][FONT=Consolas]Sheets([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"[/FONT][/COLOR][COLOR=#800000][FONT=Consolas][COLOR=#800000][FONT=Consolas]Garment Detail[/FONT][/COLOR]"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Range(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"ColorA3"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])).[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Delete[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]
' and repeat as necessary for each sheet. [/FONT][/COLOR]
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
789
Office Version
2007
Platform
Windows
Hello there. I'm sorry I missed the shapes selecter out of each of the lines. the relevant line should read
Code:
[COLOR=#141414][FONT=Consolas]Sheets([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"New Style"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Shapes.Range(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"ColorA3"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])).[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Delete[/FONT][/COLOR]

[COLOR=#141414][FONT=Consolas]Sheets([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"[/FONT][/COLOR][COLOR=#800000][FONT=Consolas][COLOR=#800000][FONT=Consolas]Garment Detail[/FONT][/COLOR]"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Shapes.Range(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"ColorA3"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])).[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Delete
[COLOR=#141414][FONT=Consolas]
' and repeat as necessary for each sheet.[/FONT][/COLOR][/FONT][/COLOR]
:
 

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
Dear Sir, thanks its worked but since sheets have two shapes and both named as "colorA3" after your code, it is only deleting one
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
789
Office Version
2007
Platform
Windows
OK I wondered why you had 2 deletes. Just repeat the relevant line, so the first 2 would be:
Code:
[COLOR=#141414][FONT=Consolas]Sheets([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"New Style"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Shapes.Range(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"ColorA3"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])).[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Delete[/FONT][/COLOR]
[COLOR=#141414][FONT=Consolas]Sheets([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"New Style"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Shapes.Range(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"ColorA3"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])).[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Delete[/FONT][/COLOR]

[COLOR=#141414][FONT=Consolas]Sheets([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"[/FONT][/COLOR][COLOR=#800000][FONT=Consolas][COLOR=#800000][FONT=Consolas]Garment Detail[/FONT][/COLOR]"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Shapes.Range(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"ColorA3"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])).[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Delete
[COLOR=#141414][FONT=Consolas]Sheets([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"[/FONT][/COLOR][COLOR=#800000][FONT=Consolas][COLOR=#800000][FONT=Consolas]Garment Detail[/FONT][/COLOR]"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])[/FONT][/COLOR][COLOR=#141414][FONT=Consolas].Shapes.Range(Array([/FONT][/COLOR][COLOR=#800000][FONT=Consolas]"ColorA3"[/FONT][/COLOR][COLOR=#141414][FONT=Consolas])).[/FONT][/COLOR][COLOR=#141414][FONT=Consolas]Delete[/FONT][/COLOR][/FONT][/COLOR]
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
Are you only deleting specific shapes?
 

ateebali

Board Regular
Joined
Dec 13, 2018
Messages
108
Dear Sir,
Thanks I just put this code twice and its perfectly fine, thanks a lot
 

bukimi

Board Regular
Joined
Apr 12, 2017
Messages
82
Do you have any Sheets that need that ColorA3 shape not deleted? If you're deleting them from every sheet that contain them (or all except one), then code can be much shorter by looping the code through all sheets in a file.
 

Forum statistics

Threads
1,081,981
Messages
5,362,535
Members
400,679
Latest member
alecalec202

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top