Chris T.
Chris T.

Reputation: 13

Two text strings are showing up as different in Excel, causing a macro to read false

I have two cells in a spreadsheet, A3 and A4. Each has "AHU-2" in it. I copied the A3 content to A4, to make it the same thing.

I have a macro that reads these two cells, and if they are the same, copies a different cell to a different sheet. This macro runs several times right now, to copy several different "Tag Value Category: Tag Value" pairs. Such as CFM: 50, and EAT: 78F.

Right now, the macro reads A3 and A4 as different values, does not select the EAT cell, keeps the last cell (50) copied, and puts that where the EAT value should be in the new sheet.

Anybody know how to fix this?

This is part of my current code.

Sheets("08-Attribute").Select
Range("I3").Select
Selection.Copy
Sheets("JCX").Select
Range("T2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("08-Attribute").Select
Range("K3").Select
Selection.Copy
Sheets("JCX").Select
Range("U2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Sheets("08-Attribute").Select
If ("A3" = "A4") Then Range("I4").Select
Selection.Copy
Sheets("JCX").Select
Range("V2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("08-Attribute").Select
If ("A3" = "A4") Then Range("K4").Select
Selection.Copy
Sheets("JCX").Select
Range("W2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

spreadsheet screenshot

Upvotes: 0

Views: 41

Answers (1)

SJR
SJR

Reputation: 23081

It is imperative that you read up on how to avoid Select. Your syntax is also off (If ("A3" = "A4")). Also you can transfer values directly which is rather more efficient than copying and pasting values.

With Sheets("08-Attribute")
    Sheets("JCX").Range("T2").Value = .Range("I3").Value
    Sheets("JCX").Range("U2").Value = .Range("K3").Value
    If .Range("A3").Value = .Range("A4").Value Then
        Sheets("JCX").Range("V2").Value = .Range("I4").Value
        Sheets("JCX").Range("W2").Value = .Range("K4").Value
    End If
End With

Upvotes: 4

Related Questions