VBA if x like... returning false

taikvei

New Member
Joined
Nov 29, 2018
Messages
14
Hi there,

I have the below bit of code. In my head, it should loop through columns 1 to 52 of row 8 of the spreadsheet assigned to cTeam, and return True if the value is like "tel".

The values in those cells are Contact Tel. Contact Tel. 2 Home Tel. Home Tel. 2 etc., but the Like operator keeps returning false - am I doing it wrong somehow? I've tried having it as "*Tel*" as well as what it is now, but it hasn't helped. I've stepped through all 52 iterations with F8 and it hasn't picked up any of the columns with the Tels

Code:
With cTeam

    For logCol = 1 To 52 Step 1
    colLetter = Split(Cells(1, logCol).Address, "$")(1)
        If Cells(8, logCol).Value Like "*" & "Tel" & "*" Then
        cTeam.Range(colLetter & "8" & ":" & logCol).SpecialCells(xlCellTypeVisible).Copy
        cSheet.Cells(1, cSheetCol).PasteSpecial Paste:=xlPasteValues
        cSheetCol = cSheetCol + 1
        End If
    
    Next logCol
End With
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Sorry, I forgot to mention it - those cells are the ones that the filter arrows are on - could that potentially be a problem?
I tried googling but was only able to find guides on how to use the Like operator and examples, but not anything that would troubleshoot why it returns false.
 
Upvote 0
Does the word "Tel" appear anywhere in row 8?
 
Upvote 0
Row 8 is where the header columns for the data below are. There's roughly 6 headers that are a variation of Contact Tel. or Home Tel., but they all have "Tel" in them.
 
Upvote 0
You have unqualified Range references inside With cTeam, so without the dot qualifier these ranges refer to the active sheet, not cTeam. Fix like this:
Code:
   colLetter = Split(.Cells(1, logCol).Address, "$")(1)
        If .Cells(8, logCol).Value Like "*Tel*" Then
Also note that Like is case-sensitive, so "Contact tel." doesn't match "*Tel*". Use LCase on the left side of the Like and match against "*tel*" if it must be case-insensitive:
Code:
        If LCase(.Cells(8, logCol).Value) Like "*tel*") Then
 
Last edited:
Upvote 0
Here's a screenshot of what these specific
VPB6G9j
column headers in that row are
https://imgur.com/a/VPB6G9j
 
Upvote 0
You have unqualified Range references inside With cTeam, so without the dot qualifier these ranges refer to the active sheet, not cTeam. Fix like this:
Code:
   colLetter = Split(.Cells(1, logCol).Address, "$")(1)
        If .Cells(8, logCol).Value Like "*Tel*" Then
Also note that Like is case-sensitive, so "Contact tel." doesn't match "*Tel*". Use LCase on the left side of the Like and match against "*tel*" if it must be case-insensitive:
Code:
        If LCase(.Cells(8, logCol).Value) Like "*tel*") Then

I've tried doing that, and the code block now spits out an error. Here's what it looks like at the moment.

Code:
With cTeam

    For logCol = 1 To 52 Step 1
    colLetter = Split(.Cells(1, logCol).Address, "$")(1)
        If .Cells(8, logCol).Value Like "*" & "Tel" & "*" Then
        cTeam.Range(colLetter & "8" & ":" & logCol).SpecialCells(xlCellTypeVisible).Copy '<-- error is here
        cSheet.Cells(1, cSheetCol).PasteSpecial Paste:=xlPasteValues
        cSheetCol = cSheetCol + 1
        End If
    
    Next logCol
End With

It gives me an error saying Method 'Range' of object '_Worksheet' failed. I tried taking the cTeam out of that line, leaving just .Range(colLetter..................., but still the same error.
 
Upvote 0
This isnt valid:

Code:
colLetter & "8" & ":" & logCol

It resolves to A8:1

when logCol is 1. What is it meant to be?
 
Upvote 0
Oh wow. You are right. It should've been colLetter & logCol. It works now.
such a silly mistake that cost me the last 3-4 hrs of racking my brain..

Any idea why it never popped up with an error before? which is why I thought the issue was with the Like operator?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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