Split Up Recipients names to get first names - vba

I am trying to break up the full name of two Recipients to get the first names.
Here is the code:
For r = 1 To .recipients.Count
Debug.Print .recipients(r)
strgreetname = Left(.recipients(r), InStr(1, .recipients(r), " ") - 1)
strTo = Left(strGreetNameAll, InStr(1, .recipients(r), " ") - 2)
strGreetNameAll = strGreetNameAll & strgreetname & ", "
strgreetnameall1 = strgreetname
Next r
For i = 1 To .recipients.Count
Debug.Print .recipients(i)
strgreetname = Left(.recipients(i), InStr(1, .recipients(i), " ") - 1)
strTo1 = Right(strTo, InStr(1, .recipients(i), " ") - 2)
strGreetNameAll = strGreetNameAll & strgreetname & ", "
strgreetnameall1 = strgreetname
Next i
I cannot get strTo and strTo1 to work separately based on the recipients count. It seems to only change if I change the first defined item, in this case r. Changing the i value does nothing. It seems that the first defined variable controls everything below it, even though I have it set separately. How do I break these up so that r controls one section, while i controls another, so they work autonomously?
Update--I got it to separate but the first alias (strTO) is cutting off in strange ways. I want to cut off at the first space in the alias, which I thought I did with the "left" piece. However, it is not consistent, cutting off long names, or pulling in parts of the next name if the first alias was a short name (like Tom).
For R = 1 To .recipients.Count
Debug.Print .recipients(R)
strgreetname = Left(.recipients(R), InStr(1, .recipients(R), " "))
strgreetname2 = Left(.recipients(2), InStr(1, .recipients(R), " "))
strGreetNameAll = strGreetNameAll & strgreetname
strGreetNameAll1 = strgreetname
strTo = Left(strGreetNameAll, InStr(1, .recipients(R), " "))
strTo1 = Left(strgreetname2, InStr(1, .recipients(R), " "))
Next R
StrTO and strTO1 are the first and second aliases in the To field, respectively.
For example:
Given .Recipients "William Hartnell", "Carole Ann Ford", "Patrick Troughton", "Anneke Wills", "Jon Pertwee", and "Tom Baker".
I want strTO to be William. I want strTO1 to be Carole. So it could be Dear William and Carole.

Based on your comment that you want strTO to be all characters up to the first space in .Recipients(1), and you want strTO1 to be all characters up to the first space in .Recipients(2), then you can use the following code:
strTO = Left(.Recipients(1), InStr(.Recipients(1) & " ", " ") - 1)
If .Recipients.Count > 1 Then
strTO1 = Left(.Recipients(2), InStr(.Recipients(2) & " ", " ") - 1)
Else
strTO1 = ""
End If
And, if you were using this to create a "greeting", you could just use something like:
strGreetName = Left(.Recipients(1), InStr(.Recipients(1) & " ", " ") - 1)
If .Recipients.Count > 1 Then
strGreetName = strGreetName & " and " & Left(.Recipients(2), InStr(.Recipients(2) & " ", " ") - 1)
End If
and not worry about having strTO and strTO1.
Or, if you wanted to include all the names, you could use something like
strGreetName = ""
For i = 1 to .Recipients.Count
If i > 1 Then
If i = .Recipients.Count Then
strGreetName = strGreetName & " and "
Else
strGreetName = strGreetName & ", "
End If
End If
strGreetName = strGreetName & Left(.Recipients(i), InStr(.Recipients(i) & " ", " ") - 1)
Next

you are doing this
for r = 1 to 10
a = r + 4
next r
for i = 1 to 10
b = a + 2
next r
when you want
for r = 1 to 10
a = r + 4
b = a + 2
next r

Related

Italicizing a specific part of a concatenation for a different worksheet

