Problem with VBA formula

DizzyBear

New Member
Joined
Jul 17, 2018
Messages
7
Hi! I am encountering a Compile error: Syntax error[FONT=&quot] [/FONT]with the VBA coding.

I would like to refer to column "Q" and produce the result of "CMS/DMS/SMS" in column "AX".

Do anyone know how can I fill up the formula to the last row?

The formula is

=IF(SUMPRODUCT(--ISNUMBER(SEARCH({"CDS","CNM","CMF","CSV"},Q2)))>0,"CMS",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"SPM","SSV","SNM","SMF"},Q2)))>0,"SMS",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"DPM","DSV","DNM","DMF"},Q2)))>0,"DMS",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"KCD"},Q2)))>0,"CMS","SMS"))))





Below is the hardcoding I managed to come up with.



Sub Classification()
'
' Classification Macro
' SMS, DMS, CMS
'
' Keyboard Shortcut: Ctrl+Shift+M
'


Dim sht As Worksheet
Dim lastrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "Q").End(xlUp).Row


Columns("AW:AW").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("AX1").Select
ActiveCell.FormulaR1C1 = "Macro"

Range("AX2:AX" & lastrow).Formula = "=IF(SUMPRODUCT(--ISNUMBER(SEARCH({"CDS","CNM","CMF","CSV"},Q2)))>0,"CMS",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"SPM","SSV","SNM","SMF"},Q2)))>0,"SMS",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"DPM","DSV","DNM","DMF"},Q2)))>0,"DMS",IF(SUMPRODUCT(--ISNUMBER(SEARCH({"KCD"},Q2)))>0,"CMS","SMS"))))"


End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try

Code:
Sub Classification()
' Classification Macro
' SMS, DMS, CMS
'
' Keyboard Shortcut: Ctrl+Shift+M
Dim sht As Worksheet
Dim lastrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "Q").End(xlUp).Row
Columns("AW:AW").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("AX1").Select
ActiveCell.FormulaR1C1 = "Macro"
Range("AX2:AX" & lastrow).Formula = "=IF(SUMPRODUCT(--ISNUMBER(SEARCH({""CDS"",""CNM"",""CMF"",""CSV""},Q2)))>0,""CMS"",IF(SUMPRODUCT(--ISNUMBER(SEARCH({""SPM"",""SSV"",""SNM"",""SMF""},Q2)))>0,""SMS"",IF(SUMPRODUCT(--ISNUMBER(SEARCH({""DPM"",""DSV"",""DNM"",""DMF""},Q2)))>0,""DMS"",IF(SUMPRODUCT(--ISNUMBER(SEARCH({""KCD""},Q2)))>0,""CMS"",""SMS""))))"
End Sub
 
Upvote 0
WOW! You are great!

I have been trying to solve this for quite some time!

Thank you for solving it!


I am curious why ""CDS"" is needed?

It is quite interesting that when converting a formula to VBA and additional " is needed.

Once again! Thank you! I really appreciate your help on this matter!
 
Upvote 0
If CDS is not needed remove it, but it was in your original formula....I simply copied the formula !!

Code:
Range("AX2:AX" & lastrow).Formula = "=IF(SUMPRODUCT(--ISNUMBER(SEARCH({""CNM"",""CMF"",""CSV""},Q2)))>0,""CMS"",IF(SUMPRODUCT(--ISNUMBER(SEARCH({""SPM"",""SSV"",""SNM"",""SMF""},Q2)))>0,""SMS"",IF(SUMPRODUCT(--ISNUMBER(SEARCH({""DPM"",""DSV"",""DNM"",""DMF""},Q2)))>0,""DMS"",IF(SUMPRODUCT(--ISNUMBER(SEARCH({""KCD""},Q2)))>0,""CMS"",""SMS""))))"
End Sub
 
Upvote 0
Sorry, I meant why did a single amperstand (In Excel Formula) changed to double amperstand when used in the Visual Basic for Application
 
Upvote 0
Try it !!
But I can see no reason for it not to work !!
 
Upvote 0
Are you still using this in the code?
Code:
Set sht = ThisWorkbook.Worksheets("Sheet1")
If you are then ThisWorkbook will refer to the workbook the code is in, i.e. your personal workbook, so when you are trying to find the last row later in the code you will get an incorrect result.

Try changing ThisWorkbook to ActiveWorkbook in the above code.
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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