Complex conditional text formatting in Excel - conditional-formatting

I need to conditionally bold text inside of an Excel spreadsheet. The logic is as follows:
For each record, I need to bold the last name first initial in column E that corresponds with the name of the person in column B (which is formatted Last Name, First Name). All the last name first initial are separated by commas in column E. Note that there might be two of the same last name with a different first initial in column E. I need to iterate through a spreadsheet with 1,000+ records. Example below:
A
B
C
D
E
value
Smith, Joseph
value
value
Jones K, Jenkins T, Smith J, Hines L, Abdhel B, Higgins M
value
Roberts, Anna
value
value
Taylor B, Starbert K, Helmann E, Santoro P, Stebnitz M, Hamilton A, Brown P, Palmer A, Roberts A, Stanton J
value
Chen, Jennifer
value
value
Anderson B, Chen J, Flanders C, Chen P, Aberdeen T, Daniels P
For the first record, "Smith J" in column E must be made bold based on the "Smith, Joseph" in column B.
For the second record, "Roberts A" in column E must be made bold based on the "Roberts, Anna" in column B.
For the thrid record, "Chen J" must be made bold base on the "Chen, Jennifer" in column B -- PLEASE NOTE that there is another "Chen" ("Chen P"), in column E that must not be made bold.
I greatly appreciate any help.

Bianca,
Welcome to StackOverflow.
I can not figure out a way to do this with formulas in the UI Conditional Formatting.
However, the following macro will accomplish the task.
Option Explicit
Sub BoldPartString()
Dim lRowCntr As Long
Dim iSStrLen As Integer
Dim iStart As Integer
Dim zSearchStr As String
lRowCntr = 1
Do
'Determine the part of the Col B value to use to search Col E
zSearchStr = Left(Cells(lRowCntr, "B"), InStr(Cells(lRowCntr, "B"), ",") - 1) & _
Mid(Cells(lRowCntr, "B"), InStr(Cells(lRowCntr, "B"), ",") + 1, 2)
'Find the number of characters you'll have to format once the string is found in Col E
iSStrLen = Len(zSearchStr)
'Search Col E for the value in zSearchStr
If (InStr(Cells(lRowCntr, "E"), zSearchStr) > 0) Then
'Find the starting character location in Col E
iStart = InStr(Cells(lRowCntr, "E"), zSearchStr)
'Apply the Bold FontStyle to the substring using previously calculated Start/Stop values
Cells(lRowCntr, "E").Characters(iStart, iSStrLen).Font.FontStyle = "Bold"
End If
'Move to next row
lRowCntr = lRowCntr + 1 'Next Row
Loop Until Cells(lRowCntr, "A") = ""
End Sub

Related

Using a dictionary to store & read values to change cell color and call macro

I have a form on a sheet (Sheet2), I would like for the values entered in the D column cells to be compared to a list of values on a different sheet (Sheet5, column C). If the value entered in the D column of Sheet2 equals any of the values in Sheet5 column C, I want the F column cell in the same row of Sheet2 to turn red. If D7 = a value in column C of Sheet5, I'd like F7 to turn red. Ultimately I want this to check if any of the values in the D column are associated with macros I have created for certain values but I am trying to avoid hard coding anything.
I have started doing this by setting up a dictionary in my VBA code but I've never used dictionaries before and the code I have so far isn't working as I'd like. With this code, the cells in the F column of Sheet2 do not turn red.
Option Explicit
Sub estArrays()
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
' Read to dictionary
Dim i As Long
For i = 1 To 17
dict.Add Sheet5.Range("C" & i).Value, 1
Next i
' Read column d
For i = 7 To 446
If dict.Exists(Sheet2.Range("D" & i).Value) Then
' Set to Red
Sheet2.Range("F" & i).Interior.Color = 3
End If
Next i
End Sub
FYI: values are derived from a "MID" formula in column D on Sheet2 so, the values entered in column D are not manually entered. Thanks in advance for any answers/feedback/suggestions.

