How to setup code in a way to modify easily at once place (1 Viewer)

rehanemis

Registered User.
Local time
Today, 22:00
Joined
Apr 7, 2014
Messages
195
Hi Pros,
I have already written some sub-routines and in my each sub-routine there are two common piece of code. 1. Checking for curtains sheets from which I don't want to take data. 2. The sheet from which I want to take data should be empty of its its B2 cell.
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Temp" Or ws.Name = "MasterSheet" Or ws.Name = "MachineCodes" Or ws.Name = "ExtraOrder" Or ws.Name = "ManualMachineCode" Or ws.Name = "MachineCodeSummary" Or ws.Name = "Procurement" Then

Else

If ws.Range("B2") = 0 Then 'Ignore the sheet if zero Board

Else

Each time I run a different sub routine i do need to set these lines of code. Is there any way to write code once and use in all sub routines.

Thanks
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:00
Joined
May 7, 2009
Messages
19,247
maybe create a Template.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:00
Joined
Feb 28, 2001
Messages
27,207
I'm going to support arnelgp's suggestion. I had a project where I needed common actions for over 30 different forms initially, and that grew to more than 40 as time passed. I had built a template form because I wanted the forms to ALL have the same look-and-feel when used. You asked about subroutines, but the concept of a template works there, too. My template form including common code and yes, it worked OK. Saved me about 40% to 50% of the typing I might otherwise have had to do. Easier to spot-edit to customize the parts that needed it than to compose the code from scratch each time.
 

rehanemis

Registered User.
Local time
Today, 22:00
Joined
Apr 7, 2014
Messages
195
I'm going to support arnelgp's suggestion. I had a project where I needed common actions for over 30 different forms initially, and that grew to more than 40 as time passed. I had built a template form because I wanted the forms to ALL have the same look-and-feel when used. You asked about subroutines, but the concept of a template works there, too. My template form including common code and yes, it worked OK. Saved me about 40% to 50% of the typing I might otherwise have had to do. Easier to spot-edit to customize the parts that needed it than to compose the code from scratch each time.
please give me with some example as I don't know about template.
 

sonic8

AWF VIP
Local time
Today, 20:00
Joined
Oct 27, 2015
Messages
998
Each time I run a different sub routine i do need to set these lines of code. Is there any way to write code once and use in all sub routines.
I'm not entirely sure what you are asking.
Is it just about encapslating that longish if statement? - It makes perfect sense to do so but it is also so simple that I'm not sure if this is what you are after.

In a module:
Code:
Public Function IsCurtainsSheet(ws as Worksheet) AS Boolean
   IsCurtainsSheet = (ws.Name = "Temp" Or ws.Name = "MasterSheet" Or ws.Name = "MachineCodes" _
                        Or ws.Name = "ExtraOrder" Or ws.Name = "ManualMachineCode" _
                        Or ws.Name = "MachineCodeSummary" Or ws.Name = "Procurement")
End Function

Wherever you need to check for this:
Code:
For Each ws In ThisWorkbook.Worksheets
       If IsCurtainsSheet(ws) Then
                 
        Else
....
 

Users who are viewing this thread

Top Bottom