VBA learning: Compile error: Expected Array

snjpverma

Well-known Member
Joined
Oct 2, 2008
Messages
1,584
Office Version
  1. 365
Platform
  1. Windows
I have written below code which should write "Starting with G" in column C, if column B contains a word starting with G.

however, this highlighted word in below line generates an error saying "Compile error: Expected Array".
Rich (BB code):
 If Not islike(cell.text, "g*") Then


Below is the completed code I managed to write.

VBA Code:
Sub trial()
Dim islike As Boolean
Dim cell As Range

    For Each cell In Range("B1:B7")
      
        If Not islike(cell.text, "g*") Then
            cell.Offset(0, 1).text = "Starting with G"
        Else
            cell.Offset(0, 1).text = "Not Starting with G"
        End If
    Next cell
End Sub

VBA Code:
Public Function islike(text As String, pattern As String) As Boolean

islike = UCase(text) Like UCase(pattern)

End Function

I am unable to understand why it is asking for an array when my function already has an argument "text as String".
I am going crazy tying to figure out the reason.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You need to remove this
VBA Code:
Dim islike As Boolean
You are confusing the compiler as it doesn't know if you want the variable or the function
 
Upvote 0
You are confusing the compiler as it doesn't know if you want the variable or the function

Thank you Fluff for being so quick. I really appreciate. It worked.

Actually, I initially wrote it like this, so I thought ISLIKE should be declared as Boolean. Unfortunately, I was wrong because the function shouldn't be declared in procedure. This is what I understood from this mistake.
If islike(cell.text, "g*") = True Then


Secondly, the below line gave an error saying Object required.
cell.Offset(0, 1).text = "Starting with G"

And then I changed ".text" to ".Value" and it worked fine.
Would you please explain why this happened?
 
Upvote 0
The Text property of a cell is read only, so you cannot write to it.
 
Upvote 0
You're welcome & thanks for the feedback.

The best ting (IMO) is to learn just the stuff you need now. Get used to working with that & then expand as needed.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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