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!
Related
So, for my work we are constantly cross referencing Part #'s. I am looking to create a VBA that will VLOOKUP column 13, 14, 23, 24 From sheet 2 and copy and paste values to sheet 1 R-U. I tried recording the macros on my first sheet and it worked fine, but when I try it on a different workbook it doesn't seem to work. Below is what I have.
Sub Vlookupandreplacetemplate()
'
' Vlookupandreplacetemplate Macro
'
'
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-16],Sheet2!C[-17]:C[64],13,0)"
Selection.AutoFill Destination:=Range("R2:R401")
Range("R2:R401").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-16],Sheet2!C[-17]:C[64],13,0)"
Range("S2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-17],Sheet2!C[-18]:C[63],14,0)"
Range("S2").Select
Selection.AutoFill Destination:=Range("S2:S401")
Range("S2:S401").Select
Range("T2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-18],Sheet2!C[-19]:C[62],23,0)"
Range("T2").Select
Selection.AutoFill Destination:=Range("T2:T401")
Range("T2:T401").Select
Range("U2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-19],Sheet2!C[-20]:C[61],24,0)"
Range("U3").Select
ActiveCell.FormulaR1C1 = ""
Range("U2").Select
Selection.AutoFill Destination:=Range("U2:U401")
Range("U2:U401").Select
Columns("R:U").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.Replace What:="#N/A", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
Make sure your new spreadsheet has a "Sheet2". No space like there is in your question.
I suspect this is where you are going wrong.
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.
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
Hope you are enjoying good health.
I have a data recording file to keep record of POs and than it payments. I was using excel file with lots of different formulas to maximize the automation of operations. But it is getting heavier and it is impossible to work with it any more.
So I generated the idea to remove all the formulas and put it in first row only. Now i want excel to copy those formulas from that first row every time i run the command and past at the required row to update the records and immediately should it convert into value. This will keep all the data updated with automation and reduction in processing time.
The script i am using here is working but at one point i have to use find function. it should find the required value in column B of selected sheet and make selection of cell found as results. After the selection it will offset to a desired column to proceed the further steps.
Sub find()
Sample Sheets("PO").Range("a1"), Sheets("PO").Range("b:b")
End Sub
The complete script is
Sub Macro4()
'
' Update the PO/WO detail with the effect of new payment entry and alsoupdate the corresponding in payment entry
'
' Keyboard Shortcut: Ctrl+j
'
ActiveCell.Offset(0, -16).Range("A1").Select
ActiveCell.FormulaR1C1 = "*"
ActiveCell.Offset(0, 7).Range("A1").Select
Selection.Copy
ActiveSheet.Previous.Select
Range("A1").Select
ActiveSheet.Paste
End Sub
Sub find()
Sample Sheets("PO").Range("a1"), Sheets("PO").Range("b:b")
End Sub
Sub Sample(FirstRange As Range, ListRange As Range)
Dim aCell As Range, bCell As Range, oRange As Range
Set oRange = ListRange.find(what:=FirstRange.Value, LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
ActiveCell.Offset(0, 7).Range("A1").Select
ActiveCell.Offset(0, 10).Range("A1").Select
ActiveCell.FormulaR1C1 = "*"
Selection.End(xlUp).Select
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, -1).Range("A1:B1").Select
ActiveCell.Activate
Selection.Copy
ActiveCell.Offset(0, 10).Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, -10).Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 10).Range("A1").Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveCell.Offset(0, -10).Range("A1").Select
ActiveSheet.Next.Select
Range("A1").Select
ActiveCell.Offset(0, 16).Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 16).Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, -16).Range("A1").Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveCell.Offset(0, 17).Range("A1").Select
End Sub
Kindly suggest any resolution,
Thanks in advance
Regards
Emm, if I understand your problem correctly, all you need is a select statement.
Set oRange = ListRange.find(what:=FirstRange.Value, LookIn:=xlValues, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
oRange.Select
Although you don't use your oRange variable anywhere else in your code, so you might just get rid of it. Did you write this code? Do you understand it? This is by no means a clean, optimized, good, readable or fast code... But if it works for you, then never mind that.
I have an automated filed that comes out with a row (4) of headers
the first column (A) contains different categories (types of payment), in base of the specific type I would run a different test based on a certain sheet contained into the file I'm working on.
What am I doing at the moment is basically create a filter (of the first column A) based on the type of payment I want to check, create a temporary sheet, copy paste this filtered view, work on that with the checks and then copy/paste the result (contained in the column R) into the main sheet.
The problem comes out in the last part, when I want to copy paste, because of the filtered view I can't Just go to the first free cell under the header of R and copy paste, because the system doesn't understand I'm moving into a filtered view. I have to be sure that I'm copy pasting the correct results in base of the value (string) contained into the A column), can you help me to solve it please?.
Another way is to do a for each loop, but actually I'm not sure about how to structure it.
Here we have the part of the code i'm working on
Sub Payexample()
' normal cleaning procedures
Sheets("Payexample").Select
Rows("1:10").Select
Selection.Delete Shift:=xlUp
Columns("J:J").Select
Selection.Replace What:="AED", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.NumberFormat = "0.00"
Sheets("Sheet1").Select
ActiveSheet.Range("$A$4:$A$500").AutoFilter Field:=1, Criteria1:="Payfort"
Range("A5 : N5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Name = "Payfort momentary"
Range("A1").Select
ActiveSheet.Paste
Range("$M$1").Formula = Range("B1") & (",") & Range("M1")
Dim Lastrow As Long
Application.ScreenUpdating = False
Lastrow = Range("J" & Rows.Count).End(xlUp).Row
Range("O1").FormulaR1C1 = "=IF(RC[-2]-VLOOKUP(RC[-3],'PayFort'!B:J,9,FALSE)<=0.99, ""Payfort Payment Checked"", ""Manual Verification Needed"")"
Range("O1").AutoFill Destination:=Range("O1:O" & Lastrow)
Range("O1:O" & Lastrow).Select
Selection.Copy
Sheets("Sheet1").Select
ActiveSheet.Range("$A$4:$A$500").AutoFilter Field:=1, Criteria1:="Payfort"
Range("R5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Sheets("Payfort Momentary").Select
'ActiveWindow.SelectedSheets.Delete
End Sub
I think this will do mostly what you are after but I wasn't sure what you want to do with your formulas, hopefully you'll be able to work out what you want to do with them from here:
Sub Payexample()
Dim rngCheck As Range
Dim r As Range
Dim rowNum As Long
' normal cleaning procedures
Sheets("Payexample").Select
Rows("1:10").Select
Selection.Delete Shift:=xlUp
Columns("J:J").Select
Selection.Replace What:="AED", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.NumberFormat = "0.00"
Sheets("Sheet1").Select
With ActiveSheet
rngCheck = .Range("$A$4:$A$500")
For Each r In rngCheck.Rows ' Loop through the rows in your data area
rowNum = r.Row
If .Range("A" & rowNum) = "Payfort" Then
' Either
.Range("R" & rowNum).Formula = "" ' Your formula here
.Range("R" & rowNum) = .Range("R" & rowNum).Value ' Change formula to value
' Or
.Range("R" & rowNum) = ' Do calculations in VBA without formula
End If
Next r
End With
End Sub