Convert Numeric List in Column A to Say Positive, Negative, or Neutral in Column B

scooby33

New Member
Joined
Nov 15, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am very new to VBA and Macros, and I am trying to get the values in column A which are made up of positive and negative numbers to be converted to "positive," "negative," or "neutral." in column B.

Column A Column B
-1 negative
0 neutral
3 -----> positive
-2 negative
4 positive

I currently do not have any code to output neutral, I am just trying to get to get this working before I add that in.

Here is my code below:
Sub SentimentRecode()
Workbooks("Sentiment Recode").Worksheets("Sheet1").Activate

Dim cell As Range
Dim n As Negative
Dim p As Positive

For Each cell In Range("A:A")
If cell.Value < 0 Then
Range("B:B").Value = n
Else
Range("B:B").Value = p
End If
Next cell
End Sub

I have tried about 10 different iterations of code to try and get the output I want, I am currently getting an error that says compile error: user-defined type not defined. Any help would be greatly appreciated!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Do you have a header row? If so which is the first row with numbers?
 
Upvote 0
Ok, how about
VBA Code:
Sub scooby()
   With Workbooks("Sentiment Recode").Worksheets("Sheet1")
      With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         .Offset(, 1).Value = .Worksheet.Evaluate(Replace("choose(sign(@)+2,""Negative"",""Neutral"",""Positive"")", "@", .Address))
      End With
   End With
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Sub scooby()
   With Workbooks("Sentiment Recode").Worksheets("Sheet1")
      With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         .Offset(, 1).Value = .Worksheet.Evaluate(Replace("choose(sign(@)+2,""Negative"",""Neutral"",""Positive"")", "@", .Address))
      End With
   End With
End Sub
That worked perfectly, thank you I appreciate it!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Hello,

I am running into an issue where if the cells are blank the output is still neutral which messes up the counts. I have your code copied verbatim from yesterday. Is there a fix for this?
 

Attachments

  • Macro.png
    Macro.png
    15.1 KB · Views: 4
Upvote 0
What should col B be if col A is blank?
 
Upvote 0
Yup, try
VBA Code:
Sub scooby()
   With Workbooks("Sentiment Recode").Worksheets("Sheet1")
      With .Range("A1", .Range("A" & Rows.Count).End(xlUp))
         .Offset(, 1).Value = .Worksheet.Evaluate(Replace("if(@="""","""",choose(sign(@)+2,""Negative"",""Neutral"",""Positive""))", "@", .Address))
      End With
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,409
Messages
6,124,730
Members
449,185
Latest member
ekrause77

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