I am searching multiple sheets for a string, once this string is found it copies it to the MergedData sheet.
The problem i am having is it is coping the formulas rather that the value. I have been searching for hours and cannot seem to find a solution.
Here is the code that i am using at the moment.
Any assistance you can provide is greatly appreciated!
Thanks Aarron
Private Sub CommandButton1_Click()
Dim FirstAddress As String, WhatFor As String
Dim Cell As Range, Sheet As Worksheet
With Application
.ScreenUpdating = False
.EnableEvents = False
.CutCopyMode = False
End With
WhatFor = Sheets("SUB CON PAYMENT FORM").Range("L9")
Worksheets("MergedData").Cells.Clear
If WhatFor = Empty Then Exit Sub
For Each Sheet In Sheets
If Sheet.Name <> "SUB CON PAYMENT FORM" And Sheet.Name <> "MergedData" _
And Sheet.Name <> "Details" Then
With Sheet.Columns(1)
Set Cell = .Find(WhatFor, LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
If Not Cell Is Nothing Then
FirstAddress = Cell.Address
Do
Cell.EntireRow.Copy ActiveWorkbook.Sheets("MergedData").Range("A" & Rows.Count)_
.End(xlUp).Offset(1, 0)
Set Cell = .FindNext(Cell)
Loop Until Cell Is Nothing Or Cell.Address = FirstAddress
End If
End With
End If
Next Sheet
Set Cell = Nothing
End Sub
With a little help from others all i had to do was replace.
Cell.EntireRow.Copy ActiveWorkbook.Sheets("MergedData").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
With this:
Cell.EntireRow.Copy
ActiveWorkbook.Sheets("MergedData").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlValues
Don't use the range.copy method with a destination, but use only copy and use PasteSpecial with XlPasteType = xlPasteValues
Range.PasteSpecial Method (Excel)
Related
I'm using visual basic to create a checkout system in an excel sheet. The sheet will be filled with information for a project, each of the projects requires that we send out a kit. This excel sheet will allow for a barcode to be scanned, when this happens, it checks for puts an "out" time. When that barcode is scanned again it puts an "in" time. The issue I'm having is that if that barcode is scanned a third time, it will only update the out time.
How do I set it up where it will see that an "in" and "out" time have been recorded and thus go the next blank cell in the row and add the barcode + new "in" or "out" time. Any help would be greatly appreciated!
This is the code I am using.
Code for on the worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
Application.EnableEvents = False
Call inout
Application.EnableEvents = True
End If
End Sub
code for the macro
Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long
barcode = Worksheets("Sheet1").Cells(2, 2)
Set rng = Sheet1.Columns("a:a").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If rng Is Nothing Then
ActiveSheet.Columns("a:a").Find("").Select
ActiveCell.Value = barcode
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
Else
rownumber = rng.Row
Worksheets("Sheet1").Cells(rownumber, 1).Select
ActiveCell.Offset(0, 2).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
End If
Worksheets("Sheet1").Cells(2, 2).Select
End Sub
All this goes in the worksheet code module:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
inout 'use of Call is deprecated
End If
End Sub
Sub inout()
Dim barcode As String
Dim rng As Range
Dim newRow As Boolean
barcode = Me.Cells(2, 2)
'find the *last* instance of `barcode` in ColA
Set rng = Me.Columns("A").Find(What:=barcode, after:=Me.Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
'figure out if we need to add a new row, or update an existing one
If rng Is Nothing Then
newRow = True 'no matching barcode
Else
'does the last match already have an "in" timestamp?
If Len(rng.Offset(0, 2).Value) > 0 Then newRow = True
End If
If newRow Then
Set rng = Me.Cells(Me.Rows.Count, "A").End(xlUp).Offset(1, 0)
rng.Value = barcode
SetTime rng.Offset(0, 1) 'new row, so set "out"
Else
SetTime rng.Offset(0, 2) 'existing row so set "in"
End If
Me.Cells(2, 2).Select
End Sub
'set cell numberformat and set value to current time
Sub SetTime(c As Range)
With c
.NumberFormat = "m/d/yyyy h:mm AM/PM"
.Value = Now
End With
End Sub
Good day to everyone,
I have been trying to find an answer here that would fit my problem but I have been unsuccessful. I am using FIND to search column F for cell with #N/A value and copy adjacent cells to another "Sheet2" at the end of the column A. I have made the following code that works but my problem is I want to make it to loop to find the next cell with #N/A value till find all.
Sub Find()
Dim SerchRange As Range
Dim FindCell As Range
Set SerchRange = Range("F:F")
Set FindCell = SerchRange.FIND(What:="#N/A", _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If FindCell Is Nothing Then
MsgBox "Nothing was found all clear"
Else
FindCell.Select
ActiveCell.Offset(0, -3).Resize(, 3).Select
Selection.Copy
Sheets("Sheet2").Select
Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Select
ActiveSheet.Paste
End If
End Sub
Try this and let me know if it works:
Option Explicit
Sub Find()
Application.ScreenUpdating = False
Dim SearchRange As Range
Dim FindCell As Range
Dim Check As Boolean
Dim LastRow As Long
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim FindCounter As Long
Set ws = ThisWorkbook.Worksheets("Sheet1") ' <--- Insert the name of your worksheet here
Set ws2 = ThisWorkbook.Worksheets("Sheet2")
LastRow = ws.Cells(Rows.Count, "F").End(xlUp).Row ' <--- Finds the last used row
Set SearchRange = Range("F1:F" & LastRow)
FindCounter = 0
For Each FindCell In SearchRange
If FindCell.Value = "#N/A" Then
FindCounter = FindCounter + 1
FindCell.Offset(0, -3).Resize(, 3).Copy
ws2.Range("A" & ws2.Cells(Rows.Count, "A").End(xlUp).Row + 1).PasteSpecial xlPasteValues
End If
Next
MsgBox "Succes!" & vbNewLine & vbNewLine & "This many cells were found: " & FindCounter
Application.ScreenUpdating = True
End Sub
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.
My code below works but it is very slow… This code in fact consists to convert the date in column C and D of my sheet (called "Test") from format day.month.year to format day/month/year (For example please see the picture below, the lines 1-2-3-4-5 have been already converted whereas the other lines from line 1183 have not been converted yet).
I am looking for a solution to improve the speed of this macro because if I have a lot of lines to convert in column C and D, the macro is really really slow…
If by chance someone know how to improve the speed of this macro, that would be really fantastic.
Sub convertdatrighteuropeanformat()
Dim cell As Range
Call selectallmylinesctrlshiftdown
Application.ScreenUpdating = False
For Each cell In Selection
With cell
.NumberFormat = "#"
.Value = Format(.Value, "dd/mm/yyyy")
End With
Next cell
Selection.Replace What:="/", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Application.ScreenUpdating = True
End Sub
Sub selectallmylinesctrlshiftdown()
With Sheets("Test")
.Range(.Range("D2"), .Range("E2").End(xlDown)).Select
End With
End Sub
Instead of a loop, refer to the entire Range (previously Selection) at once inside the With block. This is combined into one sub, although there is nothing wrong with your decision to declare the range with a stand alone procedure.
Option Explicit
Sub convert()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Test")
Dim LRow As Long, MyCell As Range, MyRange As Range
LRow = ws.Range("D" & ws.Rows.Count).End(xlUp).Row
Set MyRange = ws.Range("D2:E" & LRow)
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With MyRange
.Value = Format(.Value, "dd/mm/yyyy")
.Replace "/", ".", xlPart, xlByRows
.NumberFormat = "#"
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I want the macro to look for same value as in B2. And then copy the value from range D2:G2 to the found range. In this example D9:G9.
Thank you in advance :D.
I tried:
Sub Button1_Click()
Dim myRng1, myRng2 As Range, cell As Range
Set myRng1 = Range("A4:A1000")
Set myRng2 = Range("D2:G2")
myRng2.Select
Selection.Copy
For Each cell In myRng1
If Range("A2") = Range("A" & cell.Row) Then Range("D" & cell.Row).Select
ActiveSheet.Paste
Next cell
End Sub
Sub Find()
Dim Findcode As String
Dim Rng As Range
Range("A2:F2").Select
Selection.Copy
Findcode = Sheets("Sheet1").Range("a2").Value
If Trim(Findcode) <> "" Then
With Sheets("Sheet1").Range("A4:A60000")
Set Rng = .Find(What:=Findcode, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
ActiveSheet.Paste
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub