Cut and paste the value of cell to another cell in vba - excel

I need to transfer or move the value of Column F until last cell with value to Column D if Column C is eq to 'RRR'. I can't highlight or select the range starting from the Location of 'RRR' to the last cell with value 'SSS'. Instead, it select range from C4:C9 which is wrong.
Dim ws As Worksheet, lRow As Long
Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Dim lCol As Long
With ws
For x = 1 To lRow
If .Cells(x, 3).Value = "RRR" Then
lCol = Cells(x, Columns.Count).End(xlToLeft).Column
Range("C" & x & ":C" & lCol).Select
End If
Next x
End With
Example:
Expected:
Can anyone tell me the problem in my code.

You are very near, only the select range that should be modified.
So you can build your range:
Range(A1:D1) -> Range(Cells(A1), Cells(D1)) ->
Range(Cells(row number, column number), Cells(row number, column number)) ->
Range(Cells(1, 1), Cells(1, 4))
This should do the trick:
Dim ws As Worksheet, lRow As Long
Dim x As Long
Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Dim lCol As Long
With ws
For x = 1 To lRow
If .Cells(x, 3).Value = "RRR" Then
lCol = Cells(x, Columns.Count).End(xlToLeft).Column 'Find the last column number
Range(Cells(x, 6), Cells(x, lCol)).Cut Cells(x, 4) 'Cut from row x and Column F (Column F = 6) to row x and column "lCol". Then paste the range into row x and column 4.
End If
Next x
End With
End Sub

An alternative method would be to delete the cells in columns D and E
Dim ws As Worksheet, lRow As Long
Dim x As Long
Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Dim lCol As Long
With ws
For x = 1 To lRow
If .Cells(x, 3).Value = "RRR" Then .Range("C" & x & ":D" & x).Delete Shift:=xlToLeft
End If
Next x
End With
End Sub

Related

if column A text "customer account" and column M <=0 delete all rows between

enter image description here
Trying to delete complete rows if criteria met. If column A has text "customer account" and column M <=0 then delete all rows between.
It doesn't give any error but does not delete the rows
Dim sh As Worksheet
Set sh = Sheets("RAW DATA FILE")
Dim x As Long, lastrow As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For x = lastrow To 1 Step -1
If Cells(x, 2).Value = "customer account" And Cells(x, 13) <= 0 Then
Rows(x).Delete
End If
Next x
The answer to your current problem could be that you are using a reference to the currently active sheet. You came as far as declaring a parent sheet (sh) but never used it as such. You can overcome that with a simple With:
Dim sh As Worksheet: Set sh = Sheets("RAW DATA FILE")
Dim x As Long, lastrow As Long
With sh
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
For x = lastrow To 1 Step -1
If .Cells(x, 2).Value = "customer account" And .Cells(x, 13) <= 0 Then
.Rows(x).Delete
End If
Next x
End with
That leaves the question wheather or not there are better, faster ways in getting your result. As per #BigBen, you should look into using a filter instead. You could try:
Dim sh As Worksheet: Set sh = Sheets("RAW DATA FILE")
Dim lastrow As Long
Dim rng As Range
With sh
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rng = .Range("A1:M" & lastrow)
rng.AutoFilter Field:=2, Criteria1:="customer account"
rng.AutoFilter Field:=13, Criteria1:="<=0"
rng.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
rng.AutoFilter
End With
This is assuming you are using a header row.
EDIT:
If your intention is to delete a whole range of rows, AutoFilter is not an option nomore. In that case a loop did the trick, but you'll need some Offset to check for your column M value:
Dim sh As Worksheet: Set sh = Sheets("Blad1")
Dim x As Long, lastrow As Long
With sh
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
For x = lastrow To 1 Step -1
If .Cells(x, 2).Value = "customer account" And .Cells(x, 13).Offset(4, 0) <= 0 Then
.Range(x & ":" & x + 4).EntireRow.Delete
End If
Next x
End With
This will delete the rows between AND the rows that are checked. If this is not what you want then you should use: .Range(x+1 & ":" & x + 3).EntireRow.Delete
Try with below
Dim sh As Worksheet
Set sh = Sheets("RAW DATA FILE")
Dim x As Long, lastrow As Long
lastrow = sh.Cells(Rows.Count, 1).End(xlUp).Row
For x = lastrow To 1 Step -1
If sh.Cells(x, 1).Value = "customer account" And sh.Cells(x, 13) <= 0 Then
sh.Rows(x).Delete
End If
Next x