Invalid Outside Procedures and Syntax in VBA/Excel

I'm new to programming/coding, and I'm having problems with my code.
I'm currently trying to write a macro in excel that will go through an excel document and insert a partially blank row on duplicate columns.
I'm currently using Excel 2010
So for example if the excel sheet contained:
Column A, Column B, Column C, Column D, Column E
1 Apples 1 40 Blue
1 Bananas 2 50 Red
1 Oranges 3 60 Pink
2 Cherries
3 Kiwis
Then the script would sort it into:
Column A, Column B, Column C, Column D, Column E
1 Apples 1 40 Blue
1 Bananas
1 Oranges
2 Cherries 2 50 Red
3 Kiwis 3 60 Pink
Thus sorting the data by Column A and C while also creating blank spaces in C, D, and E if the value in Column A does not equal the value found in Column C.
My code so far:
Sub Main()
Dim a As Long, c As Long
Dim objRange As Range
Dim strCIDCol1 As String, strCIDCol3 As String
a = 1 'row counter for Column A
c = 1 'row counter for Column C
Do Until ActiveSheet.Cells(a, 1) = ""
'sets the loop to run until A1 is blank
strCIDCol1 = ActiveSheet.Cells(a, 1)
'sets the value of A1 as CIDCol1
strCIDCol3 = ActiveSheet.Cells(c, 3)
'sets the value of C1 as CIDCol3
If (strCIDCol1 <> strCIDCol2) Then
'runs until A(a) and C(c) are not equal
Set objRange = ActiveSheet.Cells(c, 3).Range(Cells(c, 3), Cells(c, 5))
objRange.Activate
'Selects Columns C, D, and E
objRange.Insert (xlShiftDown)
'Inserts "Shift Row Down"
strCIDCol1 = ActiveSheet.Cells(a + 1, 1)
'moves on
End If
a = a + 1 'adds 1 to counter to move to next row
c = c + 1 'adds 1 to counter to move to next row
Loop
End Sub
I keep getting "invalid outside procedure" errors on
a = 1
c = 1
and
a = a + 1
c = c + 1
I'm also getting a 1004 error "insert method of range class failed" on
objRange.Insert (xlShiftDown)
I have no idea what I'm doing with objRange. I saw the code online and tried to adapt it to fit what I needed. The way it was explained was that objRange was used to highlight the selected areas Column C, D, and E in this case. And then Insert would shift the cells down by inserting a row above the highlighted cells.
If (strCIDCol1 <> strCIDCol2) Then
You have never defined strCIDCoL2. I think you meant str CIDCol3 here.
Set objRange = ActiveSheet.Cells(c, 3).Range(Cells(c, 3), Cells(c, 5))
This is wrong syntax for setting range. Try using:
Set objRange = Range(ActiveSheet.Cells(c,3).Range(Cells(c,3), Cells(c,5)))
Also are you sure about using objRange.activate instead of objRange.select ?

VBA to hyperlink addresses from one column to anchors in the next column

