I have a question related to the VBA.
Problem
I have a code to do simple task but i don't what's the reason but sometimes this code works perfectly some time it's not.
Code Explanation
Go to active sheets(un-hidden) sheets in the work book.
Search specific text in the assign column, in this case text is "Sum of Current Activity".
Copy the cell before the text.
Go to Reviewer sheet and find sheet name in the table.
Paste the copied cell as link value next to cell where we have sheet name in the table.
Continue the same process until all active sheets searched
CODE
Sub Sum of_Current_activity()
Dim sht As Worksheet
Sheets("Reviewer Sheet").Select
For Each sht In ActiveWorkbook.Worksheets
If sht.Name <> "Reviewer Sheet" And Left(sht.Name, 1) = 0 Then
On Error Resume Next
sht.Select
f2 = " Total"
£1 = ActiveSheet.Name & f2
Sheets(sht).Select
Columns("J:J").Select
Selection.Find(What:="Sum of Current Activity", _
After:=ActiveCell,_
LookIn:=xlValues,_
LookAt:=xlPart,_
SearchOrder:=xlByRows,_
SearchDirection:=x1Next,_
MatchCase:=False).Activate
ActiveCell.Offset(0, 1).Select
Selection.Copy
Sheets("Reviewer Sheet").Select
Columns("C:C").Select
Selection.Find(What:=f1, _
After:=ActiveCell,_
LookIn:=xlValues,_
LookAt:=xlPart,_
SearchOrder:=xlByRows,_
SearchDirection:=xlNext,_
MatchCase:=False).Activate
ActiveCell.Offset(0, 14).Select
ActiveSheet. Paste Link:=True
Else
End If
Next sht
End Sub
P.S, I have 10 different specific text to search in the 25 sheet. this code sometime works for all 10 texts and sometimes miss the values.
Untested but something like this should work:
Sub Sum of_Current_activity()
Dim sht As Worksheet, c1 As Range, c2 As range
For Each sht In ActiveWorkbook.Worksheets
If sht.Name Like "0*" Then
Set c1 = sht.Columns("J:J").Find(What:="Sum of Current Activity", _
LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
Set c2 = Sheets("Reviewer Sheet").Columns("C:C").Find( _
What:= sht.Name & " Total", _
LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
If not c1 is nothing and not c2 is nothing then
'edit: create link instead of copy value
c2.offset(0, 14).Formula = _
"='" & c1.parent.Name & "'!" & c1.offset(0,1).Address(true, true)
End if
End If
Next sht
End Sub
just because the task is simple, you could use On Error Resume Next statement and make a direct Value paste between ranges:
Sub main()
Dim sht As Worksheet
On Error Resume Next ' prevent any subsequent 'Find()' method failure fro stopping the code
For Each sht In Worksheets
If Left(sht.Name, 1) = "0" Then _
Sheets("Reviewer Sheet").Columns("C:C").Find( _
What:=sht.Name & " Total", _
LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Offset(0, 14).Value = sht.Columns("J:J").Find(What:="Sum of Current Activity", _
LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Offset(0, 1).Value
Next
End Sub
I once more stress that On Error Resume Next is here used only because it's a case where you can have a full control of its side effects that can arise from ignoring errors and go on
should you use this snippet in a bigger code, than close the snippet with On Error GoTo 0 statement and resume default error handling before going on with some other code.
Related
I have a working code to find a specific string in a column of a specific sheet, offset and clear the contents of a specific cell. However it only clears the first occurrence of this search and I would like to have the code work on all occurrences. Can someone help me to wrap a Loop or a FindNext around this code because I wasn't able to. Please see here below the code I already have. Thnx
Dim SearchValue6 As String 'located B9
Dim Action6 As Range 'clear
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
On Error Resume Next
Worksheets(2).Columns("A:A").Select
Set Action6 = Selection.Find(What:=SearchValue6, After:=ActiveCell, LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Action6 Is Nothing Then
'MsgBox "No clearings made in " & ActiveWorkbook.Name
Else
Action6.Activate
ActiveCell.Offset(0, 1).Select
ActiveCell.ClearContents
End If
Please, try using the next updated code and send some feedback:
Sub FindMultipleTimes()
Dim SearchValue6 As String 'located B9
Dim Action6 As Range 'clear
SearchValue6 = Workbooks.Open("C:\Users\.......xlsm").Worksheets("Sheet1").Range("B9").Value
Dim ws As Worksheet: Set ws = Worksheets(2)
Dim firstAddress As String
Set Action6 = ws.Columns("A:A").Find(What:=SearchValue6, After:=ws.Range("A1"), LookIn:=xlFormulas2, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not Action6 Is Nothing Then
firstAddress = Action6.address
Do
Action6.Offset(0, 1).ClearContents
Set Action6 = ws.Columns("A:A").FindNext(Action6) 'find the next occurrence
Loop While Action6.address <> firstAddress
Else
MsgBox SearchValue6 & " could not be found in column ""A:A"" of sheet " & ws.name
End If
End Sub
I only adapted your code, but do you want letting the workbook necessary to extract SearchValue6 value, open?
I want to know if it is possible to bring two workbooks (sheet1) into one worksheet (master workbook). I need to have two data together in one worksheet.
Any help is really appreciated. Below are screenshots for further details if helps.
Two files:
Fundraise-pages(1).csv
Supporters(1).csv
Please see the attached for further details if help?
Thank you for looking into this.
File 1 and File 2 data - both have the same email addresses.
Importantly I would like a macro to pull data to identify or even match for both email addresses. If found then add it onto a worksheet.
For example on the worksheet (master) there should be a data file 1 on the left hand side and the data file 2 on the right hand side including headings.
I hope you can see my images clearly?
Regards
V
enter image description here
enter image description here
Here is an overview to start with:
https://support.microsoft.com/en-us/office/import-or-export-text-txt-or-csv-files-5250ac4c-663c-47ce-937b-339e391393ba
Vba Import CSV files to Excel
Proposal:
Using the connection via PowerQuery allows you to further process them as tables and also join/merge them into one while you can see and follow each step of the process.
Something like this should do what you want. Feel free to change the code to suit your needs.
Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "RDBMergeSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name "RDBMergeSheet"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
'Find the last row with data on the DestSh
Last = LastRow(DestSh)
'Fill in the range that you want to copy
Set CopyRng = sh.Range("A1:G1")
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
'This example copies values/formats, if you only want to copy the
'values or want to copy everything look at the example below this macro
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
'Optional: This will copy the sheet name in the H column
DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
Source code:
https://www.rondebruin.nl/win/s3/win002.htm
Also, check this out.
https://www.rondebruin.nl/win/addins/rdbmerge.htm
I have a excel sheet with around 50k rows and i need a macro to search for a cell in that sheet and if it finds it to copy the entire row to another sheet, my problem is that the keyword may be on multiple rows so if there are like 4 cells with that keyword i need it to copy all 4 rows and paste them in another sheet
Dim intPasteRow As Integer
intPasteRow = 2
Sheets("Sheet2").Select
Columns("A:AV").Select
On Error Resume Next
Selection.Find(What:="m12", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=True, SearchFormat:=True).Activate
If Err.Number = 91 Then
MsgBox "ERROR: 'Keyword' could not be found."
Sheets("Sheet1").Select
End
End If
Dim intRow As Integer
intRow = ActiveCell.Row
Rows(intRow & ":" & intRow).Select
Selection.Copy
Sheets("Sheet1").Select
ActiveSheet.Paste
End Sub
Sub saci()
Dim rng As Range
Set rng = Range(ActiveCell, ActiveCell.Offset(10000, 0))
rng.EntireRow.Select
With Selection.EntireRow
.Cut
.Offset(.Rows.Count + 1).Insert
.Select
End With
Range("A4").Select
End Sub
so far its finding the first "m12" cell in Sheet2 and copies the entire row to Sheet1, how do i make it continue to search after finding "m12" and copy all rows with the "m12" in them instead of just the first one?
How to get the value from a cell on an active sheet and look it up on a non active sheet and then rename the value?
Dim rw As Long
rw = ActiveCell.Row
If Sheets("Home").Range("D" & rw).Value = "Tender" Then
With Worksheets("Time Allocation").Columns("B:B")
Set cell = .Find(What:=.Range("B" & rw).Value, After:=Range("B" & rw), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cell Is Nothing Then
cell.Value = "test"
Else
cell.Value = "test"
End If
End With
End If
I have tried using cell.value = "test" but this causes an error:
object variable or block with variable not set
please can someone show me where I am going wrong?
The bad news is that you cannot .Select one or more cells on an inactive worksheet. The good news is that there is absolutely no requirement that you do so and in fact it is generally less efficient than directly addressing the cell, cells or column(s).
Dim rw As Long, cell as range
rw = ActiveCell.Row
If Sheets("Sheet1").Range("D" & rw).Value = "Tender" Then
With Worksheets("Sheet2").Columns("B:B")
Set cell = .Find(What:=Sheets("Sheet1").Range("B" & rw).Value, LookIn:=xlFormulas, _
LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not cell Is Nothing Then
cell = "test" '<~~the default property is the .Value
Else
MsgBox "cannot test. not found. cell is nothing and cannot be referenced."
End If
End With
End If
The way you are bouncing around between two worksheets and referring to the ActiveCell property like it is on one worksheet sometimes and another worksheet other times is a little confusing. I'm not sute I got the What parameter right in the Range.Find method.
See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.
I have an Excel tracker that I put an "X" in a cell every month if a certain activity is accomplished.
This "X" correlates to a range of cells on the same sheet.
I want when I click on a command box;
If the cell for January has an "X" copy specific cells on the current page to specific cells on another work sheet.
If the cell for February has an "X" copy some other specific cells on the current page to some other specific cells on the other worksheet.
So on and so forth through December.
I have the following code (which does not work):
Private Sub CommandButton1_Click()
Sheets("MRT").Select
If InStr(1, (Range("L8").Value), "X") > 0 Then
Range("E42:AA42").Select
Selection.Copy
Sheets("Test '12").Select
Cells(3, AP).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone
End If
End Sub
Try this:
Private Sub CommandButton1_Click()
If Sheets("MRT").Range("L8").Value like "*X*" Then
Sheets("MRT").Range("E42:AA42").Copy
Sheets("Test '12").Cells(3, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone
End If
End Sub
Worked in my test, however you might want to adapt Cells(3,1) and the other position specifiers to your desired targets.
edit: forgot about the part with the months ... wait a minute ... here:
Sub FindSignificant()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "a"
Application.FindFormat.Clear
' loop through all sheets
Set sh = Sheets("MRT")
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlFormulas, _
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
Select Case sh.Cells(cl.Row, 1).Value
Case "december"
sh.Range("E42:AA42").Copy
Sheets("Test '12").Cells(3, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone
Case "february"
sh.Range("E42:AA42").Copy
Sheets("Test '12").Cells(3, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone
Case Else
'do nothing
End Select
' find next instance
Set cl = sh.Cells.FindNext(After:=cl)
' repeat until back where we started
Loop Until FirstFound = cl.Address
End If
End Sub
this code origins from here
You would have to adapt the select case, but i really would think about solving this without VBA, if it is not necessary ;)