I can't see where I've gone wrong here, any help is appreciated.
I'm trying to cut and paste any rows that have the word 'solved' in them to another spreadsheet, but the code is getting stuck on cl.activate at the start of the loop.
Sub FindString()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
'Open first item to search and paste destination
Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\Markerstudy.xlsx"
Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\solved results.xlsx"
Workbooks("markerstudy").Activate
' Set Search value
SearchString = "solved"
Application.FindFormat.Clear
' loop through all sheets
For Each sh In ActiveWorkbook.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
' if found, remember location
FirstFound = cl.Address
' format found cell
Do
cl.Activate
ActiveCell.EntireRow.Cut
Workbooks("solved results").Activate
Range("A1").Select
If ActiveCell <> "" Then
ActiveCell.Offset(1, 0).Select
ActiveCell.PasteSpecial xlPasteAll
Else
ActiveCell.PasteSpecial xlPasteAll
End If
' find next instance
Set cl = sh.Cells.FindNext(After:=cl)
' repeat until back where we started
Loop Until FirstFound = cl.Address
End If
Next
End Sub
You need to active the sheet first, with cl.Parent.Activate
Sub FindString()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
'Open first item to search and paste destination
Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\Markerstudy.xlsx"
Workbooks.Open "G:\BS\Josh Whitfield\Credit_Chasing\NEW PROCESS\solved results.xlsx"
Workbooks("markerstudy").Activate
' Set Search value
SearchString = "solved"
Application.FindFormat.Clear
' loop through all sheets
For Each sh In ActiveWorkbook.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
' if found, remember location
FirstFound = cl.Address
' format found cell
Do
cl.Parent.Activate
cl.Activate
ActiveCell.EntireRow.Cut
Workbooks("solved results").Activate
Range("A1").Select
If ActiveCell <> "" Then
ActiveCell.Offset(1, 0).Select
ActiveCell.PasteSpecial xlPasteAll
Else
ActiveCell.PasteSpecial xlPasteAll
End If
' find next instance
Set cl = sh.Cells.FindNext(After:=cl)
' repeat until back where we started
Loop Until FirstFound = cl.Address
End If
Next
End Sub
Related
Private Sub Updateform()
Dim WB As Workbook
Dim URL As Variant
Dim Sh As Worksheet
Dim WB1 As Workbook
Dim i As Integer
Dim LR As Long
Dim stext As String
Dim stext1 As String
Application.DisplayAlerts = False
ThisWorkbook.Activate
Set WB = ThisWorkbook
Application.DisplayAlerts = False
ThisWorkbook.Activate
Set Sh = ThisWorkbook.Sheets("Database")
irow = ThisWorkbook.Sheets("Database").[Counta(Database!A:A)] + 1
With Sh
ThisWorkbook.Sheets("Emailtosend").Range("A1999").Value = FirstForm2.UD1.Value
stext = ThisWorkbook.Sheets("Emailtosend").Range("A1999").Value
End With
ThisWorkbook.Sheets("Database").Select
Range("A1").Select
On Error Resume Next
ActiveCell.Select
Dim UID As String
UID = stext
For i = 2 To irow
If ThisWorkbook.Worksheets("Database").Cells(i, 10).Value = UID Then
Worksheets("Database").Cells(i, 3).Value = FirstForm2.lstprocessingdate.Value 'iam able to update date
Worksheets("Database").Cells(i, 4).Value = FirstForm2.lstprocessed1.Value
Worksheets("Database").Cells(i, 8).Value = FirstForm2.survey1.Value ' this is a combobox though I change new value it is still not getting updated
Worksheets("Database").Cells(i, 6).Value= FirstForm2.lstcomments.Value ' this is comment box still the new comments are not getting updated
End If
Next
Dim ncell As Range
For Each ncell In Sheets("temp").Range("Checkrange")
With Sh
If FirstForm2.Controls(ncell.Value) = "" Then
MsgBox ("Make sure all text boxes have entries")
Exit Sub
Else
End If
End With
Next ncell
URL = "https://audit.global.com/sites/AdminSS/Shared%20Documents/Training%20Materials/SS%20recurring%20request%20Handbooks/Test/Updated%20Quality%20Tracker.xlsx?d=w68cd37bd0505426fb4d6fe38c21e23a8"
Set WB1 = Workbooks.Open(URL)
Application.Visible = False
Debug.Print WB1.FullName
Set WB1 = ActiveWorkbook
WB1.LockServerFile
If Err.Number <> 0 Then
MsgBox "File is already open, request you to wait for 10 minutes!"
GoTo 0
Err.Clear
Else
MsgBox "The form is getting updated"
End If
Dim rng1 As Variant
WB1.Activate
Range("J1").EntireColumn.Select
Selection.Copy
Range("K1").EntireColumn.Select
Selection.PasteSpecial xlPasteValues
Dim stext2 As String
stext2 = ThisWorkbook.Sheets("Emailtosend").Range("A1999").Value
WB.Activate
WB.Sheets("Database").Range("A1").Select
'If WB1.Worksheets("Database1").Cells(i, 10).Value = UID Then
Set rng1 = Cells.Find(What:=stext2, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
ActiveCell.Select
ActiveCell.entirerow.Select
Selection.Copy ' copying the entirerow and I want to paste this data in sheet from sharepoint
WB1.Activate
WB1.Sheets("Database1").Select
Set rng1 = Cells.Find(What:=stext2, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
ActiveCell.Select
ActiveCell.entirerow.Select
Selection.PasteSpecial xlPasteValues ' I want to paste here after searching the text value
WB1.Save
WB1.Close ' to close sharepoint excel
msgvalue = MsgBox("The information has been updated", vbOKOnly)
0:
Application.Visible = False
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.Visible = False
End Sub
I want to update existing macro sheet by calling existing details from userform unique ID and then update new details entered in the existing sheet and other tracker in Sharepoint. Please help
Let me know if you have any questions. Not sure what else to add to this question. Initially the code has to modify the existing details and update the new details and then open Sharepoint Excel and check the unique code, either delete the entire row of the unique code or update the new details by overwriting the existing details in Sharepoint Excel
I would like help finding out the appropriate code to get AutoFill for my formula in column K to work from one cell under the one that is found, all the way to the last row of the document. How can this be achieved?
Thank you!
Dim s As String
Dim rCell As Range
Dim lReply As Long
Dim firstaddress As String
Dim rngOriginal As Range
Dim Cell As Range
Columns("K:K").Select
Set Cell = Selection.Find(What:="Add", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Cell Is Nothing Then
firstaddress = Cell.Address
Cell.Offset(0, -6).Insert shift:=xlDown
Cell.Offset(0, -7).Insert shift:=xlDown
Cell.Offset(0, -8).Insert shift:=xlDown
Cell.Offset(0, -9).Insert shift:=xlDown
Cell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = _
"Add. "
Range("K9").AutoFill Destination:=Range("K9:K1936"), Type:=xlFillDefault
Cell.Select
ActiveCell.FormulaR1C1 = _
""
No need to use Autofill :) You can input formula in one go!
Like this (UNTESTED)?
Dim ws As Worksheet
Set ws = Sheet1 '<~~ Change as applicable
'
' ~~> Rest of your code
'
With ws '<~~ This is your worksheet object
LRow = .Range("K" & .Rows.Count).End(xlUp).Row
.Range("K9:K" & LRow).Formula = .Range("K9").Formula
End With
'
' ~~> Rest of your code
'
Think you could have found this out yourself as it's a common VBA question. I've also removed the Selects from your code which are generally unnecessary and inefficient.
Sub x()
Dim s As String
Dim rCell As Range
Dim lReply As Long
Dim firstaddress As String
Dim rngOriginal As Range
Dim Cell As Range
Dim n As Long
Set Cell = Columns("K:K").Find(What:="Add", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Cell Is Nothing Then
firstaddress = Cell.Address
Cell.Offset(0, -9).Resize(, 4).Insert shift:=xlDown
Cell.Offset(0, 1).Value = "Add. "
n = Range("K" & Rows.Count).End(xlUp).Row
Range("K9").AutoFill Destination:=Range("K9:K" & n), Type:=xlFillDefault
Cell.Value = 1
End If
End Sub
I have a worksheet which contains the details of the each product.
Here i have crested a button (ADD), by clicking on it i want to copy all the details of the CONTROL POWER TRANSFORMERS block and copy it to below (i mean copy it from B20).
I have written a code to pinpoint the CTPT (which is the unique id for that product) keeping it as a reference i have copied whole block till the row ends using the below code.
Set cF = .Find(what:="CTPT", _
lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(-1, 3), cF.Offset(-1, 1).End(xlDown)).Copy
Now while pasting the cell i need to do couple of things
I need to insert an row by finding the cell address of the clicked button
Paste the copied Data
Code any one help me out in achieving these couple of task.
Any help is Appreciated!
Sub test_Karthik()
Dim WbEPC As Workbook, _
WbCPT As Workbook, _
WsEPC As Worksheet, _
WsCPT As Worksheet, _
FirstAddress As String, _
WriteRow As Long, _
cF As Range, _
num As String
Set WbEPC = Workbooks("EPC 1.xlsx")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet2")
With WsEPC
.Activate
With .Range("A1:A10000")
'First, define properly the Find method
Set cF = .Find(What:="CTPT", _
After:=ActiveCell, _
LookIn:=xlValues, _
Lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If there is a result, keep looking with FindNext method
If Not cF Is Nothing Then
FirstAddress = cF.Address
Do
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(0, 1).End(xlUp), cF.Offset(0, 3).End(xlDown)).Copy
WriteRow = WsCPT.Range("E" & WsCPT.Rows.Count).End(xlUp).Row + 1
WsCPT.Range("E" & WriteRow).PasteSpecial (xlPasteValues)
cF.EntireRow.Insert xlDown, False
Set cF = .FindNext(cF)
'Look until you find again the first result
Loop While Not cF Is Nothing And cF.Address <> FirstAddress
End If
End With
End With
End Sub
Private Sub CommandButton21_Click()
Dim WbEPC As Workbook, _
WbCPT As Workbook, _
WsEPC As Worksheet, _
WsCPT As Worksheet, _
FirstAddress As String, _
WriteRow As Long, _
cF As Range, _
num As String
Set WbEPC = Workbooks("EPC 1.xlsm")
Set WbCPT = Workbooks("Control Power Transformers.xlsm")
Set WsEPC = WbEPC.Sheets("Sheet1")
Set WsCPT = WbCPT.Sheets("Sheet1")
Dim b As Object, RowNumber As Integer
Set b = ActiveSheet.Shapes("CommandButton21")
With b.TopLeftCell
RowNumber = .Row
End With
Rows(RowNumber + 1 & ":" & RowNumber + 1).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
With WsEPC
.Activate
With .Range("A1:A10000")
Set cF = .Find(what:="CTPT", _
lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
num = cF.Address ' Here we will the get the cell address of CTPT ($A$14)
WsEPC.Range(cF.Offset(-1, 3), cF.Offset(-1, 1).End(xlDown)).Copy
WsEPC.Range("B" & RowNumber + 1).Select
Selection.Insert Shift:=xlDown
Application.CutCopyMode = False
End With
End With
MsgBox " Successfully added the product to EPC"
End Sub
I'm trying to search Column A in Sheet2 for the value of A1 in Sheet1.
If it exists, I'd like to delete the whole row in Sheet2.
If it doesn't exist, I'd like the message box to open.
Here's what I have, but I'm struggling with actually deleting the row:
Sub Delete_Rows()
Dim FindString As String
Dim Rng As Range
FindString = Sheets("Sheet1").Range("A1")
If Trim(FindString) <> "" Then
With Sheets("Sheet2").Range("A:A")
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
'I can't figure out how to delete the row
Else
MsgBox "Not Found"
End If
End With
End If
End Sub
Here is an example based on THIS
You don't need to loop. You can use .Autofilter which is faster than looping.
Sub Sample()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim delRange As Range
Dim lRow As Long
Dim strSearch As String
Set ws1 = Sheet1: Set ws2 = Sheet2
strSearch = ws1.Range("A1").Value
With ws2
'~~> Remove any filters
.AutoFilterMode = False
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:A" & lRow)
.AutoFilter Field:=1, Criteria1:="=" & strSearch
Set delRange = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
If delRange Is Nothing Then
MsgBox "Not Found"
Else
delRange.Delete
End If
End Sub
Here is the code to :
loop through all the values in Column A of Sheet1,
look for all matches (with FindNext method) in Column A of Sheet 2
and delete the rows that matches
Give it a try :
Sub test_user5472539()
Dim Ws1 As Worksheet, _
Ws2 As Worksheet, _
LastRow As Long, _
FindString As String, _
FirstAddress As String, _
cF As Range
Set Ws1 = ActiveWorkbook.Sheets("Sheet1")
Set Ws2 = ActiveWorkbook.Sheets("Sheet2")
LastRow = Ws1.Range("A" & Ws1.Rows.Count).End(xlUp).Row
For i = 1 To LastRow
FindString = Ws1.Range("A" & i)
If Trim(FindString) <> "" Then
Ws2.Range("A1").Activate
With Ws2.Range("A:A")
'First, define properly the Find method
Set cF = .Find(What:=FindString, _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
'If there is a result, keep looking with FindNext method
If Not cF Is Nothing Then
FirstAddress = cF.Address
Do
cF.EntireRow.Delete
Set cF = .FindNext(cF)
'Look until you find again the first result
Loop While Not cF Is Nothing And cF.Address <> FirstAddress
Else
MsgBox "Not Found"
End If
End With
Else
End If
Next i
End Sub
I want a to make some code that searches all the worksheets for the string "Question" then take "5" lines below it. Then take those 5 lines and put them in the worksheet "Template" from lines "B2".
Here is my current code:
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "Question"
Application.FindFormat.Clear
' loop through all sheets
For Each sh In ActiveWorkbook.Worksheets
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
' if found, remember location
FirstFound = cl.Address
' format found cell
Do
cl.Font.Bold = True
cl.Interior.ColorIndex = 3
' find next instance
Set cl = sh.Cells.FindNext(After:=cl)
Loop Until FirstFound = cl.Address
End If
Next
All this code does is find the string. How do I take the data below the string and copy them to "Template" worksheet?
You will want to invest in the .Offset Method:
Dim RangeToCopy As Range, DestRow As Long
Set RangeToCopy = sh.Range(cl.Offset(1, 0), cl.Offset(5, 0))
RangeToCopy.Copy
DestRow = Sheets("Template").Range("B" & Rows.Count).End(xlUp).Row + 1
Sheets("Template").Range("B" & DestRow).PasteSpecial xlPasteValues