I need to remove some record from Excel spreadsheet. I want for macro to search for a certain name and upon finding a cell with that name, remove row containing it and next X rows.
So far I have a part that removes content of a cell upon certain words, but now I would need it to not clear but remove whole rows
Range("B2:H100").Replace What:="*Phone", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("B2:H100").Replace What:="*Queue", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("B2:H100").Replace What:="*2nd Line", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Try something like the code below:
Option Explicit
Sub RemoveRowsFindName()
Dim FindRng As Range
Dim xRows As Long
Dim FindWord As String
xRows = 7 ' number of extra rows to remove
FindWord = "Phone"
Set FindRng = Range("B2:H100").Find(What:=FindWord, LookAt:=xlPart, SearchOrder:=xlByRows)
If Not FindRng Is Nothing Then
Range("A" & FindRng.Row).Resize(1 + xRows, 1).EntireRow.Delete Shift:=xlShiftUp
Else ' word not found in range
MsgBox "Unable to find " & FindWord & " in range", vbCritical, "Find Error!"
End If
End Sub
#Shar Rado -
This would be a part of a bit larger script designed for clearing out excel spreadsheet to be more transparent for HR team, I've pasted your suggesion as:
Dim FindRng As Range
Dim xRows As Long
Dim FindWord As String
xRows = 7
FindWord = "Tony"
Set FindRng = Range("B2:H100").Find(What:=FindWord, LookAt:=xlPart, SearchOrder:=xlByRows)
If Not FindRng Is Nothing Then
Range("A" & FindRng.Row).Resize(1 + xRows, 1).EntireRow.Delete Shift:=xlShiftUp
End If
But the overall macro did the same it done previously - didn't return any errors nor did the needed removal.
Related
Here Is what i need to do :
First , I have Two sheets ("AM Production","PM Production") need to Find String "Pcs" In the each sheet and count the results then Excute macro multiple times depending on that count in both sheets (Every sheet with its own count) So i did the following : - I have Two Macros one counts pcs word in the sheet and the other excute the Second macro with that number.
Sub FindPcs()
Range("N1").Select
'Find
Cells.Find(What:="Pcs", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'Found Nothing
'Replace
ActiveCell.Replace What:="Pcs", Replacement:="Done", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Copy To Above Cell
ActiveCell.Range("A1:B1").Select
Selection.Copy
ActiveCell.Offset(-1, 0).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
End Sub
The Action Macro :
Sub FindMultipleTimes()
Dim x As Integer
x = "=COUNTIF(C[10],""Pcs"")"
For i = 0 To x
Application.Run "PERSONAL.XLSB!FindPcs"
Next i
End Sub
I need to merge the two macros As The main idea is to find pcs in the "AM Production" sheet then execute Sub FindMultipleTimes() in the end when it find nothing it goes to "PM Production" and Repeat the Counting and Executing part .
Note :I tried the Range and If Nothing Method with find but it throws another error object required.
Thanks in Advance.
No need to call the macro multiple times, use a Do .. Loop Until loop.
Option Explicit
Sub FindMultipleTimes()
Dim sht
For Each sht In Array("AM Production", "PM Production")
FindPcs Sheets(sht)
Next
End Sub
Sub FindPcs(ws As Worksheet)
Dim fnd As Range, n As Long
Application.ScreenUpdating = False
With ws
Set fnd = .Cells.Find(What:="Pcs", After:=.Range("N1"), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If Not fnd Is Nothing Then
Do
fnd.Replace What:="Pcs", Replacement:="Done", LookAt:=xlPart, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'Copy To Above Cell
fnd.Resize(1, 2).Copy fnd.Offset(-1)
fnd.EntireRow.Delete
n = n + 1
Set fnd = .Cells.FindNext
Loop Until fnd Is Nothing
End If
End With
Application.ScreenUpdating = True
MsgBox n & " found on " & ws.Name
End Sub
first post :)
I have the following code below, which selects all within a set column and clears the two text phrases, from row 12 down.
What i want is to have the user input the column instead? possibly via InputBox?
Sub ClearColumn()
Dim lastCell As Long
Dim myRange As Range
' Find lastCell in column Z
lastCell = Cells(Rows.Count, "Z").End(xlUp).Row
' Set range to look at
Set myRange = Range("Z12:Z" & lastCell)
' Replace All Pass
myRange.Replace What:="Go", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
' Replace All Fail
myRange.Replace What:="Stop", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Welcome to Stack Exchange. You seem to have answered your own question. Below code is untested, but should take a small amount of time to implement.
Sub ClearColumn()
Dim lastCell As Long
Dim chooseColumn As Variant
Dim myRange As Range
chooseColumn = InputBox("Which Column do you want to alter?")
' Find lastCell in column Z
lastCell = Cells(Rows.Count, chooseColumn ).End(xlUp).Row
' Set range to look at
Set myRange = Range(chooseColumn &"12:"&chooseColumn & lastCell)
' Replace All Pass
myRange.Replace What:="Go", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
' Replace All Fail
myRange.Replace What:="Stop", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Let us know how you go :)
i'm new here and to VBA and I need your assistance.
I have an excel spreadsheet with a dynamic table (Header is on Row 4). The data imported into the table contains a date values (5 September, 2018 6:11:17 PM EDT) that Excel cannot format to m-d-yyyy. the only way we can format the dates are to remove the 'comma', 'EDT' and 'EST' values. The macro runs and works as expected.
Now my challenge is to modify this macro (VBA) to look for the column header name instead of the whole column. As I keep getting asked to add a column to the table. The column names are 'Target Decomm Date', 'Actual Decomm Date', 'Created Date', 'Last Updated Date', 'Accreditation Date', and 'Accreditation Expiry Date'
All data is populated in row 5 and this is a dynamic table.
Here is my current code
Sub ConvertDateFormat()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+D
'
Range("V:V,W:W,Z:Z,AA:AA,AC:AC,AD:AD").Select
Range("V5").Activate
Selection.NumberFormat = "m/d/yyyy"
Selection.Replace What:=",", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="EDT", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="EST", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
You can reference to the table and it's columns directly without using column letters.
Dim tbl As ListObject 'this will be the table
Dim rngData as Range
Set tbl = ActiveSheet.ListObjects("name_of_your_table")
Set rngData = tbl.ListColumns("Target Decomm Date").DataBodyRange
rngData.NumberFormat = "m/d/yyyy"
rngData.Replace [.....]
You can use an array to loop through all your different column names, but I won't cover that here, as it's well documented on the net, for example How can I use a for each loop on an array?
here is my end script and it worked well.
Sub FormatDate()
'
Dim tbl As ListObject 'this will be the table
Dim rng1, rng2, rng3, rng4, rng5, rng6, rngM As Range
Set tbl = ActiveSheet.ListObjects("tblProductList")
Set rng1 = tbl.ListColumns("Target Decomm Date").DataBodyRange
Set rng2 = tbl.ListColumns("Actual Decomm Date").DataBodyRange
Set rng3 = tbl.ListColumns("Created Date").DataBodyRange
Set rng4 = tbl.ListColumns("Last Updated Date").DataBodyRange
Set rng5 = tbl.ListColumns("Accreditation Date").DataBodyRange
Set rng6 = tbl.ListColumns("Accreditation expiry Date").DataBodyRange
Set rngM = Union(rng1, rng2, rng3, rng4, rng5, rng6)
rngM.NumberFormat = "m/d/yyyy"
rngM.Replace What:=",", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
rngM.Replace What:="EDT", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
rngM.Replace What:="EST", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
i want to pop up a inputbox when LR is coming..
Look in code:
LR = Range("G" & Rows.Count).End(xlUp).Row
Range("G2:G" & LR).Select
Sub FixIt()
Dim LR As Long
LR = Range("G" & Rows.Count).End(xlUp).Row
Range("G2:G" & LR).Select
Selection.Replace What:=Chr(160), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.NumberFormat = "0.00"
Selection.Style = "Comma"
End Sub
You can use the Application.InputBox with Type:=8 to specify an input box which will have as its return value a Range object.
Here is an example of using such an input box to get the row number of a selected cell.
Dim inRange as Range
Set inRange = Application.InputBox("Please select a cell...", Type:=8)
If Not inRange Is Nothing Then
LR = inRange.Row
Else
'Probably you want to Exit Sub here or do some error-handling
End If
Or, to use the same approach to get the entire range of selection:
Dim myRange as Range
Set myRange = Application.InputBox("Please select some range...", Type:=8)
If myRange Is Nothing Then
'Probably you want to Exit Sub here or do some error-handling
End If
'proceed with the rest of your code...
With myRange
.Replace What:=Chr(160), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
.NumberFormat = "0.00" Selection.Style = "Comma"
End With
I'm working with hourly weather data in Excel that has each hour of every day of the year along with the corresponding temperature value that was recorded.
Some of the values weren't recorded, and instead show up as just an "M" on the spreadsheet. For example, A32 = 28, A33 = M, A34 = 30. I want to replace that "M" with a formula to take the average of the previous and next values. I know how to do this manually, but I am having difficulty writing a Macro to find all the M's in the spreadsheet, then auto-replace it as stated above.
My main obstacle is getting excel to use the correct values when replacing the "M".
Here is my code
Sub MReplace()
'
' MReplace Macro
'
'
ActiveCell.Select
Cells.Find(What:="M", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
Cells.FindNext(After:=ActiveCell).Activate
ActiveCell.Offset(-8, 1).Range("A1").Select
Cells.Find(What:="M", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True _
, SearchFormat:=False).Activate
Cells.FindNext(After:=ActiveCell).Activate
ActiveCell.Replace What:="M", Replacement:="[****This is what I am having difficulty with****]", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Find(What:="M", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True _
, SearchFormat:=False).Activate
End Sub
I have heard of something that you can put in to the code that can address the selected cell. I think it's cell() but I am not sure. Maybe that is a way to get it to work better?
Try this code:
Sub MReplace()
Dim ws As Worksheet
Dim cel As Range
Dim firstAddress As String
Set ws = ActiveSheet
Set cel = ws.Range("A:A").Find("M")
If Not cel Is Nothing Then
firstAddress = cel.Address
Do
cel.Value = (cel.Offset(1) + cel.Offset( -1)) / 2
Set cel = ws.Range("A:A").FindNext(cel)
hr = False
If Not cel Is Nothing Then
If cel.Address <> firstAddress Then
hr = True
End If
End If
Loop While hr
End If
End Sub
It loops through all the cells containing "M" and replaces it with the average of the one on the right and the one on the left. It will error on any that are in the first column as there is no column to the left.