Trouble Pasting Entire Row - excel

I'm trying to paste the enire row of information to the next available row but I keep getting errors about not having the Rows(lastrow +1, 1).EntireRow.Paste written correctly. Please let me know how I can perform that action correctly.
Private Sub CommandButton1_Click()
Dim myValue As String
myEmp = InputBox("Search for an employee by last name")
Range("B3").Value = myEmp
With Sheet7
Range("B:B").Select
Set Row = Selection.Find(What:=myEmp, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
Row.EntireRow.Copy
End With
Worksheets("Employee Reports").Activate
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Rows(lastrow + 1, 1).EntireRow.Paste
End Sub
Private Sub Workbook_Open()
Application.EnableEvents = False
Worksheets("Sheet3").Range("A4:A20").Value = ""
End Sub

Private Sub CommandButton1_Click()
Dim myValue As String
myEmp = InputBox("Search for an employee by last name")
ActiveSheet.Range("B3").Value = myEmp
Dim lastrow As Long
lastrow = Worksheets("Employee Reports").Range("A65536").End(xlUp).Row
With Sheet7
Dim rw As Range
Set rw = .Range("B:B").Find(What:=myEmp, After:=.Range("B1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not rw Is Nothing Then
rw.EntireRow.Copy Worksheets("Employee Reports").Cells(lastrow + 1, 1)
Else
MsgBox myEmp & " Not Found in Range"
End If
End With
End Sub

Related

Object Variable or With block variable not set - Using Find option in VBA

i am new to VBA. Task here is to find the address of a cell with respect to a variable.
i am using the find option and receiving error
Object Variable or With block variable not set
Using Find option in VBA. searching data is already available in the sheet. when I do it manually its working.
Sub updateddata()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim currentdate As Date
Dim empid As String
Dim attendance As String
Dim namerange As Range
Dim columnadd As String
Dim rowadd As Range
Dim pasterange As Range
Worksheets("MARK ATTENDANCE").Select
Range("b6").Select
Set namerange = Range(Selection, ActiveCell.End(xlDown))
Range("G5").Select
currentdate = Left(Range("g5").Value, 10)
attendance = ActiveCell.Offset(1, 0).Value
For Each cell In namerange
empid = cell.Value
If attendance = "" Then
ActiveCell.Offset(1, 0).Select
attendance = ActiveCell.Value
Else
Worksheets("Daily Attendance Tracker").Select
Range("1:1").Select
columnadd = Selection.Find(What:=currentdate, After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Address
Range("B2:B37").Select
rowadd = Selection.Find(What:=Trim(empid), After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Address
End If
Next
End Sub

How can I change my VBA code not to use select?

The code I have, works, but it is slow and I want to avoid using select.
I have tried something in the line of the following:
Sub PopulateBlastEvents()
Dim wsfr As Worksheet
Dim wsl As Worksheet
Dim BlNumber As String
Dim BSStep As Long
Dim SI As String
Dim Srng As Range
Dim Nrng As Range
Dim Arng As Range
Dim NotF As String
Dim Found As Range
Application.ScreenUpdating = False
NotF = "NO INFO"
BSStep = 1
Set Rrng = Sheets("Blast List").Range("A2:A45")
Set Srng = Sheets("Blast List").Range("E1:R1")
For Each cell In Rrng
If cell <> "" Then
For Each cell2 In Srng
If cell2 <> "" Then
On Error Resume Next
SI = cell.Value
BlNumber = CStr("Blasted " & BSStep)
Set wsfr = Sheets(CStr(BlNumber))
Set wsl = Sheets("Blast List")
With wsfr.Range("A:A")
Set Found = Cells.Find(What:=SI, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Found Is Nothing Then
With wsl.Range("A:A")
Set Found1 = Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1)
Found1.Value = NotF
End With
Else
With wsl.Range("A:A")
Set Found1 = Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1)
Found1.Value = Found.Value
End With
End If
End With
End If
Next cell2
BSStep = BSStep + 1
End If
Next cell
Set Arng = ThisWorkbook.Worksheets("Blast List").Range("E3:X3").End(xlDown).Select
Application.ScreenUpdating = True
Columns("A:S").EntireColumn.AutoFit
End Sub
The code does run, but returns no value as the range value "rng" remains at NOTHING even though it is in the sheet where it is looking for the value.
Below is the current code I am using that needs to change:
Sub PopulateBlastEvents()
Dim wsfr As Worksheet
Dim wsl As Worksheet
Dim BlNumber As String
Dim BSStep As Long
Dim SI As String
Dim Srng As Range
Dim Nrng As Range
Dim Rrng As Range
Dim Brng As Range
Dim Arng As Range
Dim NotF As String
Application.ScreenUpdating = False
NotF = "NO INFO"
BSStep = 1
Set Rrng = Sheets("Blast List").Range("A2:A45")
Set Srng = ThisWorkbook.Worksheets("Blast List").Range("E1:R1")
For Each Brng In Rrng.Cells
If Brng <> "" Then
For Each Nrng In Srng.Cells
If Nrng <> "" Then
On Error Resume Next
SI = Nrng.Value
BlNumber = CStr("Blasted " & BSStep)
Set wsfr = ThisWorkbook.Worksheets(CStr(BlNumber))
Set wsl = ThisWorkbook.Worksheets("Blast List")
wsfr.Select
Range("A1").Select
Cells.Find(What:=SI, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Selection.Copy
If Err.Description <> "" Then
Sheets("Blast List").Select
Range("A1").Select
Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1).Select
Selection.Value = NotF
Else
Sheets("Blast List").Select
Range("A1").Select
Cells.Find(What:=BlNumber, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).End(xlToRight).Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
End If
Next Nrng
BSStep = BSStep + 1
End If
Next Brng
Set Arng = ThisWorkbook.Worksheets("Blast List").Range("E3:X3").End(xlDown).Select
Application.ScreenUpdating = True
Columns("A:X").EntireColumn.AutoFit
End Sub
I really want to speed up the code and all previous questions I have posted, I was informed not to or avoid using Select.
Please could someone help.

Excel VBA to repeat founding

Goal:
Search Sheet1 for keywords in column 18 (keywords:case,etc.)
Once keyword is found, offset (0,-11)
enter the given value C1008 in respective cell.
and repeat the same till it completes full column (about 1500 rows)
I am not able to perform 4th step.
And I need multiple keywords to search and perform the same steps.
Program:
Sub RCIM()
Dim ws As Worksheet
Dim aCell As Range
Range("A1").Select
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
Set aCell = .Columns(18).Find(What:="case", LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -10).Value = "C1008"
Else
MsgBox "Not Found"
End If
End With
End Sub
If you want just to repeat your code use a loop statement like this:
Sub RCIM()
Dim ws As Worksheet
Dim aCell As Range
DIM isTimetoExit As Boolean
Dim currentRow As Long 'Counter of Rows
Dim KeyWord as String
Range("A1").Select
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
isTimetoExit = False
currentRow = 1 'If you start from row 1
KeyWord = "case"
DO
'
Set aCell = .Columns(18).Find(What:=KeyWord, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
aCell.Offset(0, -10).Value = "C1008"
Else
MsgBox "Not Found"
End If
'
' Whit a condition go out of loop
isTimetoExit = NOT (Trim(.Cells(currentRow, 1).Value & "") = "")
If (isTimetoExit) THEN EXIT DO
'Go for next row
currentRow = currentRow + 1
' And set the keyword to your new value;
KeyWord = "next case" 'But I don't know where are those KeyWords stroes !?
LOOP
End With
End Sub

.FindNext in a loop doesnt' work

I am using this loop to look for values. .Find works but findNext does not, ommiting many values. Here I drop my code, do you have any advice? thank you very much!!
For Each ws In SourceWb.Worksheets
If IsNumeric(Left(ws.Name, 3)) Then
Set gCell = ws.Columns(6).Find(what:=numdoc, LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Do
repetidos = repetidos + 1
finalcell = gCell.Address
'merged cells code here not displayed
oldaddress = gCell.Address
'>Having trouble here> **
Set gCell = ws.Columns(6).FindNext(after:=gCell)
'**
Loop Until gCell.Address = oldaddress
End If
End If
Next ws
This was the best I could derive from your clues:
Option Explicit
Sub Test()
Dim WS As Worksheet
Dim SourceWB As Workbook
Dim numdoc As Long
Dim gCell As Range
Dim firstAddress As String
Dim oldaddress As String
Dim finalcell As String
Dim repetidos As Long
Set SourceWB = ThisWorkbook 'added for clarity and safety
numdoc = 456
For Each WS In SourceWB.Worksheets
If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A"
Set gCell = WS.Columns(6).Find(what:=numdoc, _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Set gCell = WS.Columns(6).FindNext(after:=gCell)
Do
repetidos = repetidos + 1
finalcell = gCell.Address
'merged cells code here not displayed
oldaddress = gCell.Address
Loop Until gCell.Address = oldaddress
End If
End If
Next WS
End Sub
Not sure if it answers the question but, it does demonstrate indentation.
There's probably room for a With...End With in there but I'm too tired to look for it.
This seems to work at this point.
For Each WS In SourceWB.Worksheets
With ws.Range("F:F")
If IsNumeric(Left(WS.Name, 3)) Then 'OK I had to save it as "123 A"
Set gCell = .Find(what:=numdoc, _
LookIn:=xlValues, _
lookat:=xlWhole, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
searchformat:=False)
If Not gCell Is Nothing And IsNumeric(Left(gCell.Parent.Name, 3)) Then
firstAddress = gCell.Address
Set gCell = .FindNext(after:=gCell)
Do
'merged cells code here not displayed
Loop While Not gCell Is Nothing And gCell.Address <> firstAddress
End If
End If
end with
Next WS

I want to search/find value in textbox2 in an active sheet using command button

I want to search/find value in textbox2 in an active sheet using command button
Here is my code:
Dim ws As Worksheet
Set ws = Worksheets("FSS-TEM-00025")
Dim FindString As String
Dim Rng As Range
FindString = Me.TextBox2.Value
If Trim(FindString) <> "" Then
With ws.Range("A1:Z1048576")
'Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)'
Set Rng = ws.Cells.Find(What:=FindString, SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
Unload Me
Try this. This works for me
Option Explicit
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim FindString As String
Dim Rng As Range
Set ws = ThisWorkbook.Worksheets("FSS-TEM-00025")
FindString = TextBox2.Value
If Trim(FindString) <> "" Then
Set Rng = ws.Cells.Find( _
What:=FindString, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End If
End Sub
Change LookAt:=xlPart to LookAt:=xlWhole if you are trying to find a complete match.
More on .Find here.

Resources