Find a specific text and cut all the lines below it and paste to another sheet - excel

I am trying to find the word "BREAK" and cut the lines below it until it reaches another word "BREAK" and transfer it to another Sheet.
I need to separate it to 5 sheets since I have 5 word of "BREAK" in the file.
Sub Fails()
Dim mFind As Range
Set mFind = Columns("A").Find("BREAK")
If mFind Is Nothing Then
MsgBox "There is no cell found with the text 'BREAK'" _
& " in column A of the active sheet."
Exit Sub
End If
firstaddress = mFind.Address
Do
If IsDate(mFind.Offset(1, 0)) = True Then
Range(mFind, Cells(mFind.Row + 2, "A")).EntireRow.Cut
Sheets("Sheet2").Select
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
ElseIf WorksheetFunction.IsNumber(mFind.Offset(1, 0)) = True Then
Range(mFind, Cells(mFind.Row + 3, "A")).EntireRow.Cut
Sheets("Sheet2").Select
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
End If
Sheets("Sheet1").Select
Set mFind = Columns("A").FindNext(mFind)
If mFind Is Nothing Then Exit Sub
Loop While mFind.Address <> firstaddress
End Sub
Nothing is happening with the code above. Any help would be greatly appreciated.
Thank you and have a good day everyone.

Give this code a try, your If statement was false
Sub Fails()
Dim mFind As Range
Dim Compteur As Integer
Dim IdSheet As Integer
Dim ErrorBool As Boolean
Set mFind = Columns("A").Find("Break")
Set mfind2 = Columns("A").Find("Break")
If mFind Is Nothing Then
MsgBox "There is no cell found with the text 'Break'" _
& " in column A of the active sheet."
Exit Sub
End If
firstaddress = mFind.Address
IdSheet = 1
Compteur = 0
Do
Set mfind2 = Columns("A").FindNext(mFind)
If mfind2 Is Nothing Then
Compteur = Sheet1.Range("A1048576").End(xlUp).Row
'Exit Sub
Else:
If mFind.Row < mfind2.Row Then
Compteur = mfind2.Row
End If
If mFind.Row > mfind2.Row Then
ErrorBool = True
End If
If ErrorBool = True Then
Range(mFind, Cells(mFind.Row + 1, "A")).EntireRow.Cut
End If
End If
Range("A" & mFind.Row + 1 & ":A" & Compteur - 1).EntireRow.Cut
If mFind Is Nothing Then
Else: IdSheet = IdSheet + 1
End If
Sheets("Sheet" & IdSheet & "").Select
Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Range(mFind, Cells(mFind.Row, "A")).EntireRow.Delete
Set mFind = Columns("A").Find("Break")
Set mfind2 = Columns("A").Find("Break")
If mFind Is Nothing Then Exit Sub
Set mFind = Columns("A").FindNext(mFind)
Loop While mFind.Address <> firstaddress
End Sub
Note : You must create Sheet1, Sheet2, Sheet3, Sheet4 ,Sheet5 and so on before run macro.

Related

VBA If range [J:K] not empty, then copy [H:I] to the end of [J:K], else offset

I have two ranges, [H23:I32] and [J23:K50].
I need to copy values from [H23:I32] to [J23:K50] if [J23:K50] is empty, and if [J23:K50] is not empty I need to find the last row and add [H23:I32] below.
The "copy if empty" works, but the "add to the end of the list" doesn't unfortunately.
It does something, but clearly not the thing I need.
Sub Total_Loop()
Application.ScreenUpdating = False
Dim c As Range
For Each c In Range("J23:K50" & Cells(Rows.Count, "J").End(xlUp).Row)
If c.Value <> "" Then
Range("J23:K50" & Cells(Rows.Count, "J").End(xlUp).Row + 1) = Range("H23:I32")
Else: c.Value = c.Offset(, -2).Value
End If
Next
Application.ScreenUpdating = True
End Sub
Any suggestions how to fix this?
EDIT: After a lot of struggle I found a suitable solution!
Sub MoveData()
Dim lrow As Long
Dim ws As Worksheet
Set ws = Sheets("Loot")
If WorksheetFunction.CountA(ws.Range("J23:K50")) = 0 Then
ws.Range("H23:I32").Copy
ws.Range("J23").PasteSpecial xlPasteValues
Else
lrow = ws.Range("J23:K50").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ws.Range("H23:I32").Copy
ws.Range("J" & lrow + 1).PasteSpecial xlPasteValues
End If
End Sub

Looping over every row until last line of data

