Pastespecial, paste only values from formual - excel

I have 2 sheets, in one sheet I am looking for a specifik text in a column, if that exists then it should copy all the rows with the specific text and paste them in another sheet. That is working for me, but the problem is that when I want to pastespecial, only paste the values and not the formulas I isn't working.
Here is the code, any idea what to do?
With Sheets(1)
LR = .Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("A" & i).Value = "Orange" Then .Rows(i).Copy
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValues
Next i
End With

You have a logic error in the code, which I missed in my comment. You need the PasteSpecial inside the If block:
With Sheets(1)
LR = .Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("A" & i).Value = "Orange" Then
.Rows(i).Copy
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
Paste:=xlPasteValues
End If
Next i
End With

While the answer of Rory should fit perfectly your problem, you could speed it up by a big amount using a variable for the ranges to copy...
Dim rng As Range
With Sheets(1)
LR = .Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
If .Range("A" & i).Value = "Orange" Then
If rng Is Nothing Then
Set rng = .Rows(i)
Else
Set rng = Union(rng, .Rows(i))
End If
End If
Next
rng.EntireRow.Copy
Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End With
Written by phone. May contain errors.

Related

Applying formatting border change across all worksheets but one not working?

I have the below code to apply a purple top/bottom border to the last row of every sheet except the one titled "BudgetByMonth" however when I run it it only applies to the sheet I'm currently on. I've been staring at it for ages, can someone help me out?
Sub FormatLastRow()
Dim ws As Worksheet
Dim LastRow As Long
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "BudgetByMonth" Then
With ws
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LastRow, "BB" & LastRow).Borders(xlEdgeBottom).Weight = xlMedium
Range("A" & LastRow, "BB" & LastRow).Borders(xlEdgeBottom).ColorIndex = 29
Range("A" & LastRow, "BB" & LastRow).Borders(xlEdgeTop).Weight = xlMedium
Range("A" & LastRow, "BB" & LastRow).Borders(xlEdgeTop).ColorIndex = 29
End With
End If
Next ws
End Sub
Your Range objects are not using the With - put a period before them like this: .Range("A" &
Even better, you can simplify it like this:
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
With ws.Range("A" & LastRow, "BB" & LastRow)
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeBottom).ColorIndex = 29
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeTop).ColorIndex = 29
End With
That way, if the "A" or "BB" changes later, you only need to change it in one place.
When using a with statement, you need to put a period before the object you are referring to. In your current code you are only referring to the activesheet because you are missing the periods.
Sub FormatLastRow()
Dim ws As Worksheet
Dim LastRow As Long
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "BudgetByMonth" Then
With ws
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A" & LastRow, "BB" & LastRow).Borders(xlEdgeBottom).Weight = xlMedium
.Range("A" & LastRow, "BB" & LastRow).Borders(xlEdgeBottom).ColorIndex = 29
.Range("A" & LastRow, "BB" & LastRow).Borders(xlEdgeTop).Weight = xlMedium
.Range("A" & LastRow, "BB" & LastRow).Borders(xlEdgeTop).ColorIndex = 29
End With
End If
Next ws
End Sub

copy and paste special with VBA in excel

I wrote this code and it keeps giving me an error that the size of the copy area and the paste area are not the same.
but if I just use the copy-paste method, it works perfectly. could you pls help me out.
Sub copy()
eRow = Sheet5.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Sheet4.Range("a4", "d23").copy
Sheet5.Cells(eRow, 1).PasteSpecial (xlPasteValues)
End Sub
Move values one by one with a value transfer. As implied in the name, a value transfer does not carry over formats.
This just copies the 2 individual cells A4 & D23
Sub copy_me()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lr As Long
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1).Row
ws.Range("A" & lr).Value = ws.Range("A4").Value
ws.Range("D" & lr).Value = ws.Range("D23").Value
End Sub
If you meant to grab the entire range A4:D23 then
ws.Range("A4:D23").Copy
ws.Range("A" & lr).PasteSpecial xlPasteValues
OR
ws.Range("A" & lr).Resize(20, 4).Value = ws.Range("A4:D23").Value

How Do I Automate Multiple Text Filter Contains Sequences and Add The Text Value To The Column To The Right?

