Textbook code fails to work

mikecox39

Active Member
Joined
Mar 5, 2014
Messages
251
Office Version
  1. 365
Platform
  1. Windows
I copied this code from Excel VBA: by Nein Smith and I can't get it to work.

The data is a mixed list of number from 1-10 in Column A, color line works but the text line does not

I know there should be a Then in this macro to the author was just trying to show how Conditional logic works, at a very basic level. I guess he thought add the Then part would just cause confusion.

I'm sure there is a simple answer but I don't see it. I understand this is an Offset operation and that it's working in the second line, but why isn't it working in the first line? I expect that it is and that the issue has something to do with the text variable.


Code:
Sub DataSet1() Dim FirstNumber As Integer
 
 
FirstNumber = ActiveCell.Value


 If FirstNumber >= 5 Then
       ActiveCell(1, 2).Value = ”Completed”
       ActiveCell(1, 1).Interior.Color = RGB(225, 225, 0)


End If
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The only thing that I can see is wrong is there are some non standard quotes around Completed.
What happens when you copy/paste the code below and run it?
Code:
Sub DataSet1()
    Dim FirstNumber As Integer


    FirstNumber = ActiveCell.Value


    If FirstNumber >= 5 Then
        ActiveCell(1, 2).Value = "Completed"
        ActiveCell(1, 1).Interior.Color = RGB(225, 225, 0)
    End If
End Sub
 
Last edited:
Upvote 0
The only thing that I can see is wrong is there are some non standard quotes around Completed.
What happens when you copy/paste the code below and run it?
Code:
Sub DataSet1()
    Dim FirstNumber As Integer


    FirstNumber = ActiveCell.Value


    If FirstNumber >= 5 Then
        ActiveCell(1, 2).Value = "Completed"
        ActiveCell(1, 1).Interior.Color = RGB(225, 225, 0)
    End If
End Sub

Amazing :huh: That's what it was! After running your code and finding that it worked, I went back to the code I sent, that I cut and pasted, and entered new quotes and it worked also. I would never have picked up on that tiny difference!

Thanks!
 
Upvote 0
Hi Mike, you're welcome. It is actually quite a common problem that is encountered when copying code from the internet (just for reference the regular quotes are Ascii character 34 and the other quotes are Ascii character 148 [at least where I am]).
 
Upvote 0
Hi Mike, you're welcome. It is actually quite a common problem that is encountered when copying code from the internet (just for reference the regular quotes are Ascii character 34 and the other quotes are Ascii character 148 [at least where I am]).
Interesting! Thanks, I'll keep that in mind next time I copy code into XL from the net.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top