I have three macros that work for my first row. I want to create a loop to make that code run for all the rows until the last row of input.
It prints what is missing when an empty cell is found in columns B, D and E. (I will eventually put the three macros into one to make it more concise.)
This is the code for the first row:
'checking if PayorID is empty
Sub error_field()
Sheets("1099-Misc_Form_Template").Select
Range("A2").Select
If Range("A2").Value = "" Then
ActiveCell.Value = ActiveCell.Value & "PayorID"
End If
End Sub
'checking if TIN is empty
Sub error_field2()
Sheets("1099-Misc_Form_Template").Select
Range("A2").Select
If Range("E2").Value = "" Then
ActiveCell.Value = ActiveCell.Value & ", TIN"
End If
End Sub
'checking if AccountNo is empty
Sub error_field3()
Sheets("1099-Misc_Form_Template").Select
Range("A2").Select
If Range("F2").Value = "" Then
ActiveCell.Value = ActiveCell.Value & ", AccountNo"
End If
End Sub
This is what I tried for a loop:
'repeating for all rows
Sub repeat_all_rows()
Dim sh As Worksheet
Dim rw As Range
Dim RowCount As Integer
RowCount = 0
Set sh = ActiveSheet
For Each rw In sh.Rows
If Range("A2").Value = "" Then
ActiveCell.Value = ActiveCell.Value & "PayorID"
If Range("E2").Value = "" Then
ActiveCell.Value = ActiveCell.Value & ", TIN"
If Range("F2").Value = "" Then
ActiveCell.Value = ActiveCell.Value & ", AccountNo"
Exit For
End If
RowCount = RowCount + 1
Next rw
End Sub
I want code to be performed on all the rows until the last row of data is found.
Something like this should work:
'repeating for all rows
Sub repeat_all_rows()
Dim sh As Worksheet
Dim rw As Range
Dim RowCount As Integer
RowCount = 0
Set sh = ActiveSheet
For Each rw In sh.UsedRange.Rows
FlagMissing rw, "A", "Payor ID"
FlagMissing rw, "E", "TIN"
FlagMissing rw, "F", "AccountNo"
Next rw
End Sub
Sub FlagMissing(rw As Range, col as String, Flag As String)
If Len(Trim(rw.cells(1, col).value)) = 0 Then
With rw.Cells(1)
'add the flag with a comma if there's already content there
.Value = .Value & IIf(.Value="", "", ", ") & Flag
End with
End If
End sub
...though I've not accounted for that Exit For in your posted code.

Delete Blank Lines

