473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. Sub Macro3()
  2. '
  3. ' Macro3 Macro
  4. '
  5.     Dim i As Integer
  6.     i = 1
  7.     For i = 1 To 10
  8.         If (Range("A" & i).Select = Range("B" & i).Select) Then
  9.             Range("A" & i).Select
  10.             Selection.Copy
  11.             Range("F" & i).Select
  12.             ActiveSheet.Paste
  13.         End If
  14.     Next i
  15.  
  16.  
  17. End Sub
Oct 26 '11 #1
4 2270
Guido Geurs
767 Expert 512MB
Don't use .Select but .Value
Don't Copy and Paste but set directly the value with .Offset
Expand|Select|Wrap|Line Numbers
  1.     Dim i As Integer
  2.     i = 1
  3.     For i = 1 To 10
  4.         If (Range("A" & i).Value = Range("B" & i).Value) Then _
  5.             Range("A" & i).Offset(0, 5) = Range("A" & i).Value
  6.     Next i
Oct 26 '11 #2
@Guido Geurs:- thanks for ur useful suggestion
Oct 26 '11 #3
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
Expand|Select|Wrap|Line Numbers
  1. Sub Macro3()
  2. '
  3. ' Macro3 Macro
  4. '
  5.     Dim a As Integer
  6.     Dim j As Integer
  7.     Windows("ptimesheet.xlsm").Activate
  8.     Dim B As String
  9.     Dim TRACKERNAME As String
  10.     Dim TRACKERDATE As Date
  11.     Dim TRACKERACTIVITY As String
  12.     Dim TRACKERSUBACTIVITY As String
  13.     Dim CAPACITY_DATE As Date
  14.     Dim CAPACITY_ACTIVITY As String
  15.     Dim CAPACITY_SUBACTIVITY As String
  16.     Dim ACTUALEFFORTS As Double
  17.     Dim ACTUALEFFORTS1 As Double
  18.     Dim rowcount_capacity As Integer
  19.     Dim rowcount_tracker As Integer
  20.     ACTUALEFFORTS1 = 0
  21.     Windows("E4402_PCG_Capacity Planning_IML_Oct11.xlsm").Activate
  22.     rowcount_capacity = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
  23.     For a = 3 To rowcount_capacity
  24.         Windows("E4402_PCG_Capacity Planning_IML_Oct11.xlsm").Activate
  25.         CAPACITY_DATE = Range("A" & a).Value
  26.         CAPACITY_NAME = Range("C" & a).Value
  27.         CAPACITY_ACTIVITY = Range("F" & a).Value
  28.         CAPACITY_SUBACTIVITY = Range("G" & a).Value
  29.        Windows("ptimesheet.xlsm").Activate
  30.        rowcount_tracker = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
  31.         For j = 1 To rowcount_tracker
  32.            Windows("ptimesheet.xlsm").Activate
  33.            TRACKERDATE = Range("A" & j).Value
  34.            TRACKERNAME = Range("H" & j).Value
  35.            TRACKERACTIVITY = Range("B" & j).Value
  36.            TRACKERSUBACTIVITY = Range("C" & j).Value
  37.             On Error Resume Next
  38.             Windows("E4402_PCG_Capacity Planning_IML_Oct11.xlsm").Activate
  39.                 If CAPACITY_DATE = TRACKERDATE Then
  40.                     If CAPACITY_NAME = TRACKERNAME Then
  41.                         If CAPACITY_ACTIVITY = TRACKERACTIVITY Then
  42.                             If CAPACITY_SUBACTIVITY = TRACKERSUBACTIVITY Then
  43.                                 Windows("ptimesheet.xlsm").Activate
  44.                                 ACTUALEFFORTS = Range("F" & j).Value
  45.                                 ACTUALEFFORTS1 = ACTUALEFFORTS + ACTUALEFFORTS1
  46.                                 Windows("E4402_PCG_Capacity Planning_IML_Oct11.xlsm").Activate
  47.                                 Range("L" & a).Value = ACTUALEFFORTS1
  48.                                 ACTUALEFFORTS = 0
  49.                                 ACTUALEFFORTS1 = 0
  50.                              End If
  51.                         End If
  52.                     End If
  53.                 End If
  54.         Next j
  55.     Next a
  56.  
  57.  
  58. End Sub
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
Oct 26 '11 #4
Guido Geurs
767 Expert 512MB
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.
Oct 29 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
4
by: Mike Duffy | last post by:
I just recently realized that the comparison operator "is" actually works for comparing numeric values. Now, I know that its intended use is for testing object identity, but I have used it for a...
5
by: Jim Devenish | last post by:
I want to create a column alias to represent the comparison of two columns (ie a boolean result of True or False). A simple example is: Select VehicleFinanceID, SalePrice > PurchasePrice As...
4
by: michael.pearmain | last post by:
Hi Experts, Looking for a very quick bit on of advice on how to make some python code run. I'm a newbie to both VBA and Python, so i apologise if this is very easy but i'm about to tear my hair...
1
by: putermikey | last post by:
Hi All New to Forum, Need a little help with my script if you could any one can help. I have created a script to look for dates. current date and past 5 dates. the script only works if the...
2
by: sake | last post by:
Simply put, I need to know how to overload a comparison operator like ==. Assuming I have a function that compares to of my objects: obj1, and obj2. This function returns 0 if obj1 and obj2 are...
1
by: Praveen James | last post by:
Hi, when I execute a T-SQL command (which have IF Condition or CASE WHEN THEN statement) using excel macros I am getting a error "Item Not found in the collection.....". But the vba excel macro is...
2
by: modularmix | last post by:
Does anyone know the code for running the Excel Macro for two different spreadsheets in parallel. Here is the code that works for them sequentially. Workbooks.Open Filename:="C:\Documents and...
3
by: travish | last post by:
Is the stringize operator for macros commonly/correctly implemented on compilers advertising ANSI C compatibility? Example: #define STRINGIZE(ARG) ARG = #ARG void main() { char *test;
1
by: pnalla | last post by:
hi how to create a word documetn using Excel macros.. i have some code but it crating a word document but it isnot close properly whats the proble. please suggest me. thanks prasad
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.