Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If then statement

Status
Not open for further replies.

max1565

Technical User
Dec 16, 2002
57
US
I am new to vba and i have a problem. I am trying to compare two cells (b1 and c30 on sheet1) and if the are equal then i want to take a value from a third cell (c35 on shee1) and place it in a fourth cell(b2 on sheet data).

Heres what I thought would do it.

sheets("sheet1").select
if Range("b1").value = Range("c30").value then
Range("c35").copy

Sheets("data").Range("b2").value = sheets("sheet1").Range("c35").value
End if

Could someone please tell me what I am doing wrong?

Thanks
 
sheets("sheet1").select
if Range("b1").value = Range("c30").value then Sheets("data").Range("b2").value = sheets("sheet1").Range("c35").value
End if
 
I wouldn't say you're doing anything wrong. After all, your code works. It's just that you are maybe doing too much. The .copy operation is thrown away. Here's one way to copy the value:
[blue]
Code:
Sub test()
  With Sheets("Sheet1")
    If .Range("B1") = .Range("C30") Then
      Sheets("data").Range("B2") = .Range("C35")
    End If
  End With
End Sub
[/color]


Just for amusement, if you assign range names to the four critical cells, you could do it with just one line of code, this way:
[blue]
Code:
Sub AnotherWay()
  If [FACTOR1] = [FACTOR2] Then [TOHERE] = [COPYFROM]
End Sub
[/color]


 
ETID

Thanks for your response. I entered the following code and it worked for a while and the quit working. Any ideas on what could be wrong?

Thanks

sheets("sheet1").select
if Range("b1").value = Range("c30").value then
Sheets("data").Range("b2").value = sheets("sheet1").Range("c35").value
End if
 
As Zathras pointed out...have you tried dropping the .value and .select
from your code?

i.e.


if sheets("sheet1").Range("b1") = sheets("sheet1").Range("c30")then Sheets("data").Range("b2") = sheets("sheet1").Range("c35")
 
ETID

SORRY TO BE SO MUCH TROUBLE BUT I PUT THE BELOW CODE IN AND IT RAN FINE FOR ONE TIME, THEN WOULD NOT WORK ANYMORE. aNY MORE SUGGESTIONS?

tHANKS
 
can you post the entire sub that have written?
 
i APPRECIATE YOUR HELP. tHE SUB IS AS FOLLOWS:

Sub VDCOGS()
'''''TRANSFER VISTA DIAMANTE COST OF''''''''''''''''
''''''''GOODS SOLD TO DATA SHEET'''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''LOT 1
If Sheets("VISTA DIAMANTE").Range("C30") = Sheets("VISTA DIAMANTE").Range("B1") Then
Sheets("DATA").Range("F8") = Sheets("VISTA DIAMANTE").Range("C45")
End If
''''LOT 2
If Sheets("VISTA DIAMANTE").Range("D30") = Sheets("VISTA DIAMANTE").Range("B1") Then
Sheets("DATA").Range("F9") = Sheets("VISTA DIAMANTE").Range("D45")
End If

''''LOT 3
If Sheets("VISTA DIAMANTE").Range("E30") = Sheets("VISTA DIAMANTE").Range("B1") Then
Sheets("DATA").Range("F10") = Sheets("VISTA DIAMANTE").Range("E45")
End If

''''LOT 4
If Sheets("VISTA DIAMANTE").Range("F30") = Sheets("VISTA DIAMANTE").Range("B1") Then
Sheets("DATA").Range("F11") = Sheets("VISTA DIAMANTE").Range("F45")
End If

''''LOT 5
If Sheets("VISTA DIAMANTE").Range("G30") = Sheets("VISTA DIAMANTE").Range("B1") Then
Sheets("DATA").Range("F12") = Sheets("VISTA DIAMANTE").Range("G45")
End If

Application.Run "VISTA.xls!vdLOTSALES"

End Sub

I CAN EMAIL THE FILE TO YOU IF IT WOULD HELP

tHANKS!!!
 
Is this open when you try to run a second time?

Application.Run "VISTA.xls!vdLOTSALES"
 
tHANKS ETID FOR ALL OF YOUR HELP. i FINALLY FIGURED IT OUT. tHE CELLS i WAS COMPARING TO SEE IF THEY WERE EQUAL WERE NUMBERS. i HAD FAILED TO FORMAT THOSE CELLS AS NUMBERS. oNCE i FORMATED THEM, EVERY THING WORKED.

THANKS A LOT
 
WELL I THOUGHT IT WAS WORKING OKAY. FOR SOME REASON WHEN IT COMPARES THE TWO CELLS (WHICH CONTAINS NUMBERS) IT IS NOT RECOGNIZING THEM AS EQUAL. tHE CELLS ARE FILLED FROM A TEXTBOX ON A USERFORM.

i CAN GO TO THE SHEET AND ENTER THE SAME NUMBER IN BOTHE CELLS AND IT WORKS.

iS IT SOMETHING ABOUT THE TEXTBOX THAT COULD BE MESSING THINGS UP?

tHANKS
 
try enclosing all your references in the trim statement



trim(Sheets("VISTA DIAMANTE").Range("G30")) and so on


for each one
 
tHANKS A BUNCH ETID, THAT APPEARS TO HAVE CORRECTED THE PROBLEM

THANKS
 
Max1565 - tiny tip. Generally, any posting in CAPS is used to emphasise a point or to shout. It is general practice to not use CAPS for your whole post. Try to use lower case and only use CAPS for emphasis.

Rgds
Geoff
Si hoc legere scis, nimis eruditionis habes
 
sorry geoff, didn't mean to hurt your ears
 
It looks as though the caps lock got him...happens to me every now again.

...note the small t at the start of each sentance.
 
Indeed - was just a friendly note - no offense meant or taken :)

Rgds
Geoff
Si hoc legere scis, nimis eruditionis habes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top