Forum Discussion
I need help with the baseball file.
I need help with the baseball file.
Thes Teams Do Not Do Anything To
Blue Jays
White Sox
Red Sox
Column B
At Astros
I Want @Astros All Other Teams To
Mariners At
I Want Vs Mariners All Other Teams To
Column C Change Time To One Hour Earlier
Trim All Cells
I Want Vba Code
Thank you very much
3 Replies
- NikolinoDEPlatinum Contributor
The attached file contains the VBA code. Oddly enough, I couldn't insert it as code. Perhaps this information will help; it's untested.
My answers are voluntary and without guarantee!
- sf49ers19238597Iron Contributor
The code did not work on my end// Thank You try
- NikolinoDEPlatinum Contributor
If this code also doesn't work, a little more information would be helpful in order to assist further. Basic information, such as how it can be found here, and information on how this code interacts with the file or other VBA code would be beneficial.
Sub BaseballDataTest() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Set ws = ActiveSheet ' Find last row with data in Column A lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row If lastRow = 1 And ws.Cells(1, "A").Value = "" Then MsgBox "No data found in Column A", vbExclamation Exit Sub End If Application.ScreenUpdating = False For i = 1 To lastRow ' 1. TRIM all cells in columns A, B, C ws.Cells(i, 1).Value = Trim(ws.Cells(i, 1).Value) ws.Cells(i, 2).Value = Trim(ws.Cells(i, 2).Value) ws.Cells(i, 3).Value = Trim(ws.Cells(i, 3).Value) ' 2. COLUMN B: Only change if team is NOT Blue Jays, White Sox, or Red Sox Dim teamName As String teamName = ws.Cells(i, 1).Value If teamName <> "Blue Jays" And teamName <> "White Sox" And teamName <> "Red Sox" Then Dim colBValue As String colBValue = ws.Cells(i, 2).Value ' Exact matches (case-sensitive as shown) If colBValue = "At Astros" Then ws.Cells(i, 2).Value = "@Astros" ElseIf colBValue = "Mariners At" Then ws.Cells(i, 2).Value = "Vs Mariners" End If End If ' 3. COLUMN C: Subtract 1 hour from any time value If IsDate(ws.Cells(i, 3).Value) Then ws.Cells(i, 3).Value = ws.Cells(i, 3).Value - TimeSerial(1, 0, 0) End If Next i Application.ScreenUpdating = True MsgBox "Complete! Processed rows 1 through " & lastRow, vbInformation End SubWhat This Code Does
Action
Result
Trims Column A, B, C
Removes extra spaces
Blue Jays / White Sox / Red Sox in Column A
Column B is NOT changed
Any other team in Column A + At Astros in Column B
Changes to @Astros
Any other team in Column A + Mariners At in Column B
Changes to Vs Mariners
Valid time in Column C
Subtracts 1 hour
This code will work if your data matches the assumptions above.