Solved

How to modify my code to not be case sensitive?

Posted on 2014-12-05
7
80 Views
Last Modified: 2014-12-05
The below code searches for the string “BUILDING:", but it will only find it if it’s all in caps. How do I make it not be case sensitive?

strTmp = ""
strTmp = Filter(aLines, "BUILDING:")(0)
Range("X1") = Trim(Split(strTmp, ":")(1)

Open in new window

0
Comment
Question by:kbay808
  • 5
  • 2
7 Comments
 
LVL 26

Expert Comment

by:Nick67
ID: 40484075
Up at the top of the code module:
It's always good practice to have Option Explicit
Do you have an Option Compare set, too?

This will override it

strTmp = ""
strTmp = Filter(aLines, "BUILDING:",true,vbTextCompare)(0)
Range("X1") = Trim(Split(strTmp, ":")(1)
0
 

Author Comment

by:kbay808
ID: 40484081
I’m a novice so you lost me.  Here is my code.
Sub Description_Data()
On Error Resume Next
Range("W1:AK1").ClearContents
Range("W3:AK3").ClearContents
Range("W5:AK5").ClearContents
Range("W7:AK7").ClearContents
Range("W9:AK9").ClearContents
Range("W11:AK11").ClearContents
Range("W13:AK13").ClearContents

aLines = Split(Range("B11"), vbLf)

'Bldg

strTmp = ""
strTmp = Filter(aLines, "BLDG:")(0)
Range("W1") = Trim(Split(strTmp, ":")(1))

strTmp = ""
strTmp = Filter(aLines, "BUILDING:")(0)
Range("X1") = Trim(Split(strTmp, ":")(1))

strTmp = ""
strTmp = Filter(aLines, "BLDG")(0)
Range("Y1") = Trim(Split(strTmp, "G")(1))

strTmp = ""
strTmp = Filter(aLines, "BUILDING")(0)
Range("Z1") = Trim(Split(strTmp, "G")(1))

strTmp = ""
strTmp = Filter(aLines, "BLD")(0)
Range("AA1") = Trim(Split(strTmp, "D")(1))


End Sub

Open in new window

0
 
LVL 26

Expert Comment

by:Nick67
ID: 40484087
At the very top of the code put
Option Explicit

This requires you to define all your variables, and keeps the source of many subtle errors at bay
On Error Resume Next
You should use this ONLY as a very last ditch resort and certainly not while you actively developing.
This tells the code to keep going along merrily until it can't, ignoring all problems.
This can be VERY unhappy when you want a series of dependent things to occur.

You'll likely need
DIm aLines() as string
dim strTmp  as string
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40484091
Try this

Option Explicit
Option Compare Text
Sub Description_Data()
Dim aLines() As String
Dim strTmp  As String
'On Error Resume Next
Range("W1:AK1").ClearContents
Range("W3:AK3").ClearContents
Range("W5:AK5").ClearContents
Range("W7:AK7").ClearContents
Range("W9:AK9").ClearContents
Range("W11:AK11").ClearContents
Range("W13:AK13").ClearContents

aLines = Split(Range("B11"), vbLf)

'Bldg

strTmp = ""
strTmp = Filter(aLines, "BLDG:")(0)
Range("W1") = Trim(Split(strTmp, ":")(1))

strTmp = ""
strTmp = Filter(aLines, "BUILDING:")(0)
Range("X1") = Trim(Split(strTmp, ":")(1))

strTmp = ""
strTmp = Filter(aLines, "BLDG")(0)
Range("Y1") = Trim(Split(strTmp, "G")(1))

strTmp = ""
strTmp = Filter(aLines, "BUILDING")(0)
Range("Z1") = Trim(Split(strTmp, "G")(1))

strTmp = ""
strTmp = Filter(aLines, "BLD")(0)
Range("AA1") = Trim(Split(strTmp, "D")(1))


End Sub

Open in new window

0
 

Author Closing Comment

by:kbay808
ID: 40484106
That did the trick.  Thank you very much for your help.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40484115
This would be better, as it is commented
Option Explicit
Option Compare Text
Sub Description_Data()
Dim aLines() As String
Dim strTmp  As String
'On Error Resume Next

'clear cells as required
Range("W1:AK1").ClearContents
Range("W3:AK3").ClearContents
Range("W5:AK5").ClearContents
Range("W7:AK7").ClearContents
Range("W9:AK9").ClearContents
Range("W11:AK11").ClearContents
Range("W13:AK13").ClearContents

'split B11 based on the linefeed character into an array
'usually though its vbCrLF
aLines = Split(Range("B11"), vbLf)

'Bldg

strTmp = ""
'look through the spilt for "BLDG:"
'Put the first instance in strTemp
strTmp = Filter(aLines, "BLDG:", True, vbTextCompare)(0)
'split the result on the colon, hammer the seond bit into the cell
Range("W1") = Trim(Split(strTmp, ":")(1))

'rinse and repeat
strTmp = ""
strTmp = Filter(aLines, "BUILDING:")(0)
Range("X1") = Trim(Split(strTmp, ":")(1))

strTmp = ""
strTmp = Filter(aLines, "BLDG")(0)
Range("Y1") = Trim(Split(strTmp, "G")(1))

strTmp = ""
strTmp = Filter(aLines, "BUILDING")(0)
Range("Z1") = Trim(Split(strTmp, "G")(1))

strTmp = ""
strTmp = Filter(aLines, "BLD")(0)
Range("AA1") = Trim(Split(strTmp, "D")(1))


End Sub

Open in new window

0
 
LVL 26

Expert Comment

by:Nick67
ID: 40484116
And I can't test this, but if it is bug-free, this would be best and most maintainable
Option Explicit
Option Compare Text
Sub Description_Data()

'clear cells as required
Range("W1:AK1").ClearContents
Range("W3:AK3").ClearContents
Range("W5:AK5").ClearContents
Range("W7:AK7").ClearContents
Range("W9:AK9").ClearContents
Range("W11:AK11").ClearContents
Range("W13:AK13").ClearContents

Call Pound(Range("B11"), Range("W1"), "BLDG:", ":")
Call Pound(Range("B11"), Range("X1"), "BUILDING:", ":")
Call Pound(Range("B11"), Range("Y1"), "BLDG", "G")
Call Pound(Range("B11"), Range("Z1"), "BUILDING", "G")
Call Pound(Range("B11"), Range("AA1"), "BLD", "D")


End Sub

Sub Pound(RInput As Range, ROutput As Range, TheString As String, SplitChar As String)
Dim aLines() As String
Dim strTmp  As String
'split RInput based on the linefeed character into an array
'usually though its vbCrLF
aLines = Split(RInput, vbLf)
'look through the spilt for TheString
'Put the first instance in strTemp
strTmp = Filter(aLines, TheString, True, vbTextCompare)(0)
'split the result on the final character, hammer the second bit into the cell
ROutput = Trim(Split(strTmp, SplitChar)(1))
End Sub

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now