I have two columns (E,F) where E has 2500 URLs for Articles and F has the Titles of those articles. As part of a larger macro I need to hyperlink the titles in column F to the correlated URLs in column E. If I wasn't doing this via VBA I'd use the hyperlink function.
The current attempt I made is below. It's not executing the command past the first hyperlink. Any suggestions?
i = 1
Do While i < 2500
Cells(6, i).Hyperlinks.Add anchor:=Cells(6, i), Address:=Cells(5, i)
i = i + 1
Loop
You are confusing rows and columns:
i = 1
Do While i < 2500
Cells(i, 6).Hyperlinks.Add anchor:=Cells(i, 6), Address:=Cells(i, 5)
i = i + 1
Loop
Note: If you find it more readable, you can use column letters instead of column numbers as a parameter in the Cells property, e.g.
Cells(i, "F").Hyperlinks.Add anchor:=Cells(i, "F"), Address:=Cells(i, "E")
I find it useful to use numbers when looping across columns, and use letters when I am referring to a specific, known, column.
Even with rows and columns interpolated your code should run. I suspect that VBA objects to a blank cell (not its value, but the range) being assigned to the Address property. Heeding #YowE3K's good advice about addressing columns, I arrive at the code below.
Dim Hype As String
Dim R As Long ' row number
' Column E (5) = URLs
' Column F (6) = Product titles
With Worksheets("Sheet1") ' specify your sheet
For R = 1 To 2500
Hype = .Cells(R, "E").value
If Len(Hype) = 0 Then Exit For
.Hyperlinks.Add Anchor:=.Cells(R, "F"), _
Address:=Hype, _
TextToDisplay:=.Cells(R, "F").value
Next R
End With
This code tries to deal with an apparent flaw in your logic. A hyperlink will display a name and act on a URL. Your sheet has the name in one column and the URL in another. So, where is the Hyperlink? My above code replaces the name in column F with the hyperlink. The cell will still display the same name, but the URL column will become obsolete.

Excel VBA - Column count using variants

I have searched the forums but I am really struggling to get part of my code to work. Basically the idea is to search sheet 1 and copy one or more columns depending on the criteria to a specific worksheet.
i.e. if sheet 1 columns 1 and 3 contain "copy 01" then copy both columns to a sheet 2 and if sheet 1 columns 2 and 4 contain "copy 02" then copy both columns to a sheet 3 etc.
I can count rows fine using the code, but can't count columns. Seems to relate to not fiding the column range but I have no ideas to fix this! Any help would be much appreciated.
'Row
Dim NR As Long
Dim d As Variant
d = ws1.Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
For NR = 1 To UBound(d, 1)
'column
Dim NC As Long
Dim e As Variant
e = ws1.Range(Cells(1, Columns.Count).End(xlToLeft).Column).Value
For NC = 1 To UBound(e, 1)
Thanks,
Stewart
You want this:
e = range("A1:" & split(cells(1,cells(1,columns.Count).end(xlToLeft).column).address(true,false), "$")(0) & "1").Address
The cells(1, columns.count).end(xlToLeft).column) gets the last column number (for example 13 for 'M').
Putting this into cells(1, lastcolNum) gets a cell that represents the cell in the first row of this column (for example Cell M1).
The address(true, false) method gets the cell reference with a dollar sign before the row but not before the column letter (for example "M$1"
The split function returns an array which splits the input string by the "$" character (for example array - ("M","1")
The (0) returns the 0th element in the returned array (for example "M")
Then putting this into the range function returns the range (for example) "A1:M1"
I'm not entirely sure what you're trying to do with the UBound function here. It would make more sense to make
e = cells(1,columns.count).end(xlToLeft).column
and then loop through
For N = 1 To e
As this will loop through each column.

Macro to find if all values in an array is present in a selected cell

I have a sheet named Assignee which contains the names of certain persons. I have another sheet named Raw which contains multiple rows containing text strings. My requirement is to find out if any of the names in sheet Assignee is present in a cell, and if so, which is the last name in that cell. To specify more, see following example,
Assignee sheet contains names Vivek S. Panicker in cell A1, John Smith in A2 and William Dezuza Margeret in A3. Raw sheet A1 cell contains a text string like, "John Smith met me last night to inquire about William Dezuza Margeret". The last name in this string is "William Dezuza Margeret", which I need to extract using a VBA code. Since this need to be done in multiple lines, a macro with loop is highly appreciated.
Function LastUsedName(rng As Range) As Variant
Dim names As Variant
names = Sheets("Assignee").Range("A1:A" & Sheets(2).Range("A" & Rows.Count).End(xlUp).Row)
Dim v As Variant
For Each v In names
If InStr(1, rng, v, vbTextCompare) Then
Dim pos As Long
pos = InStrRev(rng, v, -1, vbTextCompare)
LastUsedName = Mid(rng, pos, Len(v))
Exit Function
End If
Next
End Function