TessieBear99
New Member
- Joined
- Aug 26, 2018
- Messages
- 20
- Office Version
- 365
- Platform
- Windows
Hi all, I'm hoping I can word this clearly because it's a bit of a muddle in my head.
I'm building a spreadsheet which has two sheets with a table on each sheet. At the end of each month, the user needs to be able to copy specific information from Sheet1 (called Planning) to Sheet2 (called Unplanned). I've created a button to allow this to be automated and it's almost working, but not quite.
Here is what needs to happen:
All rows in the table in Sheet1 which have the word "Unplanned" in column H need to be copied from column A:D and pasted into the first available row in the Sheet2 table.
Here is what is currently happening:
Only the last row in Sheet1 where the word "Unplanned" appears in column H is being selected and copied across to Sheet2.
Here is the code:
I'm a beginner at this so I've figured bits and pieces out here and there. Any help would be greatly appreciated!
I'm building a spreadsheet which has two sheets with a table on each sheet. At the end of each month, the user needs to be able to copy specific information from Sheet1 (called Planning) to Sheet2 (called Unplanned). I've created a button to allow this to be automated and it's almost working, but not quite.
Here is what needs to happen:
All rows in the table in Sheet1 which have the word "Unplanned" in column H need to be copied from column A:D and pasted into the first available row in the Sheet2 table.
Here is what is currently happening:
Only the last row in Sheet1 where the word "Unplanned" appears in column H is being selected and copied across to Sheet2.
Here is the code:
VBA Code:
Private Sub cmdTransferUnplanned_Click()
' Start of question box and screen updating
Dim answer As Integer
answer = MsgBox("Are you sure?", vbQuestion + vbYesNo + vbDefaultButton2, "Transfer Unplanned")
If answer = vbNo Then
Exit Sub
End If
Application.ScreenUpdating = False
' Find, copy and paste data across
Dim xWs As Worksheet
Dim xCWs As Worksheet
Dim xRg As Range
Dim xRRg As Range
Dim xC As Integer
Dim xStr As String
Dim LastCell As Range
Dim LastCellColRef As Long
Dim Destination As Range
'Find first available empty row
LastCellColRef = 1 'column number to look in when finding last cell
If (Sheet2.Cells(Rows.Count, LastCellColRef).End(xlUp)) <> "" Then
Set LastCell = Sheet2.Cells(Rows.Count, LastCellColRef).End(xlUp).Offset(1, 0)
Else
Set LastCell = Sheet2.Cells(Rows.Count, LastCellColRef).End(xlUp)
End If
Set Destination = LastCell
'Find instances of "Unplanned" and copy across
Set xWs = ActiveWorkbook.Worksheets("Planning")
Set xCWs = ActiveWorkbook.Worksheets("Unplanned")
Set xRg = xWs.Range("H:H")
Set xRg = Intersect(xRg, xWs.UsedRange)
xStr = "Unplanned"
On Error Resume Next
For Each xRRg In xRg
If xRRg.Value = xStr Then
Intersect(xRRg.EntireRow, xWs.Range("A:D")).Copy
Destination.PasteSpecial xlPasteValues
End If
Next
Application.CutCopyMode = False
' End of question box and screen updating
Application.ScreenUpdating = True
answer = MsgBox("Done!")
End Sub
I'm a beginner at this so I've figured bits and pieces out here and there. Any help would be greatly appreciated!