VBA Error 13 With Find code - vba

I am having an issue with type mismatch, in my table the value is general since it is copied and pasted values from a pivot table
Error thrown here:
Set mf = Columns("F").Find(What:=ONE, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
Dim ONE As String
Worksheets("Chart").Activate
Columns("A:B").Select
Selection.Copy
Range("F1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("F2").Select
Application.CutCopyMode = False
Selection.Copy
ONE = Cells(2, "F").Value
Sheets("Paste Data Table").Select
Set mf = Columns("F").Find(What:=ONE, After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
ActiveCell.EntireRow.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Top 5 Breakdown").Select
Sheets("Top 5 Breakdown").Select
Range("A2").Select
ActiveSheet.Paste
Worksheets("Paste Data Table").Activate
Range("A2").Select
Application.CutCopyMode = False

What causes error in this Find method is:
After:=ActiveCell
You can't search after cell which is not in searched range, column F in this case. Your active cell is not in column F.

Related

Need to copy one worksheet to another based on header info that includes blank cells

I'm trying to copy specific columns from one worksheet to another worksheet to make it uniform so I can easily sort and slice the data in other worksheets. I'm having trouble with some columns copying the entire column including blanks. I am searching the header for specific phrases, selecting the entire column (except the header), and copy/pasting to the other worksheet. The problem arises when I get to a column that has blanks - the xlDown feature stops at the blank cell, but if I use xlCellTypeLastCell it selects all of the columns to the right of the column that I want to copy, so I end up overwriting other cells in my other worksheet. Here is a sample of the code I'm using:
' Copy Potential Name
Cells.Find(What:="Potential* Name", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection.Offset(1, 0), Cells.SpecialCells(xlCellTypeLastCell)).Select
Selection.Copy
Sheets("Formatted Sheet").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet5").Select
Application.CutCopyMode = False
If I try using a LastRow function; e.g.
LastRow = Sheets("Sheet5").UsedRange.Rows.Count
I can't get it to select the column - it returns an error when I use
Range(Selection.Offset(1, 0), LastRow).Select
Please help!
Thanks in advance
Safer to use End(xlUp) from the bottom of the sheet:
Dim f As Range, rng As Range
Set f = Cells.Find(What:="Potential* Name", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
With f.Parent
Set rng = .Range(f.Offset(1, 0), .Cells(.Rows.Count, f.Column).End(xlUp))
End With
rng.Copy
Sheets("Formatted Sheet").Range("B2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Else
MsgBox "header not found!"
End If
Sheets("Sheet5").Select
Application.CutCopyMode = False

Excel VB, find and move row to bottom of list separate with new heading

I am able to get the result that I want to get with my code which is as follows:
Sub Button1_Click()
With Worksheets("Data").Select
With Range("A11:H11").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.PatternTintAndShade = 0
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
With Range("E11").Select
ActiveCell.FormulaR1C1 = "Seasonal Items"
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End With
End With
End With
End With
End With
End With
With Worksheets("Data").Select
With Range("B2").Select
Cells.Find(What:="fan", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
With Selection
ActiveCell.EntireRow.Select
With Selection
Selection.Copy
Rows("12:12").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
With Range("B2").Select
Cells.Find(What:="fan", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlAll, MatchCase:=False, SearchFormat:=False).Activate
With Selection
ActiveCell.EntireRow.Select
With Selection
Selection.Delete Shift:=xlUp
End With
End With
End With
End With
End With
End With
End With
With Worksheets("Data").Select
With Range("B2").Select
Cells.Find(What:="fan", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
With Selection
ActiveCell.EntireRow.Select
With Selection
Selection.Copy
Rows("12:12").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
With Range("B2").Select
Cells.Find(What:="fan", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlAll, MatchCase:=False, SearchFormat:=False).Activate
With Selection
ActiveCell.EntireRow.Select
With Selection
Selection.Delete Shift:=xlUp
End With
End With
End With
End With
End With
End With
End With
With Worksheets("Data").Select
With Range("B2").Select
Cells.Find(What:="fan", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
With Selection
ActiveCell.EntireRow.Select
With Selection
Selection.Copy
Rows("12:12").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
With Range("B2").Select
Cells.Find(What:="fan", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlAll, MatchCase:=False, SearchFormat:=False).Activate
With Selection
ActiveCell.EntireRow.Select
With Selection
Selection.Delete Shift:=xlUp
End With
End With
End With
End With
End With
End With
End With
With Worksheets("Data").Select
With Range("B2").Select
Cells.Find(What:="Heater", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
With Selection
ActiveCell.EntireRow.Select
With Selection
Selection.Copy
Rows("12:12").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
With Range("B2").Select
Cells.Find(What:="Heater", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlAll, MatchCase:=False, SearchFormat:=False).Activate
With Selection
ActiveCell.EntireRow.Select
With Selection
Selection.Delete Shift:=xlUp
End With
End With
End With
End With
End With
End With
End With
End Sub
This code is not very elegant nor is it flowing really.
What I would like it to do is automatically search for specific wording in the column B which is either Fan or Heater, then move it to the bottom, where it is separated with a row that states season items.
See the picture below of the result:
Why I want it different is due to that the stuff is flowing and changing at points... It would make it simpler and I also would like the code to be much shorter and not for me to each time physically having to check and edit the code before running it...
Thank you for taking the time to view this and if possible provide a solution.
Something like this will move the rows the way you want them, but you will need to add in the specific formatting yourself.
Sub test()
Dim lRow As Integer
Dim lrow2 As Integer
Dim i As Integer
lRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row
ActiveSheet.Cells(lRow + 1, 5).Value = "Seasonal Items"
With ThisWorkbook.ActiveSheet
For i = 2 To lRow
lrow2 = ActiveSheet.Cells(ActiveSheet.Rows.Count, 5).End(xlUp).Row + 1
If InStr(.Cells(i, 2), "Fan") > 0 Or InStr(.Cells(i, 2), "Heater") > 0 Then
.Rows(lrow2 & ":" & lrow2).Value = .Rows(i & ":" & i).Value
.Rows(i & ":" & i).ClearContents
End If
Next i
For i = 2 To lrow2
If .Cells(i, 1).Value = "" Then
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With
End Sub

Excel VBA - how to account for errors from searching

Sheets("Table").Select
Cells.Find(What:="Cat", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Offset(1, 1).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(rowoffset:=0, columnoffset:=-1).Select
Selection.Resize(Selection.Rows.Count + 0, Selection.Columns.Count + 10).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Table").Select
Cells.Find(What:="Bat", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Offset(1, 1).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(rowoffset:=0, columnoffset:=-1).Select
Selection.Resize(Selection.Rows.Count + 0, Selection.Columns.Count + 10).Select
Selection.Copy
Sheets("Sheet1").Select
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I'm very new to excel and trying to account for errors that will occur when one of the words I'm searching for are not on the table. I'm not sure how to format it to work, but basically for the first search, if it errors then go to the next search without doing any of selection, copy, and paste part (same for the second search).
Create a Range variable to assign your find function to, then use an If statement to determine if it exists or not. If no, move to the next one.
Dim fRange As Range
Sheets("Table").Select
Set fRange = Cells.Find(What:="Cat", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Offset(1, 1)
If Not fRange Is Nothing Then
fRange.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(rowoffset:=0, columnoffset:=-1).Select
Selection.Resize(Selection.Rows.Count + 0, Selection.Columns.Count + 10).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Sheets("Table").Select
Set fRange = Cells.Find(What:="Bat", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Offset(1, 1)
If Not fRange Is Nothing Then
fRange.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(rowoffset:=0, columnoffset:=-1).Select
Selection.Resize(Selection.Rows.Count + 0, Selection.Columns.Count + 10).Select
Selection.Copy
Sheets("Sheet1").Select
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If

Copying data to another sheet

I am having trouble understanding what the below function is doing.
The function itself has the ability to copy data to the sheet Sheet History. However, I do not get how it is doing it?
Sub histFunc()
Dim Y As String
Y = "R" & Range("G7").Value
Sheets("Sheet History").Select
Range("h17").Select
Cells.Find(What:=Y, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlToRight)).Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Sheets("Sheet Current").Select
End Sub
Any suggestions how this macro is operating?
I appreciate your replies!
In short, the code searches for the value in G7 in Sheet History and replaces the right part of that line with values only, i.e. removing references or values.
Step-by-step Explanation
Get the value of cell G7:
Y = "R" & Range("G7").Value
Select sheet Sheet History and select cell H17:
Sheets("Sheet History").Select
Range("h17").Select
Executes the Find method over Cells, all cells in the sheet (note that if no parameter is given it is the range of all Cells in the current Sheet):
Cells.Find(What:=Y, After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
This returns:
A Range object that represents the first cell where that information is found.
For more info see the Find documentation.
Now due to .Activate the (first) cell is selected where the value was found. This selection is extended to the end of the line:
Range(Selection, Selection.End(xlToRight)).Select
Then the CutCopyMode is deactivated to clear the clipboard after usage:
Application.CutCopyMode = False
Now the selected cells are copied and pasted:
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Note that they are pasted on with PasteSpecial on the same location, using xlPasteValues to only maintain the values, and therefore not to have any formulas nor references in the cells.
Now go to Sheet Current:
Sheets("Sheet Current").Select
After a bit of cleaning, this is what this could look like (explanations below) :
Sub histFunc()
Dim FindRange As Range, _
LookForValue As String
LookForValue = "R" & Range("G7").Value
With Sheets("Sheet History")
.Range("h17").Activate
Set FindRange = .Cells.Find(What:=LookForValue, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
Application.CutCopyMode = False
Range(FindRange, FindRange.End(xlToRight)).Copy
FindRange.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
End With
Application.CutCopyMode = False
Sheets("Sheet Current").Select
End Sub
I changed Y to LookForValue for better understanding and used a Range variable to reference the result of the Find method.
So this code, step by step :
Define LookForValue as "R" & Range("G7").Value
Search for that value in the formulas of Sheet History
Copy the data block (in the row of the result, from result to right, until there is a blank)
Paste it at the same place but in values, so that you get rid of the formulas!

Offset compared to Selection.Insert Shift:=xlToRight

I have the following Macro and want to reduce the number of lines to speed up the process.
5
ActiveCell.Columns("A:A").EntireColumn.Select
If Selection.Find(What:="*", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True) Is Nothing Then
GoTo 6
End If
Selection.Find(What:="*", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True).Activate
ActiveCell.Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
GoTo 5
I want to replace multiple the 'Selection.Insert Shift:=xlToRight' lines with a single line using Offset.
Can you please help!
Replace your x5 Selection.Insert Shift:=xlToRight by Range(ActiveCell, ActiveCell.Offset(0, 4)).Insert shift:=xlToRight
EDIT
More related to your code: Range(Selection, Selection.Offset(0, 4)).Insert shift:=xlToRight