I need to have this code look from the bottom up and once it reaches a cell in Column G that is populated it stops deleting lines. Can some one help me out. There will be blanks in column G but, I just need it to look from the bottom up to the last populated cell in column G and delete everything below that.
Routine to Delete Blank Lines to the Datasheet, Uncertainty and Repeatability Sheets
Public Sub DeleteBlankLines()
' Declaring the variables
Dim WS As Worksheet
Dim UncWs As Worksheet, RepWs As Worksheet, ImpWs As Worksheet
Dim StopAtData As Boolean
Dim UserAnswer As Variant
Dim rngDelete As Range, UncDelete As Range, RepDelete As Range, ImpDelete As Range
Dim RowDeleteCount As Integer
'Set Worksheets
Set UncWs = ThisWorkbook.Sheets("Uncertainty")
Set RepWs = ThisWorkbook.Sheets("Repeatability")
Set WS = ThisWorkbook.Sheets("Datasheet")
Set ImpWs = ThisWorkbook.Sheets("Import Map")
'Set Delete Variables to Nothing
Set rngDelete = Nothing
Set UncDelete = Nothing
Set RepDelete = Nothing
Set ImpDelete = Nothing
RowDeleteCount = 0
'Determine which cells to delete
UserAnswer = MsgBox("Do you want to delete empty rows " & _
"outside of your data?" & vbNewLine, vbYesNoCancel)
If UserAnswer = vbYes Then
StopAtData = True
'Not needed Turn off at Call in Form
'Application.Calculation = xlCalculationManual
'Application.ScreenUpdating = False
'Application.EnableEvents = False
' Set Range
DS_LastRow = WS.Range("A" & WS.Rows.Count).End(xlUp).Row
For CurrentRow = DS_StartRow To DS_LastRow Step 1
' Delete blank rows by checking the value of cell in column G (Nominal Value)
With WS.Range("G" & CurrentRow & ":O" & CurrentRow)
If WorksheetFunction.CountBlank(.Cells) >= 9 Then
If rngDelete Is Nothing Then
Set rngDelete = WS.Rows(CurrentRow)
Set UncDelete = UncWs.Rows(CurrentRow)
Set RepDelete = RepWs.Rows(CurrentRow)
Set ImpDelete = ImpWs.Rows(CurrentRow)
RowDeleteCount = 1
Else
Set rngDelete = Union(rngDelete, WS.Rows(CurrentRow))
Set UncDelete = Union(UncDelete, UncWs.Rows(CurrentRow))
Set RepDelete = Union(RepDelete, RepWs.Rows(CurrentRow))
Set ImpDelete = Union(ImpDelete, ImpWs.Rows(CurrentRow))
RowDeleteCount = RowDeleteCount + 1
End If
End If
End With
Next CurrentRow
Else
Exit Sub
End If
'Refresh UsedRange (if necessary)
If RowDeleteCount > 0 Then
UserAnswer = MsgBox("This will Delete " & RowDeleteCount & " rows, Do you want to delete empty rows?" & vbNewLine, vbYesNoCancel)
If UserAnswer = vbYes Then
' Delete blank rows
If Not rngDelete Is Nothing Then
UncWs.Unprotect ("$1mco")
RepWs.Unprotect ("$1mco")
rngDelete.EntireRow.Delete Shift:=xlUp
UncDelete.EntireRow.Delete Shift:=xlUp
RepDelete.EntireRow.Delete Shift:=xlUp
ImpDelete.EntireRow.Delete Shift:=xlUp
UncWs.Protect "$1mco", , , , , True, True
RepWs.Protect ("$1mco")
End If
Else
MsgBox "No Rows will be Deleted.", vbInformation, "No Rows Deleted"
End If
Else
MsgBox "No blank rows were found!", vbInformation, "No Blanks Found"
End If
' Set New Last Row Moved to Event
DS_LastRow = WS.Range("A" & WS.Rows.Count).End(xlUp).Row
'Update Line Count on Datasheet
WS.Range("A9").Value = DS_LastRow - DS_StartRow + 1
'Not needed Turn on at Call in Form
'Application.Calculation = xlCalculationAutomatic
'Application.ScreenUpdating = True
'Application.EnableEvents = True
End Sub
Delete Below Last Row
Instead of Delete you can use Clear, or if you want to preserve the formatting below the last row, you can use ClearContents.
The Code
Option Explicit
Sub DelRows()
Const cSheet As Variant = "Sheet1" ' Worksheet Name/Index
Const cColumn As Variant = "G" ' Cirteria Column Letter/Number
Dim lastR As Long ' Last Row
With ThisWorkbook.Worksheets(cSheet)
lastR = .Cells(.Rows.Count, cColumn).End(xlUp).Row
.Range(.Cells(lastR + 1, 1), .Cells(.Rows.Count, 1)).EntireRow.Delete
End With
End Sub

Selecting a Cell based on Row and Column Headers, and Inputting a Value From an Input Section on that Sheet

I've created a Table from the range A112:H206, with days of the week (sunday, monday, etc) heading the table row from B112-H112. In column A, I have names of individuals listed going all the way down to A206.
I have an input section at the top of the spreadsheet, where a user will select a name from a drop down menu in cell A109, a day of the week from a drop down menu in cell B2, and finally a value in cell C109 which should be inputted in the corresponding cell in the table.
I created a button named "Enter" to which upon clicking should search for the corresponding cell based on the input section above, and input the C109 Value in that cell. Unfortunately my attempts using VBA were unsuccessful! Any help would be greatly appreciated.
Thank you!
Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Dim x As Range
Dim y As Range
Dim valX, valY
Set ws1 = Sheets("Sheet1")
valX = ws1.Range("B2").Value
Set x = ws1.Range("A112:H112").Find(What:=valX, LookIn:=xlValues, _
lookat:=xlWhole)
If x Is Nothing Then
MsgBox "'" & valX & "' not found on '" & ws1.Name & "' !"
Exit Sub
End If
valY = ws1.Range("A109").Value
Set y = ws1.Range("A112:A206").Find(What:=valY, LookIn:=xlValues, _
lookat:=xlWhole)
If Not y Is Nothing Then
Range("C109").Select
Selection.Copy
ws1.Cells(x.Column, y.Row).Select
ActiveSheet.Paste
Range("C109").Select
Selection.ClearContents
Exit Sub
End If
End Sub
A friend of mine helped, I wanted to post it here just for reference for others!
Range("C109").Select
Selection.Copy
Dim Day As String
Dim Name As String
Dim nameFound As Boolean
Dim dayFound As Boolean
Name = Cells(109, "A").Value
Day = Cells(2, "B").Value
Range("A113").Select
nameFound = False
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = Name Then
nameFound = True
Exit Do
End If
ActiveCell.Offset(1, 0).Select
Loop
If nameFound = True Then
Dim nameAddress As Integer
nameAddress = ActiveCell.Row
Else
MsgBox "Name not found"
End If
Range("B112").Select
dayFound = False
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = Day Then
dayFound = True
Exit Do
End If
ActiveCell.Offset(0, 1).Select
Loop
If dayFound = True Then
Dim dayAddress As Integer
dayAddress = ActiveCell.Column
Else
MsgBox "Day not found"
End If
Cells(nameAddress, dayAddress).Select
ActiveSheet.Paste
If ActiveCell.Column = 2 Or ActiveCell.Column = 4 Or ActiveCell.Column = 6 Or ActiveCell.Column = 8 Then
ActiveCell.Interior.Color = RGB(83, 142, 213)
ElseIf ActiveCell.Column = 3 Or ActiveCell.Column = 5 Or ActiveCell.Column = 7 Then
ActiveCell.Interior.Color = RGB(182, 221, 232)
End If
Range("C109").Select
Selection.ClearContents
Untested:
Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Dim x As Range
Dim y As Range
Dim valX, valY
Set ws1 = Sheets("Sheet1")
valX = ws1.Range("A109").Value
Set x = ws1.Range("A112:H112").Find(What:=valX, LookIn:=xlValues, _
lookat:=xlWhole)
If x Is Nothing Then
MsgBox "'" & valX & "' not found on '" & ws1.Name & "' !"
Exit Sub
End If
valY = ws1.Range("B2").Value
Set y = ws1.Range("A112:A206").Find(What:=valY,LookIn:=xlValues, _
lookat:=xlWhole)
If Not y Is Nothing Then
With ws1.Range("C109")
.Copy ws1.Cells(y.Row, x.Column)' <<EDITED
.ClearContents
End With
Else
MsgBox "Name '" & valY & "' not found on '" & ws1.Name & "' !"
End If
End Sub

