Hello everyone,
Thanks for all the great help on these forum, I've been reading lots and learning from all the great responses. This one seems to elude me.
I have named a set range equal to cell B8. I want to change the range if cell b8 contains only one hyphen and keep that range if it has 2 and warn if it has more than 2 hyphens.
so B8 looks usually like " 10-123-01" or "10-123"
Here is what the pseudo code looks like:
Set xlWkb = XL.Workbooks.Open(varFile) 'opening the workbook to upload sheet = "Corr-Score" ' setting the sheet with the data needed Set xlWks = xlWkb.Worksheets(sheet) Set xlTestNoRng = xlWks.Range("B8") If xlWks.Range.value *has 2 "-" then leave range as "B8" If xlWks.Range.Value *has 1 "-" then change range to B6"
Hi
Broadly you could do something like
This will allow you to set it if it is 1, do nothing if it is 2 or give a warning if it is > 2. You may also want to cover the situation where you have no hyphens in the result.cntr = Len(Range("B8")) - Len(WorksheetFunction.Substitute(Range("B8"), "-", "")) Select Case cntr Case 1 'reset the named range Case 2 'do nothing Case Else MsgBox "You have " & cntr & " hyphens" End Select
HTH
rylo
Or
EDITED POST shg woke me up
Option Explicit Function CountInstances(FindWhat As String, rng As Range) As Integer Dim PositionOfChar As Integer Dim HowManyChars As Integer HowManyChars = 0 PositionOfChar = InStr(1, rng.Value, FindWhat) Do If PositionOfChar > 0 Then CountInstances = CountInstances + 1 PositionOfChar = InStr(PositionOfChar + 1, rng.Value, FindWhat) Else Exit Do End If Loop End Function
Or better still with shgs' suggestion
Function CountInstances(FindWhat As String, rng As Range) As Integer CountInstances = Len(rng.Value) - Len(Replace(rng.Value, FindWhat, "")) End Function
Called with
or in the worksheet as a UDFNumberOfHyphens = CountInstances("-", Range("B8"))
The code is now a bit more flexible it will count any instances of a string in a string.=CountInstances("-", A1)
=CountInstances("?", A1)
So your code could be on these lines.
Sub YourProcedure() Dim FileFullName As String, wsName As String Dim xlWkb As Workbook Dim xlWks As Worksheet FileFullName = "K:\Excel Forum\TempOneOffs\Book3.xls" Set xlWkb = Workbooks.Open(FileFullName) ' opening the workbook to upload wsName = "Corr-Score" ' setting the sheet with the data needed Set xlWks = xlWkb.Worksheets(wsName) With xlWks Select Case CountInstances("-", .Range("B8")) Case 2 .Range("B8") = .Range("B8") Case 1 .Range("B8") = .Range("B6") Case Else 'do something else End Select End With End Sub
Last edited by Marcol; 06-15-2010 at 04:27 AM. Reason: Engaged mouth before brain was in gear!
Another way:
Sub main() Dim r As Range Set r = x(Range("B8")) End Sub Function x(r As Range) As Range With r(1) Select Case Len(.Value) - Len(Replace(.Value, "-", "")) Case 1: Set x = .Parent.Range("B6") Case 2: Set x = .Cells Case Else: MsgBox "Oops" End Select End With End Function
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Thanks for wakening me up shg
I didn't read the question again!!!
Rather than further clutter the BB I have edited Post #3
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks