copy row to new worksheet to next available row - excel

How would you copy a row and paste it to the next available row to another worksheet. Currently the code below only pastes according to the entry row of the mastersheet.
Sub ShowMonth()
Dim k As Long
For k = 2 To 9999
Cells(k, 14).Value = Month(Cells(k, 1).Value)
Next k
End Sub
Private Sub Move()
Dim MonthNo As Range
Dim lastrow, j As Long
Set MonthNo = Worksheets("MasterSheet").Range("N2:N9999")
lastrow = Worksheets("MasterSheet").Cells(Rows.Count, "A").End(xlUp).Row
For j = 2 To 9999
If MonthNo(j) = 1 Then
lastrow = lastrow + 1
MonthNo(j).Rows.EntireRow.Copy Destination:=Worksheets("Jan").Range("A" & lastrow)
ElseIf MonthNo(j) = 2 Then
lastrow = lastrow + 1
MonthNo(j).Rows.EntireRow.Copy Destination:=Worksheets("Feb").Range("A" & lastrow)
End If
Next
End Sub

I have commented the code so that you should not have a problem understanding the code.
Option Explicit
Private Sub Move()
Dim wsLrow As Long, lastrow As Long, i As Long
Dim ws As Worksheet
Dim wsDest As String
On Error GoTo Whoa
'~~> Set your worksheet
Set ws = Worksheets("MasterSheet")
With ws
'~~> Find the last row in Col N
wsLrow = .Cells(.Rows.Count, "N").End(xlUp).Row
'~~> Loop through the cells in Col N
For i = 2 To wsLrow
Select Case .Range("N" & i).Value2
Case 1: wsDest = "Jan"
Case 2: wsDest = "Feb"
Case 3: wsDest = "Mar"
'
' And so on. Add more if applicable
'
End Select
If wsDest <> "" Then
With Worksheets(wsDest)
'~~> Find the row in the destination worksheet to copy
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
'~~> Copy the row from MasterSheet to relevant sheet
ws.Rows(i).Copy Destination:=.Rows(lastrow)
End With
wsDest = ""
End If
Next i
End With
LetsContinue:
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub

Related

Copy last 3 rows, excluding the rows for which there is a "0" in column "C"

I have a problem.
I want to find the last row in another file and sheet and copy the last 3 rows from A-AD, except those that have a "0" in column "C". I want the number of copied rows to always be 3.
I have a problem with the code below because it always only copies just one row in the end.
Sub AB ()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Dim numCopied As Long
Dim baseWB As Workbook, baseWS As Worksheet
Dim spWB As Workbook, spWS As Worksheet
Set baseWB = ThisWorkbook
Set baseWS = ActiveSheet
lastRow = spWS.Cells(spWS.Rows.Count, "D").End(xlUp).Row
numCopied = 0
For i = lastRow To lastRow - 8 Step -1
' Sprawdź, czy w kolumnie C jest 0
If spWS.Cells(i, "C").Value <> 0 Then
spWS.Range(spWS.Cells(i, "A"), spWS.Cells(i, "AD")).Copy
numCopied = numCopied + 1
End If
If numCopied = 3 Then
Exit For
End If
Next i
baseWB.Sheets("Sheet1").Range("E5").PasteSpecial xlPasteValues
spWB.Close SaveChanges:=False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
The copy inside the loop is overwriting the previous copy. They are not additive unless you use Union.
Option Explicit
Sub AB()
Dim spWB As Workbook, spWS As Worksheet
Dim baseWB As Workbook, baseWS As Worksheet
Dim rng As Range, rngCopy As Range
Dim lastRow As Long, i As Long, numCopied As Long
Set baseWB = ThisWorkbook
Set baseWS = baseWB.Sheets("Sheet1")
' open workbook to copy from
Set spWB = Workbooks.Open("Source.xlsx", ReadOnly:=True)
Set spWS = spWB.Sheets("Sheet1")
numCopied = 0
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With spWS
lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
For i = lastRow To 1 Step -1
' Sprawdz, czy w kolumnie C jest 0
If .Cells(i, "C").Value <> 0 Then
Set rng = .Cells(i, "A").Resize(, 30) ' A:AD
If rngCopy Is Nothing Then
Set rngCopy = rng
Else
Set rngCopy = Union(rng, rngCopy)
End If
numCopied = numCopied + 1
End If
If numCopied = 3 Then
Exit For
End If
Next i
End With
' copy
If rngCopy Is Nothing Then
MsgBox "No rows found to copy", vbExclamation
Else
rngCopy.Copy
baseWS.Range("E5").PasteSpecial xlPasteValues
Application.CutCopyMode = False
MsgBox " Copied : " & rngCopy.Address, vbInformation
End If
spWB.Close SaveChanges:=False
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub

How to copy rows and paste them into a sheet given a cell value

I have data in a table, where I compare two columns J and T. The values which J and T can take include A2B, APL, BGF, CMA, among others (see code).
If these values are equal, copy row i into the sheet which has the name of the cells just checked.
If these values are not equal, copy row i into the sheets which have the name of the cells just checked.
Example: Compare J2 and T2,
Suppose J2=T2=BGF then copy row 2 and paste into sheet("BGF")
Next, compare J3 and T3
Suppose J3=BGF and T3=CMA, copy row 3 and paste into sheet(BGF) and sheet(CMA)
Next, compare J4 and T4
Suppose J4=Nothing and T4=CMA, copy row 4 and paste into sheet CMA
the only other combination is where Ji has a value and Ti is empty.
Problem: When running this code, If J3=BGF and T3= nothing (its empty), then the line is not copied to any sheet.
Here's the code
Sub Sortdata()
'step 1 clear all data
Sheets("A2B").Cells.ClearContents
Sheets("APL").Cells.ClearContents
Sheets("BGF").Cells.ClearContents
Sheets("CMA").Cells.ClearContents
Sheets("K Line").Cells.ClearContents
Sheets("MacAndrews").Cells.ClearContents
Sheets("Maersk").Cells.ClearContents
Sheets("OOCL").Cells.ClearContents
Sheets("OPDR").Cells.ClearContents
Sheets("Samskip").Cells.ClearContents
Sheets("Unifeeder").Cells.ClearContents
Dim i As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim LastRow As Long
With Worksheets("All Data")
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To LastRow
If IsEmpty(.Range("J" & i)) Then
Set ws1 = Nothing
Else
Set ws1 = Worksheets(.Range("J" & i).Value)
End If
If IsEmpty(.Range("T" & i)) Then
Set ws2 = Nothing
Else
Set ws2 = Worksheets(.Range("T" & i).Value)
End If
If ws1 Is Nothing Then
If Not ws2 Is Nothing Then
CopyToWs ws2, .Rows(i)
End If
ElseIf ws2 Is Nothing Then
If Not ws1 Is Nothing Then
CopyToWs ws1, .Rows(i)
End If
Else
CopyToWs ws1, Rows(i)
If ws1.Name <> ws2.Name Then
CopyToWs ws2, .Rows(i)
End If
End If
Next
End With
End Sub
Sub CopyToWs(ws As Worksheet, rng As Range)
rng.Copy
ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow.PasteSpecial xlPasteValuesAndNumberFormats
End Sub
Please try this code. It takes a slightly different approach to what you tried but it gets the job done, I think.
Option Explicit
Sub Sortdata()
' Variatus #STO 20 Jan 2020
Const WsNames As String = "A2B,APL,BGF,CMA,K Line,MacAndrews," & _
"Maersk,OOCL,OPDR,Samskip,Unifeeder"
Dim WsS As Worksheet ' Source
Dim Ws As Worksheet
Dim Rng As Range
Dim Rt As Long ' target row
Dim LastRow As Long
Dim J As Long, T As Long
Dim Tmp As Variant, PrevTmp As Variant
Dim R As Long, C As Long
'step 1 clear all data
Tmp = Split(WsNames, ",")
For R = LBound(Tmp) To UBound(Tmp)
On Error Resume Next
Worksheets(Tmp(R)).Cells.ClearContents
Next R
Application.ScreenUpdating = False
Set WsS = Worksheets("All Data")
With WsS
J = .Columns("J").Column
T = .Columns("T").Column
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For R = 2 To LastRow
PrevTmp = ""
For C = J To T Step T - J
Tmp = .Cells(R, C).Value
If Len(Tmp) And Tmp <> PrevTmp Then
On Error Resume Next
Set Ws = Worksheets(Tmp)
If Err = 0 Then
Set Rng = .Range(.Cells(R, 1), .Cells(R, .Columns.Count).End(xlToLeft))
With Ws
Rt = Application.Max(.Cells(.Rows.Count, "A").End(xlUp).Row + 1, 2)
Rng.Copy Destination:=Ws.Cells(Rt, 1)
End With
End If
End If
PrevTmp = Tmp
Next C
If R Mod 25 = 0 Then Application.StatusBar = "Currently processing row " & R
Next R
End With
With Application
.ScreenUpdating = True
.StatusBar = "Done"
End With
End Sub
I think you will be able to find your way through it and make any required modifications. Let me know if you require any assistance.

Copy Cell to another sheet in condition is met Loop

I need to loop through a column and if a conditions if met copy cell from one sheet to another.
I'm finding problems with the incremental..
In this case double the results.
Thank you in advance.
KR
Sub copycell()
Dim iLastRow As Long
Dim i As Long
Dim erow As Long
erow = 1
iLastRow = Worksheets("Clientes").Cells(Rows.Count, "C").End(xlUp).Row
For i = 13 To iLastRow
If Sheets("Clientes").Cells(i, 3) = "0" Then
Worksheets("Ficheros").Range("B" & erow).End(xlUp).Offset(1) = Sheets("Clientes").Cells(i, 4)
erow = erow + 1
End If
Next i
End Sub
Why not use Autofilter to filter the column C and if the autofilter returns any rows, copy them to the destination sheet?
See if something like this works for you...
Sub CopyCells()
Dim wsData As Worksheet, WsDest As Worksheet
Dim iLastRow As Long
Application.ScreenUpdating = False
Set wsData = Worksheets("Clientes")
Set WsDest = Worksheets("Ficheros")
iLastRow = wsData.Cells(Rows.Count, "C").End(xlUp).Row
wsData.AutoFilterMode = False
With wsData.Rows(12)
.AutoFilter field:=3, Criteria1:="0"
If wsData.Range("D12:D" & iLastRow).SpecialCells(xlCellTypeVisible).Cells.Count > 1 Then
wsData.Range("D13:D" & iLastRow).SpecialCells(xlCellTypeVisible).Copy
WsDest.Range("B" & Rows.Count).End(3)(2).PasteSpecial xlPasteValues
End If
End With
wsData.AutoFilterMode = False
Application.CutCopyMode = 0
Application.ScreenUpdating = True
End Sub
You can achieve your result with AutoFilter, but my answer is trying to resolve your code using the For loop.
Modified Code
Option Explicit
Sub copycell()
Dim iLastRow As Long
Dim i As Long
Dim erow As Long
' get first empty row in column B in "Ficheros" sheet
erow = Worksheets("Ficheros").Range("B" & Rows.Count).End(xlUp).Row + 1
With Worksheets("Clientes")
iLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
For i = 13 To iLastRow
If .Cells(i, 3) = "0" Then
Worksheets("Ficheros").Range("B" & erow) = .Cells(i, 4)
erow = erow + 1
End If
Next i
End With
End Sub

Copy & paste each unique value from one sheet to another

I may have up to 8 unique values in column D. I am looking for a code that will copy & paste each row with unique value to a new sheet.
So I may have up to 8 new sheets.
Could you help me to build the code that will do that?
This is what I have so far:
Option Explicit
Sub AddInstructorSheets()
Dim LastRow As Long, r As Long, iName As String
Dim wb As Workbook, ws As Worksheet, ts As Worksheet, nws As Worksheet
Dim i As Integer
Dim m As Integer
'set objects
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Set ts = Sheets("Master")
'set last row of instructor names
LastRow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row
'add instructor sheets
On Error GoTo err
Application.ScreenUpdating = False
For r = 17 To LastRow 'assumes there is a header
iName = ws.Cells(r, 4).Value
With wb 'add new sheet
ts.Copy After:=.Sheets(.Sheets.Count) 'add template
Set nws = .Sheets(.Sheets.Count)
nws.Name = iName
Worksheets(iName).Rows("17:22").Delete
Worksheets("Master").Activate
Range(Cells(r, 2), Cells(r, 16)).Select
Selection.Copy
m = Worksheets(iName).Range("A15").End(xlDown).Row
Worksheets(iName).Cells(m + 1, 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End With
Next r
err:
ws.Activate
Application.ScreenUpdating = True
End Sub
The thing is that this macro is creating new sheets, which is not necessary. I only want to make following.
If you find a unique value in column D (which will have exact name as other sheet), find this sheet and paste whole row in there.
Sub CopyFromColumnD()
Dim key As Variant
Dim obj As Object
Dim i As Integer, lng As Long, j As Long
Dim sht As Worksheet, mainsht As Worksheet
Set obj = CreateObject("System.Collections.ArrayList")
Set mainsht = ActiveSheet
With mainsht
lng = .Range("D" & .Rows.Count).End(xlUp).Row
With .Range("D1", .Range("D" & lng))
For Each key In .Value
If Not obj.Contains(key) Then obj.Add key
Next
End With
End With
For i = 0 To obj.Count - 1
Set sht = Sheets.Add(After:=Sheets(Sheets.Count))
sht.Name = obj(i)
For j = 1 To lng
If mainsht.Cells(j, 4).Value = obj(i) Then
mainsht.Rows(j).EntireRow.Copy Destination:=Range("A1")
Exit For
End If
Next
Next
End Sub
Ok, I did the workaround. I have created a list of unique values in a separate sheet.
Sub copypaste()
Dim i As Integer
Dim j As Integer
LastRow = Worksheets("Master").Range("D17").End(xlDown).Row
For i = 17 To LastRow
For j = 2 To 10
Workstream = Worksheets("Database").Cells(j, 5).Value
Worksheets("Master").Activate
If Cells(i, 4) = Worksheets("Database").Cells(j, 5).Value Then
Range(Cells(i, 2), Cells(i, 16)).Select
Selection.Copy
Worksheets(Workstream).Cells(1, 1).PasteSpecial Paste:=xlPasteValues
Else
End If
Next j
Next i
End Sub
Thank you everyone for help and your time!

How can I hide zero rows in every worksheet in Excel

I want to hide rows that have a zero values in columns B & C. My code works but only on the active worksheet. I want it to loop through all worksheets in the workbook. Any help is appreciated
Private Sub CommandButton1_Click()
Dim M As Long, LastRow As Long
Dim ws As worksheet
For Each ws In ActiveWorkbook.Worksheets
LastRow = ws.Range("E65536").End(xlUp).Row
For M = LastRow To 7 Step -1
If Range("B" & M).Value = 0 And Range("C" & M).Value = 0 Then
Range("B" & M).EntireRow.Hidden = True
End If
Next M
Next ws
End Sub
activate the sheet. The issue is that Range is working off the current active sheet. If you use ws. infromt of range or activate the worksheet such as below.
For Each ws In ActiveWorkbook.Worksheets
add
ws.Activate
I found this about the subject, I hope it helps: ExtendOffice
Sub Hide_rows()
Dim LastRow As Long
Dim Rng As Range
LastRow = Range("A65536").End(xlUp).Row '
Set Rng = Range("A1:A" & LastRow) '
Application.ScreenUpdating = False
For Each cell In Rng
If cell.Value = "0" Then
cell.EntireRow.Hidden = True
End If
Next cell
Application.ScreenUpdating = True
End Sub
You need to specify the Range as a member of the worksheet like so:
Private Sub CommandButton1_Click()
Dim M As Long, LastRow As Long
Dim ws As worksheet
For Each ws In ActiveWorkbook.Worksheets
LastRow = ws.Range("E65536").End(xlUp).Row
For M = LastRow To 7 Step -1
' Notice how 'ws' has been added before range
If ws.Range("B" & M).Value = 0 And ws.Range("C" & M).Value = 0 Then
ws.Range("B" & M).EntireRow.Hidden = True
End If
Next M
Next ws
End Sub

Resources