Trying to find data pairs anywhere in a sheet

So I have an excel sheet where I want to loop through Sheet1 and find data pairs similar to Sheet2. So, I have for example A1:B1 and I need to find a row on Sheet2 that has exactly the same values next to each other (but it could be A33:B33 or anywhere) and copy the row over to Sheet1 (in column C or anything)
I am also trying to make it a dynamic loop so it checks for A1:B1 pair against Sheet2 then A2:B2 and so on until the last row.
Now the code I have only checks if A1:B1 on Sheet1 matches A1:B1 on Sheet2 (but not anywhere on the sheet). Also, I cannot make it so that it dynamically checks against every row on Sheet1 (I tried to make it with the x = x + 1 but it doesn't work)
Here is my code:
Sub matchme()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim r As Range
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
r = lastrow = sh1.Range("A" & Rows.Count).End(xlUp).Row
For x = 1 To r
If sh1.Range("A" & x) = sh2.Range("A" & x) And sh1.Range("B" & x) = sh1.Range("A" & x) & sh2.Range("B" & x) Then
sh1.Range("A" & x).EntireRow.Copy Destination:=sh2.Range("C" & x)
x = x + 1
Next x
End Sub
Please help, I have been struggling with this for days now and I need to hand in a report by the end of today, and I just cannot find anything helpful on the internet. I really appreciate any advice
If You want to use loops, try that:
Sub matchme()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim x As Long
Dim i As Long
Dim j As Long
Dim lastrow As Long
Dim lastRow2 As Long
Dim lastCol2 As Long
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
lastrow = sh1.Range("A" & Rows.Count).End(xlUp).Row
With sh2
lastRow2 = .Cells(Rows.Count, 1).End(xlUp).Row
lastCol2 = .Cells(1, Columns.Count).End(xlUp).Column
End With
For x = 1 To lastrow
For i = 1 To lastRow2
For j = 1 To lastCol2
If sh1.Cells(x, 1) = sh2.Cells(i, j) Then
If sh1.Cells(x, 2) = sh2.Cells(i, j + 1) Then
MsgBox "Found match!"
End If
End If
Next j
Next i
Next x
End Sub
I haven't tested this.
I've assumed you are searching for sheet1 A values in sheet2 column A only.
When a match is found, the column C value on sheet2 is copied to column C on sheet1.
Sub x()
Dim rFind As Range, s As String, r As Range
With Sheet1
For Each r In .Range("A1", .Range("A" & Rows.Count).End(xlUp))
Set rFind = Sheet2.Columns(1).Find(What:=r.Value, Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not rFind Is Nothing Then
s = rFind.Address
Do
If rFind.Offset(, 1).Value = r.Offset(, 1).Value Then
r.Offset(, 2).Value = rFind.Offset(, 2).Value
End If
Set rFind = Sheet2.Columns(1).FindNext(rFind)
Loop While rFind.Address <> s
End If
Next r
End With
End Sub
To get the pairs of Sheet1 and look for them in Sheet2:
I've used this code:
Application.ScreenUpdating = False
Dim i As Long
Dim LastRow As Long
Dim rng As Range
Dim wk1 As Worksheet
Dim wk2 As Worksheet
Dim SearchThis As String
Set wk1 = ThisWorkbook.Worksheets("Sheet1")
Set wk2 = ThisWorkbook.Worksheets("Sheet2")
LastRow = wk1.Range("A" & wk1.Rows.Count).End(xlUp).Row
'<--------------------------------->
'For more type of SPECIAL CELLS, and choose exactly the type you need
'please read https://learn.microsoft.com/en-us/office/vba/api/excel.range.specialcells
For i = 1 To LastRow Step 1
SearchThis = UCase(wk1.Range("A" & i).Value & wk1.Range("B" & i).Value)
For Each rng In wk2.Cells.SpecialCells(xlCellTypeConstants, 23)
If UCase(rng.Value & rng.Offset(0, 1).Value) = SearchThis Then
'code to copy where you want
Debug.Print rng.Row
End If
Next rng
Next i
Set wk1 = Nothing
Set wk2 = Nothing
Application.ScreenUpdating = True
The output of this code is:
Those are the row numbers where the pairs are. You just need to add a code to copy the entire row.
Hope this helps
Try below code (comments in code):
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim r As Range
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
lastRow = sh1.Range("A" & Rows.Count).End(xlUp).Row
iLastRow = sh2.Range("A" & Rows.Count).End(xlUp).Row
For j = 1 To lastRow
For i = 1 To iLastRow
If sh1.Cells(j, 1) = sh2.Cells(i, 1) And sh1.Cells(j, 2) = sh2.Cells(i, 2) Then
sh1.Cells(i, 3) = "Write some information"
End If
'you don't need to increment loop variable "Next" does it for you
'also i is better suited for iterator name :)
Next
Next

macro to copy and paste data from one Sheet to another when Header is matching

I am trying to create a macro to copy and paste data from one Sheet to another sheet when Header and Column A data is matching and want to paste into the specific cell.
below code is working fine for me when Row(headers) order is the same in both sheets. but I need a solution for when the row (Headers) are not in the order.
"I hope I was able to explain my problem"
Sub transfer()
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim myname As String
lastrow1 = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow1
myname = Sheets("sheet1").Cells(i, "A").Value
Sheets("sheet2").Activate
lastrow2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
For j = 2 To lastrow2
If Sheets("sheet2").Cells(j, "A").Value = myname Then
Sheets("sheet1").Activate
Sheets("sheet1").Range(Cells(i, "B"), Cells(i, "F")).Copy
Sheets("sheet2").Activate
Sheets("sheet2").Range(Cells(j, "D"), Cells(j, "H")).Select
ActiveSheet.Paste
End If
Next j
Application.CutCopyMode = False
Next i
Sheets("sheet1").Activate
Sheets("sheet1").Range("A1").Select
End Sub
if i understood your goal then may try something like (code is tested with makeshift data)
Sub test()
Dim SrcWs As Worksheet, TrgWs As Worksheet
Dim Col As Long, TrgLastRw As Long, SrclastRw As Long, SrcLastCol As Long, TrgLastCol As Long
Dim SrcRng As Range, TrgRng As Range, C As Range, Hd As String
Set SrcWs = ThisWorkbook.Sheets("Sheet1")
Set TrgWs = ThisWorkbook.Sheets("Sheet2")
SrcLastCol = SrcWs.Cells(1, Columns.Count).End(xlToLeft).Column
TrgLastCol = TrgWs.Cells(1, Columns.Count).End(xlToLeft).Column
For Col = 1 To SrcLastCol
Hd = SrcWs.Cells(1, Col).Value
If Hd <> "" Then
SrclastRw = SrcWs.Cells(Rows.Count, Col).End(xlUp).Row + 1
Set SrcRng = SrcWs.Range(SrcWs.Cells(2, Col), SrcWs.Cells(SrclastRw, Col))
With TrgWs.Range(TrgWs.Cells(1, 1), TrgWs.Cells(1, TrgLastCol))
Set C = .Find(Hd, LookIn:=xlValues) 'each column header is searched in trgWs
If Not C Is Nothing Then
TrgLastRw = TrgWs.Cells(Rows.Count, C.Column).End(xlUp).Row + 1
Set TrgRng = TrgWs.Cells(TrgLastRw, C.Column).Resize(SrcRng.Rows.Count, 1)
SrcRng.Copy Destination:=TrgRng
End If
End With
End If
Next Col
End Sub

Macro insert formula across columns

I am trying to insert a formula across all columns (Column B to last column with data) based on value in Column A.
Below is what I have so far:
Sub Insert_Falldown_Ratio_Formula()
Dim Rng As Range
Dim lRow As Long
Dim lLastRow As Long
lLastRow = Cells(Rows.Count, "A").End(xlUp).Row - 1
lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
For lRow = lLastRow To 2 Step -1
If Cells(lRow, "A").Value = "Falldown Ratio"
Set Rng = Range("B" & (1Row) & ":" & lastcolumn)
Rng.FormulaR1C1 = "=IF(LEFT(RC[-1],2)=""45"",""45'"",IF(RIGHT(RC[-1],1)=""Q"",""40'HC"",LEFT(RC[-1],2)&""'""))"
End If
Next lRow
End Sub
Any help is greatly appreciated! Thanks!
A few things. (1) You were missing a Then on your If line (2) in the following line you had 1Row instead of lRow and (3) you were copying down rather than across.
Sub Insert_Falldown_Ratio_Formula()
Dim Rng As Range
Dim lRow As Long
Dim lLastRow As Long, lastcolumn As Long
lLastRow = Cells(Rows.Count, "A").End(xlUp).Row - 1
lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
For lRow = lLastRow To 2 Step -1
If Cells(lRow, "A").Value = "Falldown Ratio" Then
Set Rng = Range(Cells(lRow, "B"), Cells(lRow, lastcolumn))
Rng.FormulaR1C1 = "=IF(LEFT(RC[-1],2)=""45"",""45'"",IF(RIGHT(RC[-1],1)=""Q"",""40'HC"",LEFT(RC[-1],2)&""'""))"
End If
Next lRow
End Sub

VBA Excel Automatically Copy & Paste Specific cells based on IF statement

The code I have placed below is a combination of what works and what i can't get to work.
The code that is not commented will copy cells to "sheet2" from "sheet1".
What I cannot get to work correctly is the code that I have disabled that would replace my Range Method of coping from "sheet1" to "sheet2".
Also my If Then Code is what will some up what I'm trying to accomplish. I'm trying to get the If statement to search all of column A and copy all Cars that are year 1991 to sheet2.
Mind my poor coding skills I'm Doing my best to show & explain so I can be helped.
Here is the Sheets 1 & 2
(hxxp://s15.postimg.org/orfw7tlaz/test.jpg)
OLD CODE
Sub Macro1()
Set a = Sheets("Sheet1")
Set b = Sheets("Sheet2")
Set c = Sheets("Sheet3")
Dim x
Dim z
Dim lastrow As Long, erow As Long
x = x + 1
z = 2
'lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row
'lastrow = b.Cells(Rows.Count, 1).End(xlUp).Row
'For i = 2 To lastrow
lastrow = b.Range("A" & Rows.Count).End(xlUp).Row + x
'If a.Cells(i, 1) = “1991” Then
'a.Cells(i, 1).Copy
'erow = b.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'b.Paste Destination:=b.Range.Cells(erow, 4)
Range("A" & z).Copy Destination:=b.Range("D" & lastrow)
'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range.Cells(erow, 1)
Range("B" & z).Copy Destination:=b.Range("A" & lastrow)
'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range.Cells(erow, 3)
Range("C" & z).Copy Destination:=b.Range("C" & lastrow)
'a.Cells(i, 1).Copy
'b.Paste Destination:=b.Range(erow, 2)
Range("D" & z).Copy Destination:=b.Range("B" & lastrow)
'End If
'Next i
Application.CutCopyMode = False
Sheet2.Columns().AutoFit
'b.Range("A1").Select
End Sub
So I added some Lines and Began changing the cell locations to reflect the format I need and now when I run the macro it only copys the very last line from Sheet1 to sheet2. I believe it has to do with the order of the way these cells are.
b.Cells(erow, 1) = a.Cells(i, 1)
b.Cells(erow, 2) = a.Cells(i, 2)
b.Cells(erow, 3) = a.Cells(i, 3)
b.Cells(erow, 4) = a.Cells(i, 4)
Changing these back fixes it so it copys all the cells but its not what I'm trying to do.
The Code I'm Trying to run is Below
NEW CODE Working Thanks to EntryLevel!
Sub TakeTwo()
Dim a As Worksheet
Dim b As Worksheet
Dim c As Worksheet
Set a = ThisWorkbook.Sheets("Sheet1")
Set b = ThisWorkbook.Sheets("Sheet2")
Set c = ThisWorkbook.Sheets("Sheet3")
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long
lastrowsheet1 = a.Cells(Rows.Count, 1).End(xlUp).Row
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 2 To lastrowsheet1
If a.Cells(i, 1).Value = "AEM" Then
b.Cells(erow, 31) = a.Cells(i, 1) '<------When I modify these
b.Cells(erow, 6) = a.Cells(i, 4) '<------The copied cells
b.Cells(erow, 28) = a.Cells(i, 5) '<------Don't show up
b.Cells(erow, 26) = a.Cells(i, 6) '<------In Sheet2
b.Cells(erow, 46) = a.Cells(i, 11) '<------Only the last
b.Cells(erow, 29) = a.Cells(i, 14) '<------Line found Is copied to sheet2
erow = erow + 1
End If
Next i
Application.CutCopyMode = False
b.Columns.AutoFit
'b.Range("A1").Select
End Sub
Now Using Same Working Code But Different function Not Working
Sub TakeThree()
Dim a As Worksheet
Dim b As Worksheet
Dim c As Worksheet
Set a = ThisWorkbook.Sheets("Sheet1")
Set b = ThisWorkbook.Sheets("Sheet2")
Set c = ThisWorkbook.Sheets("Sheet3")
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long
lastrowsheet1 = c.Cells(Rows.Count, 1).End(xlUp).Row
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For i = 2 To lastrowsheet1
If c.Cells(i, 1).Value = b.Cells(erow, 6).Value Then 'If serial number is found from sheet2 column 6 in sheet3 Column 1
b.Cells(erow, 8) = c.Cells(i, 2) 'Then copy description from sheet3 cell row to Sheet2 cell row
erow = erow + 1
End If
Next i
Application.CutCopyMode = False
b.Columns.AutoFit
c.Columns.AutoFit
'b.Range("A1").Select
End Sub
So I added another For Loop with Dim r and added another Line erow = erow + r & now the code copys the first 2 rows needed but does not continue iterating down the list. which is confusing me. here is the code below i have added.
Dim r As Long
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long
lastrowsheet1 = c.Cells(Rows.Count, 1).End(xlUp).Row
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
For r = 1 To erow
For i = 2 To lastrowsheet1
If c.Cells(i, 1) = b.Cells(erow, 6) Then
b.Cells(erow, 8) = c.Cells(i, 2)
erow = erow + r
End If
Debug.Print i
Next i
Next r
Based on your statement that I'm trying to get the If statement to search all of column A and copy all Cars that are year 1991 to sheet2, it seems like Autofilter might be an easier solution than looping. You should be able to use something like this:
Sub TestyTestTest()
Dim lastrowsheet1 As Long
Dim lastrowsheet2 As Long
With ThisWorkbook.Sheets("Sheet1")
.AutoFilterMode = False
lastrowsheet1 = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(1, 1), .Cells(lastrowsheet1, 4)).AutoFilter Field:=1, Criteria1:="1991"
.Range(.Cells(2, 1), .Cells(lastrowsheet1, 4)).SpecialCells(xlCellTypeVisible).Copy
.AutoFilterMode = False
End With
With ThisWorkbook.Sheets("Sheet2")
.AutoFilterMode = False
lastrowsheet2 = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(lastrowsheet2 + 1, 1).PasteSpecial Paste:=xlPasteValues
.AutoFilterMode = False
End With
Application.CutCopyMode = False
End Sub
EDIT:
Trying to stick close to your original code, would something like this be more like what you are looking for?
Sub TakeTwo()
Dim a As Worksheet
Dim b As Worksheet
Dim c As Worksheet
Set a = ThisWorkbook.Sheets("Sheet1")
Set b = ThisWorkbook.Sheets("Sheet2")
Set c = ThisWorkbook.Sheets("Sheet3")
Dim i As Long
Dim lastrowsheet1 As Long
Dim erow As Long
lastrowsheet1 = a.Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrowsheet1
If a.Cells(i, 1).Value = 1991 Then
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
b.Cells(erow, 1) = a.Cells(i, 1)
b.Cells(erow, 2) = a.Cells(i, 2)
b.Cells(erow, 3) = a.Cells(i, 3)
b.Cells(erow, 4) = a.Cells(i, 4)
End If
Next i
Application.CutCopyMode = False
b.Columns.AutoFit
End Sub
SECOND EDIT - OP's NEW PROBLEM:
It looks like your data is just pasting over itself because erow is defined as the row after the last row in column 1 that is not empty, but you are not actually putting any data into that column, so erow isn't moving down to the next line.
Basically, change the column number in this line:
erow = b.Cells(b.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
The 1 in b.Cells(b.Rows.Count, 1) should be changed to a column number that you paste data into every time. Alternatively, you could use erow as a counter and increment it manually each time through the loop. In that case move the existing line that defines erow up underneath the line that defines lastrowsheet1 and then put erow = erow + 1 inside the loop after all the pasting has taken place but before End if. If you put it after End If, you'll end up with a bunch of blank lines between your data.

Resources