How do augment this formula
=IF(ISERROR(FIND(" ",TRIM(A1),1)),TRIM(A1),MID(TRIM(A1),FIND(" ",TRIM(A1),1),LEN(A1)))
to separate the data below into columns by symptom cause resolution
L=Cannot print in UNIX
Symptom: Cannot print or plot in UNIX Cause: Configuration issue
Resolution: Escalate to the appropriate team Escalation Path: OEMM GIT Desktop References: Keywords: ZEH Created:
I like to get messy and complicated, but this can be done very easy Old fashion style.
hit Ctrl H
replace your multiple line break chars (i.e. "1234") with a single wild char "~" or "}" are usually good
use Excel's feature "text to columns" to break the line based on your wild char separator. (ctrl +A, +E)
If you only have a single space " " to delimit your columns simply use text to column, check Delimited, and hit " " under Other separator, then Finish.
And of course, before doing this, you should copy the column (paste/special values on column C, and then breake it to keep the initial values on column B) :)
Hope this help.
EDIT
Here is a piece of code that I wrote up (in a bit of a hurry). This follows the example from above with user input for column select and the string of chars used to break the text. If you only need to use space as "text breaker" then enter " " in the second promt.
Usually I take time to "clean" the code but this is what 10 minutes produced:
Sub SplitColumns()
DestinationColumn = InputBox("Please enter the name of the column you on which you want to perform the split", _
"Column Selection", "A", 100, 100)
Dim ReplaceRange As Range: Set ReplaceRange = ActiveSheet.Range(DestinationColumn & ":" & DestinationColumn)
SeparatorString = InputBox("Please enter the string of charatesrs used to define the split", _
"String Definition", "anything goes", 100, 100)
' Please be carefull and check for anythinkg like "~" as it will produce errors if found within the cell text
Dim Response As Integer
Response = MsgBox(prompt:= _
"Are you sure you want to split the text from column [" & DestinationColumn & "] based on [" & SeparatorString & "] ?" & vbNewLine & _
"Once the macro is run, there is no Undo Option available(Ctrl+Z)", _
Buttons:=vbYesNo)
If Response = vbYes Then
ReplaceRange.Replace _
What:=SeparatorString, _
Replacement:="~", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
ReplaceRange.TextToColumns _
Destination:=Range(DestinationColumn & "1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
OtherChar:="~", _
FieldInfo:=Array(Array(1, 1), Array(2, 1)), _
TrailingMinusNumbers:=True
End If
End Sub
Maybe I'll give this code another shot for a facelift (some other time).
Hope there's no debuging to be done. I've tested it on Excel 2007. Cheers!
Related
My VBA code is like this:
Sheets("Result").Range("E" & i) = Application.WorksheetFunction.AverageIfs( _
Sheets("Data").Range("B:B"), _
Sheets("Data").Range("B:B"), _
">=" & Sheets("Result").Range("A" & i), _
Sheets("Data").Range("B:B"), _
"<" & Sheets("Result").Range("B" & i), _
Sheets("Data").Range("B:B"))
However, an error message showed "Unable to get the averageif property of the worksheetfunction class".
When I change the critiria value to be a constant, however, it can run correctly:
Sheets("Result").Range("E" & i) = Application.WorksheetFunction.AverageIfs( _
Sheets("data").Range("b:b"), _
Sheets("data").Range("b:b"), _
">=" & 0.06, _
Sheets("data").Range("b:b"), _
"<" & 0.07)
What's the problem here? And how can I correct it?
-I found the problem was that there is no value within the specified range. But when I tried to debug using the iferror() function, it still shows the same error message.
The error comes up when
there is no data in any one of the referenced ranges
none of the data in column B matches the criteria
In a particular 1-column-wide range in a spreadsheet I need to use Excel 2007 VBA's range.find method to locate a text-valued cell containing the 2-character long value: 8" (pronounced in the US as Eight Inches). The .find method is in a sub that works fine for all other searches it's doing, but it cannot seem to find 8", or in fact any text value with a trailing double-quotation mark.
In the code below, initially sComparisonText contains 8"
I've tried adding from 1 to 6 double-quotation marks to the end of sComparisonText, using Chr(34), but the .find method still returns Nothing.
Various searches have noted the Chr(34) approach, and also stacking double-quotation marks: """" resolves to ", """""" resolves to "", etc. I've also looked into the .find method having a specialty escape character, but no success there either.
If Right(sComparisonText, 1) = """" Then
sComparisonText = sComparisonText & Chr(34) & Chr(34) & Chr(34) & Chr(34) & Chr(34) & Chr(34)
End If
Set rResult = rCT.Columns(InputColumn).Find(What:=sComparisonText, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If (rResult Is Nothing) Then 'Add a new row to the bottom of the rCT range
Can someone tell me what I'm doing wrong?
Thank you very much!
Dave
It isn't clear why you are trying to escape something that isn't a string literal. The reason you need to escape double-quotes in a string literal is so the compiler can parse it. The .Find function only expects a single " if you are only looking for a single ". If you already have a string stored in a variable that contains " in the string, use that. If you need to add one to a string, you can either use Chr$(34) or the escaped string literal """". They give you exactly the same resulting string:
Dim sComparisonText As String
Dim rResult As Range
sComparisonText = 8 & Chr$(34)
Set rResult = ActiveSheet.Cells.Find(What:=sComparisonText, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Debug.Print rResult.Address
...is the same as...
sComparisonText = "8"""
Set rResult = ActiveSheet.Cells.Find(What:=sComparisonText, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Debug.Print rResult.Address
...is the same as...
sComparisonText = 8 & """"
...etc.
The escape sequence has no meaning outside the compiler.
The first thing to consider is using xlPart rather than xlWhole
The second thing is to verify you really have double-quotes rather than a pair of single quotes. Click on the miscreant cell and run:
Sub WhatIsInThere()
Dim st As String, msg As String
Dim i As Long, CH As String
st = ActiveCell.Text
msg = Len(st)
For i = 1 To Len(st)
CH = Mid(st, i, 1)
msg = msg & vbCrLf & CH & vbTab & Asc(CH)
Next i
MsgBox msg
End Sub
To see an example of finding something with a trail double-quote, start with an empty worksheet and run:
Sub EightInchNails()
Dim DQ As String, WhereIsIt As Range
DQ = Chr(34)
Range("A15").Value = "8" & DQ
Set WhereIsIt = Range("A:A").Find(what:="8" & DQ, after:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart)
If WhereIsIt Is Nothing Then
Else
MsgBox WhereIsIt.Address(0, 0)
End If
End Sub
range.find "8"""
Should do the trick. The first two quotes at the end escape the actual " character, and the third quote terminates the string.
Recently I created Macros which searched through column B using a text match and populated column H with "Y" or "N" depending on whether there was a match or not. The code used was as follows.
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("H2:H" & lRow).FormulaR1C1 = "=IF(C[-6] = ""Commodities Ags/Softs"", " & _
"(IF(RC[-3]=R1C24,""Y"",(IF(RC[-3]=R2C24,""Y""," & _
"(IF(RC[-3]=R3C24,""Y"",(IF(RC[-3]=R4C24,""Y""," & _
"(IF(RC[-3]=R5C24,""Y"",(IF(RC[-3]=R6C24,""Y""," & _
"(IF(RC[-3]=R7C24,""Y"",(IF(RC[-3]=R8C24,""Y""," & _
"(IF(RC[-3]=R9C24,""Y"",""N"")))))))))))))))))),"""")"
Range("H2:H" & lRow).Select
Selection.Copy
ActiveSheet.Range("H2:H" & lRow).PasteSpecial xlPasteValues
I had to write 7 different macros because of the 7 possible matching texts in column B and the data I was matching it to comes from 7 different sources. I.E. If I received data from Commodities Ags/Softs, I would run the Commodities Ags/Softs macro (the other macros are identical, just swapping the text).
Now I've been told the data will be expanding to 70 different sources with 70 potential matching texts, rendering my specific macro to specific data approach pretty useless.
I was wondering if anyone knows how I could generalise the macro and in doing so, create a textbox which would tell the macro what text to match in column B.
Basically, I was hoping that if I received data from a particular source, I could run the macro, a textbox would appear in excel and whatever I typed into it would be the text I'm trying to match in column B, effectively altering the generalised macro.
Any help would be greatly appreciated, I'm new to VBA,
Cheers!
Basically:
Dim sMatch as String
sMatch = InputBox("Enter match data")
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
With .Range("H2:H" & lRow)
.FormulaR1C1 = "=IF(C[-6] = """ & sMatch & """, " & _
"(IF(RC[-3]=R1C24,""Y"",(IF(RC[-3]=R2C24,""Y""," & _
"(IF(RC[-3]=R3C24,""Y"",(IF(RC[-3]=R4C24,""Y""," & _
"(IF(RC[-3]=R5C24,""Y"",(IF(RC[-3]=R6C24,""Y""," & _
"(IF(RC[-3]=R7C24,""Y"",(IF(RC[-3]=R8C24,""Y""," & _
"(IF(RC[-3]=R9C24,""Y"",""N"")))))))))))))))))),"""")"
.Value = .Value
End With
I am trying to replace some text including line break, however it doesn't work. Was using Chr(10), Chr(13) and combination Chr(10)+Chr(13). None works. Any ideas? Code below:
Sub Replacetext()
Dim MyFolder As String
Dim MyFile As String
Dim Fname As String
MyFolder = "D:\Excel"
MyFile = Dir(MyFolder & "\*.txt")
Do While MyFile <> ""
Fname = MyFolder & "\" & MyFile
Workbooks.OpenText Fname
Cells.Replace What:="a" & Chr(13) & "a", Replacement:="b" & Chr(13) & "b" _
SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, lookat:=xlWhole
ActiveWorkbook.Close savechanges:=1
MyFile = Dir
Loop
End Sub
Short Answer
The ANSI code you are looking for is just 10
Longer Answer
Ok, So maybe this will help you. I created multiline data in a cell like this:
This is
Multiline
In "A1". Then I wanted to see what the code(s) of the whitespace were.
Public Sub ToAnsi()
Dim strCellData As String
strCellData = Range("A1").Value
For i = 1 To Len(strCellData)
MsgBox Asc(Mid$(strCellData, i, 1))
Next
End Sub
Not elegant by an means but we get the answer from the MsgBox. I was able to ascertain that the newline whitespace code was 10. Which was a surprise to me. My test was on Excel 2010.
Update
I have noticed you are missing a trailing comma in the first line of your Cells.replace. So it should be:
Cells.Replace What:="a" & Chr(13) & "a", Replacement:="b" & Chr(13) & "b", _
Also consider the following data. Row one contains multiline data.
A B C
1 This is a
Multiline a
2
If I run your code against my sheet with my ANSI numbers and even edit cell C1 with similar data.
Range("C1").Value = "c" & Chr(10) & "c"
Cells.Replace What:="a" & Chr(10) & "a", Replacement:="b" & Chr(10) & "b", _
SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, lookat:=xlWhole
A B C
1 This is b c
Multiline b c
2
If it is not working for you I think you need some more debugging or more information. Also here is a picture in case the formatting of "cells" isnt the best.
I'm attempting to have the following code insert the formula into the changed cell. I'm not receiving any errors but the code is not populating on the worksheet. Any insight into what I'm not doing?
formula_p1 = _
"=GETPIVOTDATA(""Max of " & [value_column_header] & ",Database!R1C14, & _
""Key"",& Key &)"
Debug.Print Cell.Address
Sheets("Cost Sheet").Cell.Formula = formula_p1
Thank you in advance.
Update
My current code is below I'm stuck on the concatenation in the final part. I'm not sure how to accomplish this in VBA. Also I need this to be a formula that will re evaluate on changes to the "Family" part that can be done on the worksheet. The change in "Family" is accomplished from a combo box selection.
formula_1 = _
"=GETPIVOTDATA(""Max of " & value_column_header & """,Database!$N$1,""Key""," & Concatenate(CurrentHFMFamily, G5, Left(B12, 4)) & ")"
This is the final result of what i was trying accomplish. It's not pretty but it's quick and stable.
=IF(INDIRECT(CONCATENATE("B",ROW()))=""," ",IFERROR(GETPIVOTDATA("Max of & _
"&INDIRECT((ADDRESS(11,COLUMN())))&"_ADJ",Database!R1C14,"Key",& _
LEFT(INDIRECT(CONCATENATE("B",SUM(ROW()-1))),4)&CurrentHFMFamily& & _
(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),-(SUM(ROW()-(5))),- & _
(MOD(COLUMN()+1,4))))),0))
Thanks to everyone for your ideas they helped in pointing me in the right direction.