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
Related
New to forum and vba but want to learn more.
Got two tables of large data and want to look for a cell value equal to the cell value to the left of my active cell in table 1 and then find that value in the 2nd table. When value is found I want to return the cell value found in the 5th column to the right of column A in the 2nd table.
The macro I have created works well - if it hadn't been that it always looks for the same value "10.136.32.10" i.e. this value does not change as the active cell moves down table 1. I would like the value to change depending on what is actually copied from the cell to the left. Is there a way to do this? I use Ctrl+f function and then paste in the cell value copied from table 1
Have the following macro:
Sub Makro2()
'
' Makro2 Makro
'
'
ActiveCell.Offset(0, -1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("SKF-NOV-6-2017").Select
Cells.Find(What:="10.136.32.10", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 4).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All Equipment").Select
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub
Here is the code by which you can do your job. This macro searches immediately on all rows. If you only need to search for an active cell, then you need to remove the loop.
Sub macro2()
Dim lr As Long, r As Long, c As Long
Dim str As String
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 1 To lr
str = Cells(r, c).Offset(0, -1)
Sheets("SKF-NOV-6-2017").Select
Cells.Find(What:=str, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Selection.End(xlToLeft).Select
ActiveCell.Offset(0, 4).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("All Equipment").Select
Cells(r, c + 1).past
Next r
End Sub
I am new to VBA and have primarily used it in conjunction with creating a macro. As you can see from the code below, I am trying to take tables from three different tabs and merge them into one. However, I am having a hard time understanding how to ensure that each table will paste directly underneath the previous table and not overwrite it (especially when each month new rows are created).
Thank you in advance for any help you can provide.
' Step_4_Combination_Tab Macro
Sheets("Past Data").Select
Range("A2:M2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Combination").Select
Range("A1").Select
ActiveSheet.Paste
Range("A1").Select
Selection.End(xlDown).Select
Range("A5483").Select
Sheets("Actual").Select
Range("A5:M5").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Combination").Select
Range("A5483").Select
ActiveSheet.Paste
Range("A5483").Select
Selection.End(xlDown).Select
Range("A8341").Select
Sheets("Forecast").Select
Range("A4:M4").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Combination").Select
ActiveSheet.Paste
Selection.End(xlUp).Select
End Sub
The following code might do what you want:
Sub mergeSheets()
Set targetSheet = Sheets("Combination")
For i = 1 To Sheets.Count
If Sheets(i).Name <> "Combination" Then
Last = LastRow(Sheets("Combination"))
Sheets(i).UsedRange.Copy targetSheet.Cells(Last + 1, 1)
End If
Next i
End Sub
Function LastRow(sh As Worksheet)
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End Function
some codebits taken from here https://www.exceltip.com/cells-ranges-rows-and-columns-in-vba/copy-the-usedrange-of-each-sheet-into-one-sheet-using-vba-in-microsoft-excel.html
You will need to find the last row that has data and paste you next table there.
LR = Sheets("Combination").Range("A" & Rows.Count).End(xlUp).Row
Pasterange = "A" & LR
Sheets("Combination").Range(Pasterange).Paste
I am guessing that you want to copy data from tabs "Past data", "Actual" and "Forecast" to "Consolidated". Am I right? And for some odd reason data in source worksheets begins in different rows. I would do it this way:
Sub AllToCons()
CopyToCons "Past data", 2
CopyToCons "Actual", 5
CopyToCons "Forecast", 4
End Sub
Sub CopyToCons(wsName As String, lRow As Long)
'wsName: name of sheet we are copying from
'lRow: number of row where data start
Dim ws As Worksheet
Dim wsCons As Worksheet
Dim rng As Range
Set wsCons = ThisWorkbook.Worksheets("Consolidated")
Set ws = ThisWorkbook.Worksheets(wsName)
With ws
Set rng = Range(.Range("A" & lRow), .Range("M" & .Cells.Rows.Count).End(xlUp))
End With
rng.Copy
With wsCons
.Range("A" & .Cells.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteAll
End With
If you want to paste values only, type xlPasteValues instead of xlPasteAll.
Hope it helped.
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
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!
I am hoping someone can help me in my dilemma. Due to a system software limitation, I need to have all my code in one Macro.
1.) take Column Q which is a name in the format "last, first" and break it up using text to column (some names contain initials which is why I used text to column)
2.) include code to dismiss the message box that appears "Do you want to replace the contents of the destination cells?"
3.) delete all columns that are generated except the "last" & "first" name.
4.) concatenate the two columns so that they read Firstname Lastname
5.) auto fill down to the last row.
6.) copy paste special the values into an adjacent column and delete the old column with the function.
I tried recording the code but it seems that what I need can't be recorded and needs to be written.
Here's my shot at get those steps to work, pulled from code from my recorder as well as forums, and think I need a trained eye to sort through my mess:
Application.DisplayAlerts = False
Columns("Q:Q").Select
Selection.TextToColumns Destination:=Range("Q1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
Semicolon:=True, Comma:=True, Space:=True, Other:=False, FieldInfo:= _
Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), TrailingMinusNumbers:= _
True
Application.DisplayAlerts = False
Columns("U:U").Select
Selection.Delete Shift:=xlToLeft
Columns("S:T").Select
Selection.ClearContents
Range("S2").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[-1], "" "", RC[-2])"
Range("S2").Select
Selection.AutoFill Destination:=Range("S2:500")
Range("S2:S42").Select
Columns("S:S").Select
Selection.Copy
Columns("T:T").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("T1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Lead Recruiter"
Columns("Q:S").Select
Selection.Delete Shift:=xlToLeft
Thanks to Ron, I was able to get the msg box to dismiss. Right now it's break at the line:
Selection.AutoFill Destination:=Range("S2:500")
How can I update this to Autofill to the last row? The data is in column Q. Any insight is greatly appreciated.
My Assumptions
Data is in Column Q
There is no data after Column Q
The results needs to be generated in Column R. If you want to replace the values in Column Q then see CODE 2.
Here is the shortest code that I could think of.
CODE 1
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find the last row in a column
lRow = .Range("Q" & .Rows.Count).End(xlUp).Row
'~~> Enter the formula in the complete column
.Range("R1:R" & lRow).Formula = "=IFERROR(MID(Q1,FIND("","",Q1,1)+2,FIND("" "",Q1,FIND("","",Q1,1)+2)-" & _
"(FIND("","",Q1,1)+2)),MID(Q1,FIND("","",Q1,1)+2,LEN(Q1)-FIND("","",Q1" & _
",1)+2+1)) & "" "" & LEFT(Q1,FIND("","",Q1,1)-1)"
'~~> Convert all formulas to values in one go
.Range("R1:R" & lRow).Value = .Range("R1:R" & lRow).Value
End With
End Sub
ScreenShot
CODE 2
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find the last row in a column
lRow = .Range("Q" & .Rows.Count).End(xlUp).Row
'~~> Enter the formula in the complete column
.Range("R1:R" & lRow).Formula = "=IFERROR(MID(Q1,FIND("","",Q1,1)+2,FIND("" "",Q1,FIND("","",Q1,1)+2)-" & _
"(FIND("","",Q1,1)+2)),MID(Q1,FIND("","",Q1,1)+2,LEN(Q1)-FIND("","",Q1" & _
",1)+2+1)) & "" "" & LEFT(Q1,FIND("","",Q1,1)-1)"
'~~> Convert all formulas to values in one go
.Range("R1:R" & lRow).Value = .Range("R1:R" & lRow).Value
'~~> Delete Col Q so R moves to Q
.Columns(17).Delete Shift:=xlToLeft
End With
End Sub
Followup from Comments
Is this what you are trying?
Sub FormatPushReport()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Range("R:R,U:U").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("R1").Value = .Range("Q1").Value
.Range("V1").Value = .Range("U1").Value
'~~> Find the last row in a column
lRow = .Range("Q" & .Rows.Count).End(xlUp).Row
'~~> Enter the formula in the complete column
.Range("R2:R" & lRow).Formula = "=IFERROR(MID(Q2,FIND("","",Q2,1)+2,FIND("" "",Q2,FIND("","",Q2,1)+2)-" & _
"(FIND("","",Q2,1)+2)),MID(Q2,FIND("","",Q2,1)+2,LEN(Q2)-FIND("","",Q2" & _
",1)+2+1)) & "" "" & LEFT(Q2,FIND("","",Q2,1)-1)"
.Range("V2:V" & lRow).Formula = "=IFERROR(MID(U2,FIND("","",U2,1)+2,FIND("" "",U2,FIND("","",U2,1)+2)-" & _
"(FIND("","",U2,1)+2)),MID(U2,FIND("","",U2,1)+2,LEN(U2)-FIND("","",U2" & _
",1)+2+1)) & "" "" & LEFT(U2,FIND("","",U2,1)-1)"
'~~> Convert all formulas to values in one go
.Range("R2:R" & lRow).Value = .Range("R2:R" & lRow).Value
.Range("V2:V" & lRow).Value = .Range("V2:V" & lRow).Value
.Columns(18).Replace What:="#VALUE!", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Columns(22).Replace What:="#VALUE!", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.Range("Q:Q,U:U").Delete Shift:=xlToLeft
End With
End Sub
Screenshot