+ Reply to Thread
Results 1 to 12 of 12

macro work but very slow

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-14-2010
    Location
    oman
    MS-Off Ver
    Excel 2003
    Posts
    384

    Post macro work but very slow

    Sub Equal()
    
    Dim LR As Long, c As Range
    For Each c In Range("D3:G300")
        c.Value = Application.WorksheetFunction.Clean(Trim(c.Value))
    Next c
    LR = Range("J" & Rows.Count).End(xlUp).Row
    For Each c In Range("J3:J" & LR)
       If Left(c.Formula, 1) <> "=" Then c.Value = "=" & c.Value
    Next c
    
    Range("E2:E500").Select
    
        Selection.Replace What:="-", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False
    Range("D2:H500").Select
    Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False
    
    
    End Sub
    dear friends i don't have VB knowledge.i have found two macro from net & i have make above macro.it's work but it's take too much time.pls any body can remake this macro to run fast.thanks.
    Last edited by johncena; 04-04-2010 at 11:48 AM.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: macro work but very slow

    The Slow part is

    For Each c In Range("D3:G300")
        c.Value = Application.WorksheetFunction.Clean(Trim(c.Value))
    Next c
    Do you have to loop through all 1192 cells in this range?

  3. #3
    Forum Contributor
    Join Date
    02-14-2010
    Location
    oman
    MS-Off Ver
    Excel 2003
    Posts
    384

    Re: macro work but very slow

    dear Marcol my data range is not fix.some time it's 5 rows,some time it's 100 rows.pls u can make this to run in used data range.pls help me.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: macro work but very slow

    Move your LR resolution up to the top and then use it in the first FOR/EACH like you did for the second. That should help some.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Contributor
    Join Date
    02-14-2010
    Location
    oman
    MS-Off Ver
    Excel 2003
    Posts
    384

    Re: macro work but very slow

    sir JBeaucaire thanks for your reply.i am downloading data from my system to excel.after that i have tp copy that data to my worksheet.on that data i have try to run my macro & it's not work.then my friend told me with that data has some extra space before running the macro i should remove that space.for that i try above macro first part & it's work..
    second part u can understand i want to add each values in column "J " to equal " = "
    that last part i have take from macro record & i want to remove column "E" all " - ",column "D to H" all space also i want to remove.
    i think now u have some idea about my macro...

  6. #6
    Forum Expert contaminated's Avatar
    Join Date
    05-07-2009
    Location
    Baku, Azerbaijan
    MS-Off Ver
    Excel 2013
    Posts
    1,430

    Re: macro work but very slow

    maybe to use

    For Each c In Range("D3:G300").SpecialCells (xlCellTypeConstants)
        c.Value = Application.WorksheetFunction.Clean(Trim(c.Value))
    Next c
    Люди, питающие благие намерения, как раз и становятся чудовищами.

    Regards, ?Born in USSR?
    Vusal M Dadashev

    Baku, Azerbaijan

  7. #7
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: macro work but very slow

    Or ... when using ws functions

      With Range("D3:G300")
        .Value = Evaluate("IF(ROW(" & .Address & "),clean(trim(" & .Address & ")))")
      End With
    .
    This will give you a much better performance.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  8. #8
    Forum Contributor
    Join Date
    02-14-2010
    Location
    oman
    MS-Off Ver
    Excel 2003
    Posts
    384

    Re: macro work but very slow

    thanks u very much for your all helps.pls any body can give me the complete macro code.thanks.

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: macro work but very slow

    In response to your PM

    Try this, it uses rwgrietvelds' suggestion and a bit of guess work

    Sub Equal()
        Dim myRange As Range, c As Range
        Dim LR As Long
        
        Set myRange = ActiveSheet.UsedRange
        
        With Range(myRange.Address)
          .Value = Evaluate("IF(ROW(" & .Address & "),clean(trim(" & .Address & ")))")
        End With
        
        LR = Range("J" & Rows.Count).End(xlUp).Row
        For Each c In Range("J2:J" & LR)
           If c.Formula = True Then c.Value = c.Value
        Next c
        
        LR = Range("E" & Rows.Count).End(xlUp).Row
        Range("E2:E" & LR).Replace What:="-", Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False
                
        Range("D2:H500").Replace What:=" ", Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False
    
        Set myRange = Nothing
    End Sub

    A sample workbook would help to clear up any guesswork.

    Cheers

  10. #10
    Forum Contributor
    Join Date
    02-14-2010
    Location
    oman
    MS-Off Ver
    Excel 2003
    Posts
    384

    Post Re: macro work but very slow

    thanks sir Marcol.it's almost work.some cells has data like this.'12/2.i have used " ' " (apostrophe) before typing 12/2 because other wise it's automatically change to date format like this." 2-Dec ".after running this macro.that my apostrophe disappear & it's again change to " 2-Dec ".
    this part i want to run only in column "D" to "G" range only.i don't want to run whole sheet..
     With Range(myRange.Address)
          .Value = Evaluate("IF(ROW(" & .Address & "),clean(trim(" & .Address & ")))")
        End With
    Last edited by johncena; 04-04-2010 at 10:41 AM.

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: macro work but very slow

    Try this

    Option Explicit
    
    Sub Equal()
        Dim c As Range
        Dim LR As Long, maxLR As Long, colNo As Long
        
        For colNo = Range("D:G").Column To Range("D:G").Column + Range("D:G").Columns.Count - 1
            LR = Cells(Rows.Count, colNo).End(xlUp).Row
            If LR > maxLR Then maxLR = LR
        Next
        LR = maxLR
        
        With Range("D2:G" & LR)
          .Value = Evaluate("IF(ROW(" & .Address & "),clean(trim(" & .Address & ")))")
        End With
        
        LR = Range("J" & Rows.Count).End(xlUp).Row
        For Each c In Range("J2:J" & LR)
           If c.Formula = True Then c.Value = c.Value
        Next c
        
        LR = Range("E" & Rows.Count).End(xlUp).Row
        Range("E2:E" & LR).Replace What:="-", Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False
                
        For colNo = Range("D:H").Column To Range("D:H").Column + Range("D:H").Columns.Count - 1
            LR = Cells(Rows.Count, colNo).End(xlUp).Row
            If LR > maxLR Then maxLR = LR
        Next
        LR = maxLR
        
        Range("D2:H" & LR).Replace What:=" ", Replacement:="", LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False
    
    End Sub
    If this doesn't work

    A sample workbook would help to clear up any guesswork

    Cheers

    EDIT:

    Missing lines

    LR = maxLR
    Sorry for that.

    Better if each section is written as a function.
    Last edited by Marcol; 04-05-2010 at 04:28 AM. Reason: Missed 2No. lines in code

  12. #12
    Forum Contributor
    Join Date
    02-14-2010
    Location
    oman
    MS-Off Ver
    Excel 2003
    Posts
    384

    Re: macro work but very slow

    thanks u very much sir Marcol.it's work very well.thanks a lot again.REP+

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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