Here are some screen caps of the data structure of the Excel workbook I am working with:
OK, I have gone through and edited the code based on what everyone has said. It still needs a lot of work though.
What I am stuck on now, is the error handling. Obviously if one of the key words - Last
, First
, Middle
, or Rank
- is not found, it will give me an error.
What I am ultimately trying to do is output a blank if there is no value (word) following the key word, and the value word if there is one. If the key word is missing I want to output a blank. It is also possible for the value word to be in the row below the key word. I want to output that value in this case as well.
I am trying to do this now using If
-Else
statements. However, I think they may be written wrong because if the key word isn't found, I get an error.
Option Explicit
Sub find2()
Dim lrd As Long
Dim lrdWS1 As Long
Dim iRow As Integer
Dim celltosplit As String
Dim result As String
'--------------------------------------------------------------------------------------------------------------------------------------
lrdWS1 = Sheets("Table 1").Cells(Sheets("Table 1").Rows.count, 1).End(xlUp)(2).Row
Sheets.Add(After:=Sheets(Sheets.count)).name = "FieldValues"
lrd = Sheets("FieldValues").Cells(Sheets("FieldValues").Rows.count, 1).End(xlUp)(1).Row
Worksheets("Table 1").Activate
'--------------------------------------------------------------------------------------------------------------------------------------
Do While Worksheets("Table 1").Activate And Cells.find(What:="Last", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Application.Goto (Cells(1, 1))
'--------------------------------------------------------------------------------------------------------------------------------------
Worksheets("Table 1").Activate
Application.Goto (Cells(1, 1))
If Cells.find(What:="Last", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate Then
Selection.Copy
Sheets("FieldValues").Activate
Range("A" & lrd).Activate
ActiveSheet.Paste
Columns("A:A").EntireColumn.AUTOFIT
Cells.Replace What:="Last", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
lrd = Sheets("FieldValues").Cells(Sheets("FieldValues").Rows.count, 1).End(xlUp)(2).Row
Worksheets("Table 1").Activate
ActiveCell.UnMerge
Selection.Replace What:="Last", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Else
Cells(1, lrd) = ""
lrd = Sheets("FieldValues").Cells(Sheets("FieldValues").Rows.count, 1).End(xlUp)(2).Row
End If
'-------------------------------------------------------------------------------------------------------------------------------------
Worksheets("Table 1").Activate
Application.Goto (Cells(1, 1))
If Cells.find(What:="First", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate Then
Selection.Copy
Sheets("FieldValues").Activate
Range("A" & lrd).Activate
ActiveSheet.Paste
Columns("A:A").EntireColumn.AUTOFIT
Cells.Replace What:="First", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
lrd = Sheets("FieldValues").Cells(Sheets("FieldValues").Rows.count, 1).End(xlUp)(2).Row
Worksheets("Table 1").Activate
ActiveCell.UnMerge
Selection.Replace What:="First", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Else
Cells("1", lrd) = ""
lrd = Sheets("FieldValues").Cells(Sheets("FieldValues").Rows.count, 1).End(xlUp)(2).Row
End If
'-------------------------------------------------------------------------------------------------------------------------------------
Worksheets("Table 1").Activate
Application.Goto (Cells(1, 1))
If Cells.find(What:="Middle", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate Then
Selection.Copy
Sheets("FieldValues").Activate
Range("A" & lrd).Activate
ActiveSheet.Paste
Columns("A:A").EntireColumn.AUTOFIT
Cells.Replace What:="Middle", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
lrd = Sheets("FieldValues").Cells(Sheets("FieldValues").Rows.count, 1).End(xlUp)(2).Row
Worksheets("Table 1").Activate
ActiveCell.UnMerge
Selection.Replace What:="Middle", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Else
Cells("A", lrd) = ""
lrd = Sheets("FieldValues").Cells(Sheets("FieldValues").Rows.count, 1).End(xlUp)(2).Row
End If
'-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Worksheets("Table 1").Activate
Application.Goto (Cells(1, 1))
If Cells.find(What:="Rank", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate Then
Selection.Copy
Sheets("FieldValues").Activate
Range("A" & lrd).Activate
ActiveSheet.Paste
Columns("A:A").EntireColumn.AUTOFIT
Cells.Replace What:="Rank", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
lrd = ActiveCell.Row + 2
Worksheets("Table 1").Activate
ActiveCell.UnMerge
Selection.Replace What:="Rank", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Else
Cells("A", lrd) = ""
lrd = ActiveCell.Row + 2
End If
Loop