I am just beginning to experiment with VBA and wanted to create code that italicized the title of presentations in a concatenation, given that Excel normally does not allow that to happen. I also want to paste the output in another worksheet.
The current code is what I created to paste the concatenation output in a specific column of the SAME worksheet (named "Presentations Table"), but I am having trouble figuring out how to:
paste the concatenated output in a specific column of a different worksheet (named "Presentations Cited") starting from the first row and column in the same workbook
how to get it to automatically italicize ONLY the text from the title column. All other text in the concatenation should not be italicized.
I would be grateful for any help!
Table structure:
Current output:
Worksheets("Presentations Table").Range("a3", Worksheets("Presentations Table").Range("a3").End(xlDown)).Select
Row = 1
col = 1
For Each Cell In Selection
Authors = Cells(Row, col)
Year_Month = Cells(Row, col + 1)
Title = Cells(Row, col + 2)
Presentation_Type = Cells(Row, col + 3)
Event_Name = Cells(Row, col + 4)
Location = Cells(Row, col + 5)
Worksheets("Presentations Table").Cells(Row, col + 2) = Authors & " (" & Year_Month & "). " & Title & ". " & Presentation_Type & " at the " & Event_Name & ", " & Location & "."
Row = Row + 1
Next
You can paste them as HTML formatting with something like this (not tested):
Dim c As Range, s As String
Set c = ThisWorkbook.Worksheets("Presentations Table").Cells(3)
s = "<html>"
While c <> ""
s = s & c & " (" & c(, 2) & "). <i>" & c(, 3) & "</i>. " & c(, 4) & " at the " & c(, 5) & ", " & c(, 6) & ".</br>"
set c = c(2) ' the cell below c
Wend
With CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
.SetText s
.PutInClipboard
End With
c(2).PasteSpecial

Trim both newlines and spaces in VBA 7.0

Trim (in VBA for MS-Access 2010) does not remove vbCrLfs, only spaces. In the immediate window, I get
? Len(vbCrLf & "a" & vbCrLf & "b" & vbCrLf)
8
? Len(Trim(vbCrLf & "a" & vbCrLf & "b" & vbCrLf))
8
For spaces however:
? Len(" " & "a" & " " & "b" & " ")
5
? Len(Trim(" " & "a" & " " & "b" & " "))
3
How to make a trim that removes vbCrLFs on the ends only?
If you don't mind removing ALL new lines (and not just edges) you could just do:
myStr = Application.clean(Application.trim(myStr))
For imitating Trim function, you'd need to test each character in your string's edges:
Function TrimNewLines(mtStr As String) As String
Dim pattern As String, c As String
Dim i As Integer: i = 1
pattern = "[" & Chr(10) & Chr(13) & "]"
c = Mid(mtStr, i, 1)
Do While c Like pattern
i = i + 1
c = Mid(mtStr, i, 1)
Loop
mtStr = Mid(mtStr, i, Len(mtStr))
i = Len(mtStr)
c = Mid(mtStr, i, 1)
Do While c Like pattern
i = i - 1
c = Mid(mtStr, i, 1)
Loop
mtStr = Mid(mtStr, 1, i)
TrimNewLines = mtStr
End Function
This seems to have done the trick:
Public Function trimNewlinesAndSpaces(chaine As String) As String
chaine = Trim(chaine)
Do While (left(chaine, 2) = vbCrLf) Or right(chaine, 2) = vbCrLf
If left(chaine, 2) = vbCrLf Then
chaine = right(chaine, Len(chaine) - 2)
End Ifj
If right(chaine, 2) = vbCrLf Then
chaine = left(chaine, Len(chaine) - 2)
End If
chaine = Trim(chaine)
Loop
trimNewlinesAndSpaces = chaine
End Function

Fill a column from a concatenate range

What I have this far is below.
Range("F4").Value = Range("D4").Value & " x " & Range("E4")
This is creating a concatenate in Column F for me. This is working fine for the individual cell but I would like this to continue concatenating down until it hits an empty cell.
I have tried looping without success.
below is an image of the sheet
Set Concat= Rows(4)
concatstring =""
For i = 1 To Concat.Cells.Count
If Concat.Cells(i).Text <> "" Then
Concatstring = concatstring & " x " & Concat.Cells(i).Text
Else
Exit for
End If
Next
Concat.cells(I).text = concatstring
This should do it. Let me know if you still have problem.
Try this...
i=4
Do while(Range("D" & i).Value<> "")
Range("F" & i).Value = Range("D" & i).Value & " x " & Range("E" & i)
Loop