Setting up if cell is blank don't continue... and show a message

This code works perfectly. I only have one question, I want to make it so that if there is nothing in cell Q23 that it will not put anything into NCMR Data, and say something... the code is below of what I have, and below it is what I think I need to do to a specific section to work, can someone review and make sure I am on the right path?
Option Explicit
Sub NCMR()
Dim i As Integer
With Application
.ScreenUpdating = False
End With
'Internal NCMR
Dim wsInt As Worksheet
Dim wsNDA As Worksheet
'Copy Ranges
Dim c As Variant
'Paste Ranges
Dim P As Range
'Setting Sheet
Set wsInt = Sheets("NCMR Input")
Set wsNDA = Sheets("NCMR Data")
Set P = wsInt.Range("B61:V61")
With wsInt
c = Array(.Range("B11"), .Range("B14"), .Range("B17"), .Range("B20"), .Range("Q23"), .Range("B23") _
, .Range("Q11"), .Range("Q14"), .Range("Q17"), .Range("Q20"), .Range("R26"), .Range("V23") _
, .Range("V25"), .Range("V27"), .Range("B32"), .Range("B40"), .Range("B46"), .Range("B52") _
, .Range("D58"), .Range("L58"), .Range("V58"))
End With
For i = LBound(c) To UBound(c)
P(i + 1).Value = c(i).Value
Next
With wsNDA
Dim LastRow As Long
LastRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
wsInt.Rows("61").Copy
With .Rows(LastRow)
.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteValues
.Interior.Pattern = xlNone
End With
With .Range("A" & LastRow)
If LastRow = 3 Then
.Value = 1
Else
.Value = Val(wsNDA.Range("A" & LastRow - 1).Value) + 1
End If
.NumberFormat = "0#######"
End With
End With
With Application
.Range("A61:V61").ClearContents
.ScreenUpdating = True
End With
End Sub
What I want to do I think:
With wsInt
Dim f As Range
Set f = .Cell("Q23")
If IsEmpty(f) Then
MsgBox "The data can't entered, you have not entered any data into the Sales Order field."
Else
c = Array(.Range("B11"), .Range("B14"), .Range("B17"), .Range("B20"), .Range("Q23"), .Range("B23") _
, .Range("Q11"), .Range("Q14"), .Range("Q17"), .Range("Q20"), .Range("R26"), .Range("V23") _
, .Range("V25"), .Range("V27"), .Range("B32"), .Range("B40"), .Range("B46"), .Range("B52") _
, .Range("D58"), .Range("L58"), .Range("V58"))
End If
End With
Maybe as simple as:
With wsInt
If Len(.Range("Q23")) = 0 Then
MsgBox "The data can't be entered, you have not entered any data into the Sales Order field."
Exit Sub
End If
End With 'added this line for clarity

Resources