Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I need to add the word "and" to the code below so that my output will be, for
instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") ..Range("A" & NewRow) = Num ..Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You sure you want:
1, and, 3, and, 5, and 7 I would think that: 0, and 3, and 5, and 7 would be more natural. If you mean the second version, I'd use: Dim ColCount As Long Dim RowCount As Long Dim Data As String Dim mySep As String mySep = ", and " With ActiveSheet RowCount = 5 'for testing For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & mySep & ColCount - 3 End If End If Next ColCount End With If you really meant the top version, then change this: mySep = ", and " to mySep = ", and, " ILoveMyCorgi wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thank you so much. I needed the top version, believe it or not. I truly
appreciate this. Could you recommend a beginner's VBA book that would have these types of formulas? "Dave Peterson" wrote: You sure you want: 1, and, 3, and, 5, and 7 I would think that: 0, and 3, and 5, and 7 would be more natural. If you mean the second version, I'd use: Dim ColCount As Long Dim RowCount As Long Dim Data As String Dim mySep As String mySep = ", and " With ActiveSheet RowCount = 5 'for testing For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & mySep & ColCount - 3 End If End If Next ColCount End With If you really meant the top version, then change this: mySep = ", and " to mySep = ", and, " ILoveMyCorgi wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I just tried to run this and I'm not sure if I'm supposed to incorporate this
into my original code. If so, where? When I replaced your code, it did not work. "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Maybe...
Option Explicit Sub formatData() Dim NewRow As Long Dim RowCount As Long Dim Num As Double Dim Data As String Dim ColCount As Long Dim mySep As String mySep = ", and, " NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount).Value Data = "" For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & mySep & ColCount - 3 End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub ILoveMyCorgi wrote: I just tried to run this and I'm not sure if I'm supposed to incorporate this into my original code. If so, where? When I replaced your code, it did not work. "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks again... this ran perfectly... I know this is simple programming and I
know I can do it... I just need to do more of this via trial and error. I really appreciate this! "Dave Peterson" wrote: Maybe... Option Explicit Sub formatData() Dim NewRow As Long Dim RowCount As Long Dim Num As Double Dim Data As String Dim ColCount As Long Dim mySep As String mySep = ", and, " NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount).Value Data = "" For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & mySep & ColCount - 3 End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub ILoveMyCorgi wrote: I just tried to run this and I'm not sure if I'm supposed to incorporate this into my original code. If so, where? When I replaced your code, it did not work. "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
That's a good attitude. I bet you'll learn a lot more from your trials (and
errors) than by your successes. At least that's true for me. ILoveMyCorgi wrote: Thanks again... this ran perfectly... I know this is simple programming and I know I can do it... I just need to do more of this via trial and error. I really appreciate this! "Dave Peterson" wrote: Maybe... Option Explicit Sub formatData() Dim NewRow As Long Dim RowCount As Long Dim Num As Double Dim Data As String Dim ColCount As Long Dim mySep As String mySep = ", and, " NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount).Value Data = "" For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & mySep & ColCount - 3 End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub ILoveMyCorgi wrote: I just tried to run this and I'm not sure if I'm supposed to incorporate this into my original code. If so, where? When I replaced your code, it did not work. "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson -- Dave Peterson |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The output has changed a bit... the "and" should only now show up at the next
to the last T if there are more than 2 T's. For instance: 1, 3, and, 5 1, and, 4 or 2 How do I generate that "and" in only one spot in the code you sent me? "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Option Explicit
Sub formatData() Dim NewRow As Long Dim RowCount As Long Dim Num As Double Dim Data As String Dim ColCount As Long Dim mySep As String Dim HowManyTs As Long Dim TCtr As Long NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount).Value Data = "" HowManyTs = Application.CountIf(.Cells(RowCount, "C").Resize(1, 8), "T") TCtr = 0 For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then TCtr = TCtr + 1 If Data = "" Then Data = ColCount - 3 Else If TCtr = HowManyTs Then mySep = ", and, " Else mySep = ", " End If Data = Data & mySep & ColCount - 3 End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub ILoveMyCorgi wrote: The output has changed a bit... the "and" should only now show up at the next to the last T if there are more than 2 T's. For instance: 1, 3, and, 5 1, and, 4 or 2 How do I generate that "and" in only one spot in the code you sent me? "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Watch for line wrap:
HowManyTs = Application.CountIf(.Cells(RowCount, "C").Resize(1, 8), "T") is one line in your code. Dave Peterson wrote: Option Explicit Sub formatData() Dim NewRow As Long Dim RowCount As Long Dim Num As Double Dim Data As String Dim ColCount As Long Dim mySep As String Dim HowManyTs As Long Dim TCtr As Long NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount).Value Data = "" HowManyTs = Application.CountIf(.Cells(RowCount, "C").Resize(1, 8), "T") TCtr = 0 For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then TCtr = TCtr + 1 If Data = "" Then Data = ColCount - 3 Else If TCtr = HowManyTs Then mySep = ", and, " Else mySep = ", " End If Data = Data & mySep & ColCount - 3 End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub ILoveMyCorgi wrote: The output has changed a bit... the "and" should only now show up at the next to the last T if there are more than 2 T's. For instance: 1, 3, and, 5 1, and, 4 or 2 How do I generate that "and" in only one spot in the code you sent me? "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson -- Dave Peterson |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
It's me again... I hope I'm not being a pest. I played with the additional
line of code and I the output was 1, 2, 3, 4, 5 without the "and," before the 5 What did I do wrong? "Dave Peterson" wrote: Watch for line wrap: HowManyTs = Application.CountIf(.Cells(RowCount, "C").Resize(1, 8), "T") is one line in your code. Dave Peterson wrote: Option Explicit Sub formatData() Dim NewRow As Long Dim RowCount As Long Dim Num As Double Dim Data As String Dim ColCount As Long Dim mySep As String Dim HowManyTs As Long Dim TCtr As Long NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount).Value Data = "" HowManyTs = Application.CountIf(.Cells(RowCount, "C").Resize(1, 8), "T") TCtr = 0 For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then TCtr = TCtr + 1 If Data = "" Then Data = ColCount - 3 Else If TCtr = HowManyTs Then mySep = ", and, " Else mySep = ", " End If Data = Data & mySep & ColCount - 3 End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub ILoveMyCorgi wrote: The output has changed a bit... the "and" should only now show up at the next to the last T if there are more than 2 T's. For instance: 1, 3, and, 5 1, and, 4 or 2 How do I generate that "and" in only one spot in the code you sent me? "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson -- Dave Peterson |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
It worked! I just needed to play with it! Thank you so much for all your
help!! Susan "Dave Peterson" wrote: Watch for line wrap: HowManyTs = Application.CountIf(.Cells(RowCount, "C").Resize(1, 8), "T") is one line in your code. Dave Peterson wrote: Option Explicit Sub formatData() Dim NewRow As Long Dim RowCount As Long Dim Num As Double Dim Data As String Dim ColCount As Long Dim mySep As String Dim HowManyTs As Long Dim TCtr As Long NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount).Value Data = "" HowManyTs = Application.CountIf(.Cells(RowCount, "C").Resize(1, 8), "T") TCtr = 0 For ColCount = 3 To 10 If UCase(.Cells(RowCount, ColCount)) = "T" Then TCtr = TCtr + 1 If Data = "" Then Data = ColCount - 3 Else If TCtr = HowManyTs Then mySep = ", and, " Else mySep = ", " End If Data = Data & mySep & ColCount - 3 End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub ILoveMyCorgi wrote: The output has changed a bit... the "and" should only now show up at the next to the last T if there are more than 2 T's. For instance: 1, 3, and, 5 1, and, 4 or 2 How do I generate that "and" in only one spot in the code you sent me? "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? -- Dave Peterson -- Dave Peterson |
#13
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks! That additional line of code HowManyTs =
Application.CountIf(.Cells(RowCount, "C").Resize(1, 8), "T") does not run... Not sure how to read it. Sorry. Susan "ILoveMyCorgi" wrote: I need to add the word "and" to the code below so that my output will be, for instance: 1,[space]and,[space]3 ... etc. The code I currently have is: Sub formatData() NewRow = 1 With Sheets("sheet1") RowCount = 2 Do While .Range("A" & RowCount) < "" Num = .Range("A" & RowCount) Data = "" For ColCount = 3 To 10 If .Cells(RowCount, ColCount) = "T" Then If Data = "" Then Data = ColCount - 3 Else Data = Data & ", " & (ColCount - 3) End If End If Next ColCount With Sheets("sheet2") .Range("A" & NewRow) = Num .Range("B" & NewRow) = Data NewRow = NewRow + 1 End With RowCount = RowCount + 1 Loop End With End Sub How do I modify this code to include the and's? |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
any formula to convert numbers in word form, e.g. "2" as "Two"? | Excel Worksheet Functions | |||
Code to create a list of records in another sheet, based on the entryof the word "Terminated" in a given column | Excel Programming | |||
Check if cells contain the word "Thailand", return "TRUE" | Excel Worksheet Functions | |||
Can you "duplicate" "copy" listboxes and code to multiple cells? | Excel Programming | |||
Looking for VB code to test for "RING" , "BUSY" disconnects or other signals | Excel Programming |