I've been trying to implement excel VBA's at work. I have to manually categorise each keyword into categories and my current process is a simple text filter contains then manually add to all cells (GIF to demonstrate at the bottom of the post).
The community has helped me get this far with my VBA code - I'm trying to loop through a range C2:C3 (freehold and leasehold) and then return the value freehold or lease hold in column B next to the relevant keyword.
I'm completely stuck on why this isn't working and I would love a hand.
Here is the excel spreadsheet I'm using to test my macro on
Sub LoopRange()
Dim lastrow, i As Variant
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Dim rCell As Range
Dim rRng As Range
Set rRng = Sheet1.Range("C2:C3")
For Each rCol In rRng.Columns
For Each rCell In rCol.Rows
Debug.Print rCell.Address, rCell.Value
Next rCell
Next rCol
For i = 2 To lastrow
If Range("A" & i).Value Like "*rCell.Value*" Or Range("A" & i).Value Like "*rCell.Value" Or Range("A" & i).Value Like "rCell.Value*" Then
Range("B" & i).Value = "rCell.Value"
End If
Next i
End Sub
There is usually another 20-40 terms just like freehold and leasehold - that is why I need to use a loop through sequence.
P.S. Thank you to those who already replied - you guys have been immensely helpful already and I can't wait to improve my skills and start giving back to this community
Current process of manually adding the keyword categorisation.
Thanks again I really appreciate it guys!
use the below code.
Sub test()
Dim lastrow, i As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
If Range("A" & i).Value Like "*freehold*" Or Range("A" & i).Value Like "*freehold" Or Range("A" & i).Value Like "freehold*" Then
Range("B" & i).Value = "yes"
End If
Next i
End Sub
Output:
EDIT 1
As requested, try this with below.
Sub LoopRange()
Dim lastrow As Long, i As Long, lastfilterrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
lastfilterrow = Range("C" & Rows.Count).End(xlUp).Row
For j = 2 To lastfilterrow
For i = 2 To lastrow
If Range("A" & i).Value Like "*" & Range("C" & j).Value & "*" Then
Range("B" & i).Value = Range("C" & j).Value
End If
Next i
Next j
End Sub

VBA incorrectly entering range as series name

I'm trying to create a dynamic graph in VBA in Excel. Unfortunately, when I generate the graph the values that are meant for the y axis are ending up as the series name. Here's the graph. I'm an extremely new VBA user, so I'm sure I am just overlooking a very basic error. Here is my code.
Sheets("Sheet1").Activate
Dim lRow As Long
Dim lCol As Long
'Find the last non-blank cell in column G and H
Range("G3:H500").Select
Do Until ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
Loop
lastrow = ActiveCell.Row - 1
'Find the last non-blank cell in row 1
lCol = Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox "Last Row: " & lastrow & vbNewLine & _
"" & Columns(lCol).Address(False, False)
Set rng = Range("G3:H300" & lastrow)
Set xrang = Range("F3:F300" & lastrow)
rng.Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ApplyChartTemplate ( _
"c:\users\name\appdata\Roaming\Microsoft\Templates\Charts\brand_line_chart.crtx" _
)
ActiveChart.SetSourceData Source:=Range("G3:H300" & lastrow)
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(1).XValues = Range("F3:F300" & lastrow)
Any help would be greatly appreciated, thank you!
Delete this line ActiveChart.SeriesCollection(1).XValues = Range("F3:F300" & lastrow). Also since you are getting the lastrow data , change the ("G3:H300" & lastrow) to ("G3:H" & lastrow). Do this everywhere you have set range

Remove all the "Selections" from a recorded Macro

I recorded this then added the LR = LastRow to make it dynamic but I can not figure out how to remove all the selections that go on
Also these both do the samething but is one way of writting the array better then the other i.e faster, more stable...
Thanks
Selection.FormulaArray = "=ISNUMBER(MATCH(RC[-5]&RC[-6],R1C1:R" & LR & "C1 & R1C2:R" & LR & "C2,0))"
Selection.FormulaArray = "=ISNUMBER(MATCH(B1&A1,$A$1:$A$" & LR & " & $B$1:$B$" & LR & ",0))"
Recorded Macro
Sub Winding()
Dim ws As Worksheet
Dim Rng As Range
Dim LR As Long
Set ws = Sheets("Unpivot_RegistrationData")
LR = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Set Rng = ws.Range("G1").Resize(LR, 1)
Range("G1").Select
Selection.FormulaArray = "=ISNUMBER(MATCH(B1&A1,$A$1:$A$" & LR & " & $B$1:$B$" & LR & ",0))"
Selection.AutoFill Destination:=Rng, Type:=xlFillDefault
End Sub
Firstly, preference for A1 or R1C1 style should be driven by ease of constructing the formula string. There is no diffrence in performance or stability
To remove the Selection try this
Note that I have removed the AutoFill, and applied to formula to the whole range in one step.
Sub Winding()
Dim Rng As Range
Dim LR As Long
With Worksheets("Unpivot_RegistrationData")
LR = .Cells(.Rows.Count, 1).End(xlUp).Row
Set Rng = .Range("G1:G" & LR)
End With
Rng.Cells(1, 1).FormulaArray = _
"=ISNUMBER(MATCH(B1&A1,$A$1:$A$" & LR & " & $B$1:$B$" & LR & ",0))"
Rng.Cells(1, 1).AutoFill Destination:=Rng, Type:=xlFillDefault
End Sub

Resources