+ Reply to Thread
Results 1 to 6 of 6

USE ALL CODES in 1 code

Hybrid View

fakhteh USE ALL CODES in 1 code 05-20-2020, 04:01 PM
Kaper Re: USE ALL CODES in 1 code 05-21-2020, 12:57 PM
fakhteh Re: USE ALL CODES in 1 code 05-22-2020, 03:20 AM
Sintek Re: USE ALL CODES in 1 code 05-24-2020, 03:29 AM
fakhteh Re: USE ALL CODES in 1 code 05-25-2020, 09:51 AM
Sintek Re: USE ALL CODES in 1 code 05-25-2020, 10:02 AM
  1. #1
    Forum Contributor
    Join Date
    08-06-2014
    Location
    iran
    MS-Off Ver
    2016
    Posts
    110

    USE ALL CODES in 1 code

    hi can any body help me
    i run allsheets code as below and it takes long time for example i had 800 sheets and it takes time about 4 hours of me
    i think it can be shorter and better
    please help me to gather all in one code
    Sub formula()
    
    
    LR = Cells(Rows.Count, "a").End(xlUp).Row
    Set yl = Range("b1:b" & LR)
    
    yl.Copy Range("q1")
    
    Range("m2:m" & LR) = "=IF(L2-E2=L2 ,"""",IF(L2-E2=-E2,"""",L2-E2))"
    
    Range("q1:q" & LR).RemoveDuplicates Columns:=1, Header:=xlYes
    
    clr = Cells(Rows.Count, "q").End(xlUp).Row
    Range("r1") = "Average"
    Range("r2:r" & clr) = "=AVERAGEIFS($M$2:$M$" & LR & ",$B$2:$B$" & LR & ",Q2)"
    
    End Sub
    
    
    
    
    Sub Chart1()
    
    Dim ydata As Range
    Dim xdata As Range
        
    Set ch = ActiveSheet.ChartObjects.Add(Left:=100, Width:=400, Top:=100, Height:=250)
    LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "q").End(xlUp).Row
    Set xdata = ActiveSheet.Range("Q2:Q" & LastRow)
    Set ydata = ActiveSheet.Range("R2:R" & LastRow)
        
    With ch.Chart
        .SeriesCollection.NewSeries
        .FullSeriesCollection(1).XValues = xdata
        .FullSeriesCollection(1).Values = ydata
        .ChartType = xlXYScatterLines
        .ChartArea.Border.Color = vbRed
        .ChartArea.Border.Weight = 3.25
        
        ActiveWorkbook.Save
    End With
       
    End Sub
    
    
    Sub ReplaceDivError()
    
    Dim r As Range
    Dim c As Range
    Dim lr As Integer
    
    lr = Cells(Rows.Count, "r").End(xlUp).Row
    Set r = Range("r2:r" & lr)
    
    For Each c In r
    
    If IsError(c.Value) Then
    If c.Value = CVErr(xlErrDiv0) Then
    c.Value = ""
    End If
    End If
    
    Next c
    
    End Sub
    
    
    Sub AllSheets()
    
    For Each ws In ThisWorkbook.Worksheets
        ws.Activate
        Call formula
        Call Chart1
        Call ReplaceDivError
       
    Next ws
    
    End Sub
    Than k you so much

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: USE ALL CODES in 1 code

    I'd take such measures to make it quicker
    in AllSheets
    application.screenupdating = false ' at the beginning
    '...
    application.screenupdating = true ' at the end
    Swithing off automatic recalculation and calling
    activesheet.calculate
    only after
    Range("m2:m" & LR)
    is filled with formula, then after
    Range("r2:r" & clr)
    is filled with formula
    If you don't need formulas in M and R columns I'd convert (after recalculation) these columns formulas -> to values, like:

    Range("m2:m" & LR) = "=IF(L2-E2=L2 ,"""",IF(L2-E2=-E2,"""",L2-E2))"
    Activesheet.calculate
    Range("m2:m" & LR).value = Range("m2:m" & LR).value
    I'd not use
    ActiveWorkbook.Save
    after each graph is prepared (it's 800 large workbook saving actions!)

    and as for errors searching and replacing - if you could allow all errors (not only #DIV/0!) to be replaced with "" then

    Sub ReplaceDivError()
    
    Dim r As Range
    Dim lr As Integer
    
    lr = Cells(Rows.Count, "r").End(xlUp).Row
    Set r = Range("r2:r" & lr).SpecialCells(xlCellTypeFormulas, 16) 'this could be skipped if column R was transformed to values before
    if not r is nothing then 'and this
      r.value - "" 'and this
    else 'and this
      Set r = Range("r2:r" & lr).SpecialCells(xlCellTypeConstants, 16)
      if not r is nothing then r.value - ""
    end if ' and this
    
    End Sub
    PS. Just putting all actions into one sub (which is perfectly possible) will save you no noticeable time amount. But the proposed ammendments could save a lot. Of courrse 800 sheets (with 800 graphs!) is still very large file and some time will be needed to process it.
    Last edited by Kaper; 05-21-2020 at 12:59 PM.
    Best Regards,

    Kaper

  3. #3
    Forum Contributor
    Join Date
    08-06-2014
    Location
    iran
    MS-Off Ver
    2016
    Posts
    110

    Re: USE ALL CODES in 1 code

    Hi Mr Kaper
    please use this workbook for running your code and compare its result with my previous code (i should say that it is not my code some one else wrote it and i appreciate him or her same as you)
    thank you
    Attached Files Attached Files

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: USE ALL CODES in 1 code

    800 sheets - 4 Hours...
    As per Kaper suggestions...Down to 8 seconds...
    I don't see the need for Error delete code...Perhaps formula need to be adjusted to ensure no errors in formula...
    can only see once a sample file is uploaded containing such errors...
    Option Explicit
    
    Sub AllSheets()
    Dim ws As Worksheet, ch As Object, lr As Long, clr As Long
    Dim ydata As Range, xdata As Range, cell As Range
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    For Each ws In ThisWorkbook.Worksheets
        With ws
            ' ! Formula Code...............................................
            lr = .Cells(.Rows.Count, 1).End(xlUp).Row
            .Range(.Cells(1, 2), .Cells(lr, 2)).AdvancedFilter xlFilterCopy, , .Range("Q1"), True
            .Range("M2:M" & lr).formula = "=IF(L2-E2=L2 ,"""",IF(L2-E2=-E2,"""",L2-E2))"
            clr = .Cells(Rows.Count, 17).End(xlUp).Row
            .Range("R1") = "Average"
            .Range("R2:R" & clr).formula = "=AVERAGEIFS($M$2:$M$" & lr & ",$B$2:$B$" & lr & ",Q2)"
            
            ' ! Chart Code......................................................
            Set cell = .Range("N20") ' ! Start Position of Chart
            Set ch = .ChartObjects.Add(Left:=cell.Left, Width:=400, Top:=cell.Top, Height:=250)
            lr = .Cells(.Rows.Count, 17).End(xlUp).Row
            Set xdata = .Range("Q2:Q" & lr): Set ydata = .Range("R2:R" & lr)
            With ch.Chart
                .ChartType = xlXYScatterLines
                .SeriesCollection.NewSeries
                With .SeriesCollection(1): .XValues = xdata: .Values = ydata: End With
                With .ChartArea.Border: .Color = vbRed: .Weight = 3.25: End With
              End With
        End With
    Next ws
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  5. #5
    Forum Contributor
    Join Date
    08-06-2014
    Location
    iran
    MS-Off Ver
    2016
    Posts
    110

    Re: USE ALL CODES in 1 code

    Hi Mr Sintek
    your code could be acceptable if the DivError not exist so please check your previous code and put this part (ReplaceDivError) in it .
    i attached an example for this error.
    thank you
    Attached Files Attached Files

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: USE ALL CODES in 1 code

    if you want to clear errors just change formula...
     .Range("R2:R" & clr).Formula = "=IFERROR(AVERAGEIFS($M$2:$M$" & lr & ",$B$2:$B$" & lr & ",Q2),"""")"

+ 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. [SOLVED] Need advice on VBA code in sheet / 2 different codes
    By Chu3 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-25-2019, 09:57 AM
  2. [SOLVED] Merging Two VBA Codes to one Code
    By mvinay in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-22-2019, 01:30 AM
  3. Testing single code, work fine. Put multiple codes in one sheet, one code doesn't work.
    By MayDay1988 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-30-2017, 06:14 PM
  4. Replies: 7
    Last Post: 08-13-2015, 10:58 AM
  5. How to connect these two codes into one code.
    By Zahhhaaaa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-21-2011, 07:22 AM
  6. Code 3 of 9 bar codes and Excel
    By iIgnite in forum Excel General
    Replies: 2
    Last Post: 06-14-2011, 02:37 AM
  7. Merging Two Codes Into one code
    By LoveCandle in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-27-2005, 12:13 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