+ Reply to Thread
Results 1 to 4 of 4

IF-THEN-ELSE - how i reduce size of the prodedure

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2017
    Location
    tamilnadu
    MS-Off Ver
    2013
    Posts
    9

    IF-THEN-ELSE - how i reduce size of the prodedure

    hai in below code there any possibility to reduce the size of the prodedure.
    i am new to VBA trying to learn the skill
    Sub RSIDSTOBUYSELL()
     With Application
       Application.Calculation = xlCalculationManual
       Application.ScreenUpdating = False
       Application.DisplayStatusBar = False
       Application.EnableEvents = False
       Application.PrintCommunication = False
       End With
    
    Dim I As Long
    Dim IRow As Long
    Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Select
    IRow = Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("H" & Rows.Count).End(xlUp).Row
    For I = 20600 To IRow
    
        If Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("S" & I).Value < 50 And _
        Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("U" & I).Value < 50 And _
        Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("T" & I).Value > 80 Then
        Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("L" & I).Value = "next LL is BUY"
        Else: Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("L" & I).Value = " "
    
    If Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("S" & I).Value > 50 And _
    Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("U" & I).Value > 50 And _
    Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("T" & I).Value < 20 Then
    Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("L" & I).Value = "next HH is SELL"
    Else: Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("L" & I).Value = " "
    
    If Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("S" & I).Value > 30 And _
    Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("U" & I).Value < 20 And _
    Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("T" & I).Value < 20 Then
    Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("L" & I).Value = "BULLISH trend"
    Else: Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("L" & I).Value = " "
    
         If Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("S" & I).Value < 70 And _
         Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("U" & I).Value > 80 And _
         Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("T" & I).Value > 80 Then
         Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("L" & I).Value = "DOWN trend"
         Else: Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("L" & I).Value = " "
    
      End If
      End If
      End If
      End If
      Next I
      ''Call DSRBSCOLOR
       With Application
            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
            .DisplayStatusBar = True
            .EnableEvents = True
            .PrintCommunication = True
        End With
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,065

    Re: IF-THEN-ELSE - how i reduce size of the prodedure

    No VBA expert but there's a command called CASE that may solve this
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: IF-THEN-ELSE - how i reduce size of the prodedure

    One way to get rid of excess statements is by using 'With', as you have done near the end of the posted block

    Option Explicit
    
    Sub RSIDSTOBUYSELL()
       With Application
          Application.Calculation = xlCalculationManual
          Application.ScreenUpdating = False
          Application.DisplayStatusBar = False
          Application.EnableEvents = False
          Application.PrintCommunication = False
       End With
    
       Dim I                As Long
       Dim IRow             As Long
       Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Select
       IRow = Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1").Range("H" & Rows.Count).End(xlUp).Row
    
       With Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1")
          For I = 20600 To IRow
    
             If .Range("S" & I).Value < 50 And _
                .Range("U" & I).Value < 50 And _
                .Range("T" & I).Value > 80 Then
                .Range("L" & I).Value = "next LL is BUY"
             Else: .Range("L" & I).Value = " "
    
                If .Range("S" & I).Value > 50 And _
                   .Range("U" & I).Value > 50 And _
                   .Range("T" & I).Value < 20 Then
                   .Range("L" & I).Value = "next HH is SELL"
                Else: .Range("L" & I).Value = " "
    
                   If .Range("S" & I).Value > 30 And _
                      .Range("U" & I).Value < 20 And _
                      .Range("T" & I).Value < 20 Then
                      .Range("L" & I).Value = "BULLISH trend"
                   Else: .Range("L" & I).Value = " "
    
                      If .Range("S" & I).Value < 70 And _
                         .Range("U" & I).Value > 80 And _
                         .Range("T" & I).Value > 80 Then
                         .Range("L" & I).Value = "DOWN trend"
                      Else: .Range("L" & I).Value = " "
    
                      End If
                End If
             End If
          End If
       Next I
       End With
       
       ''Call DSRBSCOLOR
       With Application
          .Calculation = xlCalculationAutomatic
          .ScreenUpdating = True
          .DisplayStatusBar = True
          .EnableEvents = True
          .PrintCommunication = True
       End With
    
    End Sub
    Although it does not help shorten code, you should learn to indent. It helps make the logic of the code clearer
    Last edited by cytop; 07-04-2017 at 11:25 AM.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,936

    Re: IF-THEN-ELSE - how i reduce size of the prodedure

    Further to what cytop did, you can shorten code a bit more.

    1. You don't need select the sheet as code specifies which sheet to run code on.

    2. Your If statements can be combined into one If statement with Elseif arguments. Reducing "Else.." to single line, instead of repeating at each If statement.

    3. Move Optimization codes to another sub routine and call it at start and end (using Boolean switch).

    Code becomes something like...
    Option Explicit
    
    Sub RSIDSTOBUYSELL()
        Dim I                As Long
        Dim IRow             As Long
        
        OptimizeCode True
        With Workbooks("01A  data 1 MT --1.xlsm").Sheets("DATAIND1")
            IRow = .Range("H" & Rows.Count).End(xlUp).Row
            For I = 20600 To IRow
                If .Range("S" & I).Value < 50 And _
                 .Range("U" & I).Value < 50 And _
                 .Range("T" & I).Value > 80 Then
                    .Range("L" & I).Value = "next LL is BUY"
                ElseIf .Range("S" & I).Value > 50 And _
                 .Range("U" & I).Value > 50 And _
                 .Range("T" & I).Value < 20 Then
                    .Range("L" & I).Value = "next HH is SELL"
                ElseIf .Range("S" & I).Value > 30 And _
                 .Range("U" & I).Value < 20 And _
                 .Range("T" & I).Value < 20 Then
                 .Range("L" & I).Value = "BULLISH trend"
                ElseIf .Range("S" & I).Value < 70 And _
                 .Range("U" & I).Value > 80 And _
                 .Range("T" & I).Value > 80 Then
                 .Range("L" & I).Value = "DOWN trend"
                Else: .Range("L" & I).Value = " "
                End If
            Next I
       End With
       
       ''Call DSRBSCOLOR
       OptimizeCode False
    End Sub
    
    Sub OptimizeCode(switch As Boolean)
       With Application
          .Calculation = IIf(switch, xlCalculationManual, xlCalculationAutomatic)
          .ScreenUpdating = Not (switch)
          .DisplayStatusBar = Not (switch)
          .EnableEvents = Not (switch)
          .PrintCommunication = Not (switch)
       End With
    End Sub
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 9
    Last Post: 05-18-2023, 07:18 PM
  2. [SOLVED] how i reduce size of the prodedure
    By karurkumar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-18-2017, 11:28 PM
  3. Spreadsheet size is too large - any tips to reduce the size?
    By Jessica.Bush in forum Excel General
    Replies: 9
    Last Post: 11-11-2015, 02:45 PM
  4. [SOLVED] Reduce File Size
    By Perk1961 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-25-2015, 11:38 AM
  5. Need to reduce file size
    By cuznleroy in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-18-2014, 12:24 PM
  6. Replies: 1
    Last Post: 12-17-2013, 03:18 PM
  7. Reduce File Size
    By djfatboyfats in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-18-2008, 09:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1