comparing dataset through vb.net

dasaset (ds) contain value like this comapring two dataset and write the phone number from dataset to notepad which is not equal to dataset1.but am getting the result in notepad like the phone numbers which are equal to both dataset
dataset
-------
91 9942321400
91 9865015695
91 9677031515
91 9994828285
91 9688104229
dataset1 values
----------------
91 9942321400
91 9865015695
91 9677031515
expected result in notepad
--------------------------
91 9994828285
91 9688104229
my code
-------
Dim i As Integer = 0
Dim toggle As Boolean = False
Do While (i <= ds1.Tables(0).Rows.Count - 1)
Dim phone As String = ds1.Tables(0).Rows(i).Item(1).ToString
Dim j As Integer = 0
Do While (j <= Ds.Tables(0).Rows.Count - 1)
Dim dumphone As String = Ds.Tables(0).Rows(j).Item(4).ToString
If dumphone <> phone Then toggle = True 'This will set your flag to add the output.
j = (j + 1)
Loop
'After we're done checking if there's a match, we decided to add it to the output.
If toggle = True Then
TextBox1.AppendText(a.ToString & "|" & b.ToString & "|" & c.ToString & "|" & d.ToString & "|" & phone.ToString & "|" & e1.ToString & "|" & f.ToString & "|" & g.ToString & "|" & h.ToString & "|" & i1.ToString & "|" & j1.ToString & "|" & k.ToString & "|" & l.ToString & "|" & m.ToString & "|" & n1.ToString & "|" & o.ToString & "|" & p.ToString & "|" & q.ToString & "|" & r.ToString & "|" & s.ToString & "|" & t.ToString & "|" & u.ToString & "|" & v.ToString & "|" & w.ToString & "|" & x.ToString)
sw.WriteLine(TextBox1.Text)
TextBox1.Text = ""
toggle = False 'Reset the flag for the next value
End If
i = (i + 1) 'Move to the next value to check against.
Loop
but am getting the output in note pad like this
------------------------------------------------
91 9942321400
91 9865015695
91 9677031515
I tried this way and I got the result you were looking for...
For i As Integer = 0 To dataset.Tables(0).Rows.Count - 1
Dim found As Boolean = False
For j As Integer = 0 To dataset1.Tables(0).Rows.Count - 1
If dataset.Tables(0).Rows(i)(0).ToString = dataset1.Tables(0).Rows(j) (0).ToString Then
found = True
End If
Next
If found = False Then
'here you are getting the right result in each loop
'in this example i'm showing the result in a textbox
'just change the instruction and write them in your note pad or wherever you want to
MsgBox(dataset.Tables(0).Rows(i)(0).ToString)
End If
Next

Array not printing in 2D form inside a textBox in visualbasic

Private Sub Command4_Click()
Dim x As Integer
r = InputBox("Enter row size ")
c = InputBox("Enter column size ")
ReDim arr(r, c) As Integer
For i = 0 To r - 1 Step 1
For j = 0 To c - 1 Step 1
arr(i, j) = InputBox("Enter row : " & (i + 1) & "column size : " & (j + 1))
Next j
Next i
For i = 0 To r - 1
For j = 0 To c - 1
Text1.Text = Text1.Text & " " & arr(i, j)
Next j
Text1.Text = Text1.Text & vbNewLine & vbCr
Next i
End Sub
This is my code for taking inputs in an array. Here everything is working fine except this line "Text1.Text = Text1.Text & vbNewLine & vbCr" here I am trying to print the array in row-column in 2D form inside a text box but its not happening "vbNewLine or vbcr" both are not working and my array is getting printed in a single line.
I suggest vbCrLf instead of vbNewLine & vbCr, and you need to make sure you have your textbox set to Multiline in the properties editor.