Consulting

Results 1 to 11 of 11

Thread: Solved: Speeding up routine

  1. #1

    Solved: Speeding up routine

    First of all , congratulations for this top programming forum , and sorry if my english is not so clear , anyway I go straight to the point: I have the following routine (part of the code was picked up from this forum)that I use to compare two different column and for reporting (if values matches) the value in the first range under a specific column .
    The problem is:

    1) Is it possible to speed-up the code (basically I compare column with 4000-8000 records) It takes too long.
    2) it could happen that one of the two columns need to be trimmed but in this case trim seems not working.Why?
    3) Is It possible to set a sort of progressive bar or something like that (point 3 is not really important)to see the status.
    I have not so much experience in VBA (sorry) , here is the code:

    [VBA]
    Sub Mousepare()



    Const strTitleSelectRange_c As String = "Select Range"
    Dim rng1 As Excel.Range
    Dim rng2 As Excel.Range
    Dim cella As Object
    Dim cella2 As Object
    Dim C As String
    Dim R As Integer
    Dim c1r As Integer
    Dim c2r As Integer
    Dim c1c As Integer
    Dim c2c As Integer
    Dim tot As Integer


    ' Definisci i range
    Set rng1 = GetRange("Select range n?1 with mouse:", strTitleSelectRange_c)
    Set rng2 = GetRange("Select range n?2 with mouse :", strTitleSelectRange_c)
    R = InputBox("Select offset (number value) to report:")
    C = InputBox("Select column where report the value:")
    contatore = 1
    For Each cella In rng1
    For Each cella2 In rng2


    Trim (cella.Value)
    Trim (cella2.Value)
    c1r = cella.Row
    c2r = cella2.Row
    'c1c = cella.Column
    'c2c = cella.Column





    If cella2 = cella Then
    'Range(C & c1r).Value = cella2.Offset(0, 1).Value
    Range(C & c1r).Value = cella2.Offset(0, R).Value

    End If
    contatore = contatore + 1
    Next cella2
    Next cella


    End Sub
    Private Function GetRange(Prompt As String, Title As String) As Excel.Range
    On Error Resume Next
    Const lngRange_c As Long = 8
    Set GetRange = Excel.Application.InputBox(Prompt, Title, Type:=lngRange_c)
    End Function
    [/VBA]
    Best Regards
    Tuxy(Italy)

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    A couple of thoughts

    [vba]
    Sub Mousepare()
    Const strTitleSelectRange_c As String = "Select Range"
    Dim rng1 As Excel.Range
    Dim rng2 As Excel.Range
    Dim cella As Object
    Dim cella2 As Object
    Dim C As String
    Dim R As Integer
    Dim c1r As Integer
    Dim c2r As Integer
    Dim c1c As Integer
    Dim c2c As Integer
    Dim tot As Integer

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    ' Definisci i range
    Set rng1 = GetRange("Select range n?1 with mouse:", strTitleSelectRange_c)
    Set rng2 = GetRange("Select range n?2 with mouse :", strTitleSelectRange_c)
    R = InputBox("Select offset (number value) to report:")
    C = InputBox("Select column where report the value:")
    contatore = 1
    For Each cella In rng1

    For Each cella2 In rng2

    Trim (cella.Value)
    cella.Value = Replace(cella.Value, Chr(160), "")
    Trim (cella2.Value)
    cella2.Value = Replace(cella2.Value, Chr(160), "")
    c1r = cella.Row
    c2r = cella2.Row
    'c1c = cella.Column
    'c2c = cella.Column

    If cella2 = cella Then

    'Range(C & c1r).Value = cella2.Offset(0, 1).Value
    Range(C & c1r).Value = cella2.Offset(0, R).Value
    End If

    contatore = contatore + 1
    Next cella2
    Next cella

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    End Sub
    Private Function GetRange(Prompt As String, Title As String) As Excel.Range
    On Error Resume Next
    Const lngRange_c As Long = 8
    Set GetRange = Excel.Application.InputBox(Prompt, Title, Type:=lngRange_c)
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3

    re

    Quote Originally Posted by xld
    A couple of thoughts

    [vba]
    Sub Mousepare()
    Const strTitleSelectRange_c As String = "Select Range"
    Dim rng1 As Excel.Range
    Dim rng2 As Excel.Range
    Dim cella As Object
    Dim cella2 As Object
    Dim C As String
    Dim R As Integer
    Dim c1r As Integer
    Dim c2r As Integer
    Dim c1c As Integer
    Dim c2c As Integer
    Dim tot As Integer

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    ' Definisci i range
    Set rng1 = GetRange("Select range n?1 with mouse:", strTitleSelectRange_c)
    Set rng2 = GetRange("Select range n?2 with mouse :", strTitleSelectRange_c)
    R = InputBox("Select offset (number value) to report:")
    C = InputBox("Select column where report the value:")
    contatore = 1
    For Each cella In rng1

    For Each cella2 In rng2

    Trim (cella.Value)
    cella.Value = Replace(cella.Value, Chr(160), "")
    Trim (cella2.Value)
    cella2.Value = Replace(cella2.Value, Chr(160), "")
    c1r = cella.Row
    c2r = cella2.Row
    'c1c = cella.Column
    'c2c = cella.Column

    If cella2 = cella Then

    'Range(C & c1r).Value = cella2.Offset(0, 1).Value
    Range(C & c1r).Value = cella2.Offset(0, R).Value
    End If

    contatore = contatore + 1
    Next cella2
    Next cella

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    End Sub
    Private Function GetRange(Prompt As String, Title As String) As Excel.Range
    On Error Resume Next
    Const lngRange_c As Long = 8
    Set GetRange = Excel.Application.InputBox(Prompt, Title, Type:=lngRange_c)
    End Function
    [/vba]
    First of all thank you for your help.

    I had to disable this part of the code because it' impossible to select range using the mouse:
    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    Trim is not working if I compare a column with the value = A with another with value = space A match is skipped and the corrisponding value is not reported.
    But the main problem remains the same too , too slow.

    Best Regards.
    Roberto

  4. #4
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi tuxy,

    I moved the screen and calculation lines so they will work for you.

    and changed the 'Trim' Statements


    [vba]
    Sub Mousepare()
    Const strTitleSelectRange_c As String = "Select Range"
    Dim rng1 As Excel.Range
    Dim rng2 As Excel.Range
    Dim cella As Object
    Dim cella2 As Object
    Dim C As String
    Dim R As Integer
    Dim c1r As Integer
    Dim c2r As Integer
    Dim c1c As Integer
    Dim c2c As Integer
    Dim tot As Integer


    ' Definisci i range
    Set rng1 = GetRange("Select range n?1 with mouse:", strTitleSelectRange_c)
    Set rng2 = GetRange("Select range n?2 with mouse :", strTitleSelectRange_c)
    R = InputBox("Select offset (number value) to report:")
    C = InputBox("Select column where report the value:")
    contatore = 1


    '//MOVED TO HERE to allow for mouse selection

    With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    '//Do global replace
    rng1.Replace What:=Chr(160), Replacement:=""
    rng2.Replace What:=Chr(160), Replacement:=""

    For Each cella In rng1
    '//Do once
    cella = Trim (cella)

    For Each cella2 In rng2

    cella2 = Trim (cella2)
    c1r = cella.Row
    c2r = cella2.Row
    'c1c = cella.Column
    'c2c = cella.Column

    If cella2 = cella Then

    'Range(C & c1r).Value = cella2.Offset(0, 1).Value
    Range(C & c1r).Value = cella2.Offset(0, R).Value
    End If

    contatore = contatore + 1
    Next cella2
    Next cella

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    End Sub
    [/vba]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  5. #5

    re

    Thak you very much , I'll try it , in the meanwhile I modified the code as follow
    [vba]
    Private Sub CommandButton1_Click()

    ' Definisco le variabili
    Const strTitleSelectRange_c As String = "Select Range"
    Dim rng1 As Excel.Range
    Dim rng2 As Excel.Range
    Dim cella As Object
    Dim cella2 As Object
    Dim C As String
    Dim R As Integer
    Dim c1r As Integer
    Dim c2r As Integer
    Dim c1c As Integer
    Dim c2c As Integer
    ' Definisci i range
    Set rng1 = GetRange("Selezionare con il mouse il primo range di ricerca:", strTitleSelectRange_c)
    Set rng2 = GetRange("Selezionare con il mouse il secondo range di ricerca:", strTitleSelectRange_c)
    R = InputBox("Indicare Colonna (valore numerico) da riportare:")
    C = InputBox("Indicare Colonna sotto cui riportare il dato:")
    'For Each cella In rng1
    ''For Each cella2 In rng2
    'Trim (cella.Value)
    'Trim (cella2.Value)
    'c1r = cella.Row
    'c2r = cella2.Row
    'c1c = cella.Column
    'c2c = cella.Column
    For Each cella In rng1
    For Each cella2 In rng2
    If Application.CountIf(rng2, Trim(cella.Value)) > 0 Then
    c1r = cella.Row

    'Cells(CL.Row, C).Value = "OK"
    'Else
    'Cells(CL.Row, C).Value = "NO in " & NF2
    'End If
    'Next
    'If cella2 = cella Then
    'Range(C & c1r).Value = cella2.Offset(0, 1).Value
    'Range(C & c1r).Value = cella2.Offset(0, R).Value
    'my problem is here I can get the different ‘value of the matching but only the first one
    Range(C & c1r).Value = rng2.Cells.Offset(0, R).Value
    End If

    Next cella2

    Next cella
    End Sub
    Private Function GetRange(Prompt As String, Title As String) As Excel.Range
    On Error Resume Next
    Const lngRange_c As Long = 8
    Set GetRange = Excel.Application.InputBox(Prompt, Title, Type:=lngRange_c)
    End Function
    [/vba]

    Now the routine is faster and faster but I can get the right value but only the first one , am I on the right path? It seems to me that the istruction If application.count could be the solution for real speeding up
    This forum is amazing and also the people around it

  6. #6
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi tuxy.

    Why not post a small sample workbook so we can see just what it is you're trying to do. I can't figure it out...
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Tuxy,
    When you post code, select it and click the VBA button to format it as shown. I makes it more readable
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8

    re

    Here is an example of what I'm going to do:

    1)launch macro1
    2)select first range (sheet "a")
    3)select 2? range (sheet "b")
    4)input 1 when prompted
    5)input column B or C........

    I also set a progressive bar into the status bar (please set status bar as visible)

    Best Regards
    Thank you very much for your cooperation

  9. #9
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi tuxy,

    I think I understand now. You want to select Sheet A value, compare it to Sheet 2 value and if a match is found copy the offset value to Sheet 1. Is that correct?

    I ran your code and it completed in 4:57

    I updated it with Screenupdating turned off and it completed in 3:45 ( a savings of a whopping 1:12)

    I rewrote the code and it runs in 00:03.


    NOTE: Your code has no routine to handle duplicates. So I assume the codes will ALWAYS be unique. If dupes exist, your code will overwrite the first value with the next, etc until only the last value is displayed.

    I did not provide any code to handle dupes, either...
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  10. #10

    Wow!!!

    Rbrhodes , you did it!!!!!!! , congratulations..................amazing job
    THANK YOU ,
    YOU ARE GREAT!!!!!!!!!!!

  11. #11
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    You're welcome!

    dr
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

Posting Permissions

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