Comparison operator in excel macros

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vensriram
    New Member
    • Aug 2010
    • 48

    Comparison operator in excel macros

    I am trying to compare each cell in a i.e. A1 with B1 and if it is true i will populate the cell F1 with A1 value.....But irrespective of my input values the if condition becomes true....can anybody help me in this. My code goes like this
    Code:
    Sub Macro3()
    '
    ' Macro3 Macro
    '
        Dim i As Integer
        i = 1
        For i = 1 To 10
            If (Range("A" & i).Select = Range("B" & i).Select) Then
                Range("A" & i).Select
                Selection.Copy
                Range("F" & i).Select
                ActiveSheet.Paste
            End If
        Next i
        
          
    End Sub
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    Don't use .Select but .Value
    Don't Copy and Paste but set directly the value with .Offset
    Code:
        Dim i As Integer
        i = 1
        For i = 1 To 10
            If (Range("A" & i).Value = Range("B" & i).Value) Then _
                Range("A" & i).Offset(0, 5) = Range("A" & i).Value
        Next i

    Comment

    • vensriram
      New Member
      • Aug 2010
      • 48

      #3
      @Guido Geurs:- thanks for ur useful suggestion

      Comment

      • vensriram
        New Member
        • Aug 2010
        • 48

        #4
        Now i have a problem with the performance. I use 2 excel to perform the task. I compare each row of EXCEL1 with all the rows of EXCEL2.
        So as the number of rows increases my excel crashes due to the for loop. My code is
        Code:
        Sub Macro3()
        '
        ' Macro3 Macro
        '
            Dim a As Integer
            Dim j As Integer
            Windows("ptimesheet.xlsm").Activate
            Dim B As String
            Dim TRACKERNAME As String
            Dim TRACKERDATE As Date
            Dim TRACKERACTIVITY As String
            Dim TRACKERSUBACTIVITY As String
            Dim CAPACITY_DATE As Date
            Dim CAPACITY_ACTIVITY As String
            Dim CAPACITY_SUBACTIVITY As String
            Dim ACTUALEFFORTS As Double
            Dim ACTUALEFFORTS1 As Double
            Dim rowcount_capacity As Integer
            Dim rowcount_tracker As Integer
            ACTUALEFFORTS1 = 0
            Windows("E4402_PCG_Capacity Planning_IML_Oct11.xlsm").Activate
            rowcount_capacity = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
            For a = 3 To rowcount_capacity
                Windows("E4402_PCG_Capacity Planning_IML_Oct11.xlsm").Activate
                CAPACITY_DATE = Range("A" & a).Value
                CAPACITY_NAME = Range("C" & a).Value
                CAPACITY_ACTIVITY = Range("F" & a).Value
                CAPACITY_SUBACTIVITY = Range("G" & a).Value
               Windows("ptimesheet.xlsm").Activate
               rowcount_tracker = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
                For j = 1 To rowcount_tracker
                   Windows("ptimesheet.xlsm").Activate
                   TRACKERDATE = Range("A" & j).Value
                   TRACKERNAME = Range("H" & j).Value
                   TRACKERACTIVITY = Range("B" & j).Value
                   TRACKERSUBACTIVITY = Range("C" & j).Value
                    On Error Resume Next
                    Windows("E4402_PCG_Capacity Planning_IML_Oct11.xlsm").Activate
                        If CAPACITY_DATE = TRACKERDATE Then
                            If CAPACITY_NAME = TRACKERNAME Then
                                If CAPACITY_ACTIVITY = TRACKERACTIVITY Then
                                    If CAPACITY_SUBACTIVITY = TRACKERSUBACTIVITY Then
                                        Windows("ptimesheet.xlsm").Activate
                                        ACTUALEFFORTS = Range("F" & j).Value
                                        ACTUALEFFORTS1 = ACTUALEFFORTS + ACTUALEFFORTS1
                                        Windows("E4402_PCG_Capacity Planning_IML_Oct11.xlsm").Activate
                                        Range("L" & a).Value = ACTUALEFFORTS1
                                        ACTUALEFFORTS = 0
                                        ACTUALEFFORTS1 = 0
                                     End If
                                End If
                            End If
                        End If
                Next j
            Next a
                
              
        End Sub

        Comment

        • Guido Geurs
          Recognized Expert Contributor
          • Oct 2009
          • 767

          #5
          If you use sheets with much rows (records) then it's better to use arrays (tables in the memory): it's much, much faster !!

          Is it possible to attach in Bytes a demo workbook with +- 10 records?
          If it's delicate information you can change it with fictive names and values.
          I just need to see the structure to do some tests.

          Comment

          Working...