Please help me fix a macro to paste completed items (Column A) to the next sheet (Completed Tab) then delete it from 1st sheet (Email Tracker) once transferred to Completed Tab. It's behaving like this:
-overwrites the contents in Completed tab, instead of adding additional entries to it
Below is my code.
Sub Clear()
'
' Clear Macro
'
' Keyboard Shortcut: Ctrl+Shift+G
'
Sheets("Email Tracker").Select
Range("A1").AutoFilter Field:=1, Criteria1:= _
"Completed"
ActiveSheet.AutoFilter.Range.Copy
Sheets("Completed").Select
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Email Tracker").Select
Range("A1").Select
Application.CutCopyMode = False
ActiveSheet.ShowAllData
End Sub
When you use the macro recorder you should always try to clean up the code and remove "Select". I see where you tried to add last row, but didn't know how to apply it. The code below first sets up the last row (LR) for your destination sheet, so you can properly paste your "completed" emails to the first empty row after the last row in the Destination sheet. Next, it copies your headers from the source sheet to destination sheet, because I don't know if they are already in the destination sheet. Next it filters your source sheet and copies the visible data to your destination sheet, pasting the data to the first empty row below the last row. Then it will delete the visible data on your filtered source sheet. Finally it removes the filter in your source sheet.
Dim LR As Long
With Worksheets("Email Tracker")
Rows(1).EntireRow.Copy Sheets("Completed").Range("A1")
LR = Sheets("Completed").Cells(Rows.Count, 1).End(xlUp).Row
.AutoFilterMode = False
With Range("A1").CurrentRegion
.AutoFilter Field:=1, Criteria1:="Completed"
On Error Resume Next
.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Completed").Cells(LR + 1, 1)
.Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
.AutoFilterMode = False
End With
Related
Please help me with a macro for copying and pasting text from Input sheet (worksheet1) to the Reports sheet (worksheet 2) within the same excel file.
Input Sheet Format:
Name- cell J5
Date- cell J6
Start Time- cell J7
End Time- cell J8
Downtime- cell J9
I need a macro for copying J5:J9 and paste it in A2:E2, using transpose function.
Also, the sheet is shared with multiple users so it needs to be pasted in the last available row in the Reports sheet (worksheet 2).
Presently, I am using the following macro code:
Sub Report()
Sheets("Input").Select
Range("J5:J9").Select
Selection.Copy
Sheets("Reports").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Sheets("Input").Select
ActiveWindow.SmallScroll Down:=-5
Sheets("Input").Select
Range("J5:J9").Select
Selection.ClearContents
End Sub
I am getting error at "ActiveCell.Offset(1, 0).Select" while running the macro.
Please advise any changes to the code which can help me fixing the issue.
Thanks
You can rewrite everything you're doing in a few lines:
Sub Report()
' Copy the range from J5:J9 on INPUT worksheet
ThisWorkbook.Worksheets("Input").Range("J5:J9").Copy
With ThisWorkbook.Worksheets("Reports")
' Find the LAST ROW in COLUMN A
Dim lROW As Long: lROW = .Cells(.Rows.Count, 1).End(xlUp).Row
' Paste in cell A-lROW with transpose
.Range("A" & lROW).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, Transpose:=True
End With
' Clear entry in original INPUT sheet
ThisWorkbook.Worksheets("Input").Range("J5:J9").ClearContents
End Sub
I'm not sure why you're getting an error for offsetting by one row, it works for me - but it's probably to do with your method of "selecting" the last row
I'm fairly new to VBA user-forms so hopefully it's an easy fix.
I am using this code to move my entries from one sheet to another within the same workbook,but its working with some errors.
*I want it work on a specific sheet but its working on the active sheet.
**I want that after moving entries it should auto clear the specific sheet ( and I don't know how to do that :( )
Here is my code:
Private Sub CommandButton8_Click() 'Move Button
For Each cell In ThisWorkbook.Sheets("Daily").Range("endRange")
If IsDate(cell) = True Then
myEndRow = cell.Row
End If
Next cell
ThisWorkbook.Worksheets("Daily").Range("A2:E10000" & myEndRow).Select
Selection.Copy
Sheets("Data").Select
'Range("A2660").Select
ThisWorkbook.Worksheets("Data").Range("a99999").End(xlUp).Select
ActiveCell(2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub
Here is the link for the file:
Link
In your daily sheet in the code associated with the button put the following.
Please note i am not sure of the purpose of your test to see if there is a date. If you can clarify this i can amend the code accordingly. You don't need a button in the data sheet as this is where you are copying to. Make sure this code only resides in the sheet associated with the button i.e. Daily and does not exist elsewhere in the workbook.
Private Sub CommandButton1_Click() 'Move Button
Dim wb As Workbook
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Dim rangeToCopy As Range
Set wb = ThisWorkbook
Set wsSource = wb.Worksheets("Daily")
Set wsTarget = wb.Worksheets("Data")
Dim NextRow As Long
NextRow = wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Row + 1 'Find next free row in Data sheet
Set rangeToCopy = wsSource.Range("A1").CurrentRegion.Offset(1, 0) 'get current set of rows that have data excluding header in daily sheet
rangeToCopy.Copy wsTarget.Cells(NextRow, "A") 'copy the new data from daily across to the next free row in the data sheet
rangeToCopy.ClearContents 'clear the contents of the daily sheet under the header
End Sub
I am looking for advice on how to write a macro that does the below. I imagine its easy to do, but I can't figure it out. Thanks in advance!
START
In the active sheet (in the workbook I am running this macro in [Title changes but same formatting each time]), copy cell B9. Paste in column A on the next blank row of the other workbook I am using [Can have the same title every time I run this process, or just be the only other workbook open]
In the active sheet (in the workbook I am running this macro in), copy cell B8. Paste in column B of the row identified above.
In the active sheet (in the workbook I am running this macro in), copy cell B12. Paste in column C of the row identified above.
In the active sheet (in the workbook I am running this macro in), copy cells A17:E17. Paste in D:H of the row identified above.
In the active sheet (in the workbook I am running this macro in), copy cells A17:E17. Paste in D:H of the row identified above.
In the active sheet (in the workbook I am running this macro in), copy cells G17:N17. Paste in I:P of the row identified above.
END
Given my lack of vba coding ability I'm trying to record a macro and then adjust. I've tried as many options as I can find on google. The below seems to be the best, but doesn't work. (NB: I start with B9 from point 1 above selected).
Sub Copy_Timesheet()
'
' Copy_Timesheet Macro
'
'
Selection.Copy
Windows("WorkbookB").Activate
Find_Blank_Row()
Dim BlankRow As Long
BlankRow = Range("A65536").End(xlUp).Row + 1
Cells(BlankRow, 1).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
ActiveCell.Offset(3, 0).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("WorkbookB").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
ActiveCell.Offset(-4, 0).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("WorkbookB").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
ActiveCell.Offset(9, -1).Range("A1:E1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("WorkbookB").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
ActiveCell.Offset(0, 6).Range("A1:H1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("WorkbookB").Activate
ActiveCell.Offset(0, 5).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Now that you have shown some effort in generating the code, here is a refactored version of what you said you were after. (I didn't check to see whether that matched what you actually recorded, but the fact that you went to the trouble of recording something indicated that you weren't just too lazy to do this yourself.)
Sub Copy_Timesheet()
'Set up some objects to make life easier in the rest of the code
' "the active sheet (in the workbook I am running this macro in)"
Dim wsSrc As Worksheet
Set wsSrc = ThisWorkbook.ActiveSheet
'the sheet in the other workbook
Dim wsDst As Worksheet
Set wsDst = Workbooks("WorkbookB").Worksheets("destination_sheet_name") 'change sheet name to whatever you need
Dim BlankRow As Long
'Fully qualify ranges so that we ensure we are working with the sheet we expect to be
'Use Rows.Count rather than 65536 just in case we are working in a recent workbook that allows 1048576 rows
BlankRow = wsDst.Range("A" & wsDst.Rows.Count).End(xlUp).Row + 1
'In the active sheet (in the workbook I am running this macro in [Title changes but same formatting each time]), copy cell B9. Paste in column A on the next blank row of the other workbook I am using [Can have the same title every time I run this process, or just be the only other workbook open]
wsDst.Range("A" & BlankRow).Value = wsSrc.Range("B9").Value
'In the active sheet (in the workbook I am running this macro in), copy cell B8. Paste in column B of the row identified above.
wsDst.Range("B" & BlankRow).Value = wsSrc.Range("B8").Value
'In the active sheet (in the workbook I am running this macro in), copy cell B12. Paste in column C of the row identified above.
wsDst.Range("C" & BlankRow).Value = wsSrc.Range("B12").Value
'In the active sheet (in the workbook I am running this macro in), copy cells A17:E17. Paste in D:H of the row identified above.
wsDst.Range("D" & BlankRow & ":H" & BlankRow).Value = wsSrc.Range("A17:E17").Value
'In the active sheet (in the workbook I am running this macro in), copy cells A17:E17. Paste in D:H of the row identified above.
'No need to do this - we just did it
'In the active sheet (in the workbook I am running this macro in), copy cells G17:N17. Paste in I:P of the row identified above.
wsDst.Range("I" & BlankRow & ":P" & BlankRow).Value = wsSrc.Range("G17:N17").Value
End Sub
Sub copysheet()
Dim wb As Workbook
Dim wb1 As Workbook
application.screenupdating=False
application.DisplayAlerts=False
On error goto resetsettings
MyPath = "C:\Users\foo\" 'The folder containing the files you want to use
MyExtension = "*.xlsx" 'The extension of the file you want to use
Myfile = Dir(MyPath & MyExtension)
Set wb = ThisWorkbook
While Myfile <> ""
Set wb1 = Workbooks.Open(MyPath & Myfile)
lr = wb1.Sheets(1).Range("A1:A" & Rows.Count).End(xlUp).Row + 1
wb.Sheets(1).Range("B9").Copy Destination:=wb1.Sheets(1).Range("A" & lr)
wb.Sheets(1).Range("B8").Copy Destination:=wb1.Sheets(1).Range("B" & lr)
wb.Sheets(1).Range("B12").Copy Destination:=wb1.Sheets(1).Range("C" & lr)
wb.Sheets(1).Range("A17:E17").Copy Destination:=wb1.Sheets(1).Range("D" & lr & ":H" & lr)
wb.Sheets(1).Range("G17:N17").Copy Destination:=wb1.Sheets(1).Range("I" & lr & ":P" & lr)
wb1.close Savechanges:=True
Myfile = Dir
Wend
ResetSettings:
application.screenupdating=True
application.DisplayAlerts=True
End Sub
This Macro will loop through all Xlsx Files in a folder and make the above changes in the files and closes them.
I want to select specific cells from all the worksheets present in my Excel workbook and then paste in a master sheet. Problem is I am not getting that from the code created, I get an error but if I leave it as it is right now (shown below) I get it for a specific cell and then I have to go into the code to change the cell and where I want it outputted to. I apologize in advance for my naivety.
As it is right now
Sub CopyIt()
Dim ws As Worksheet
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Masters" Then
ws.Range("B18").Copy Sheets("Masters").Cells(Rows.Count, "Q").End(xlUp).Offset(1)
End If
Next
Application.ScreenUpdating = True
End Sub
I want this cell range "B2-B18" to be copied to "A:Q" and in the master sheet. So values in B2 go to A column and so on and so and then at the end B18 goes to Q.
What did I not do for the code to do what it should?
Hey I just tested this and it should do the trick for you
Sub CopyIt()
Dim pasteRow As Integer
Dim ws As Worksheet
Application.ScreenUpdating = False
pasteRow = 2
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Masters" Then
ws.Range("B2", "B18").Copy
Sheets("Masters").Range("A" & pasteRow).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
pasteRow = pasteRow + 1
End If
Next
Application.ScreenUpdating = True
End Sub
This will advance a row for each worksheet so you can add as many worksheets as you like. Note that this really isn't the most universal code, you would need to change the ws.Range("B2", "B18").copy to something that would select say, all ranges in a column or you will have to manually expand the range each time you want to change it.
Try:
ws.Range("B1:B18").Copy
Sheets("Masters").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
That should copy B1:B18, transpose it from columns to rows and paste it in the last row in Column A of your Masters sheet.
enable developer toolbar
select record a macro
select b2:b18 in a one sheet select another sheet and right click paste special all and also select transpose
stop recording macro
now edit the macro to suit your requirements
a sample macro autogenerated code as follows
Sub Macro1()
'
' Macro1 Macro
'
'
Range("B2:B18").Select
Selection.Copy
Sheets("Sheet2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
End Sub
This should do the required...
(This will copy paste values from B2:B18 cells in each sheet to different rows in the Sheet "Masters")
Sub Macro1()
Dim ws As Worksheet
Dim row_count As Integer
row_count = 1
For Each ws In ActiveWorkbook.Worksheets
MsgBox ws.name
If ws.name <> "Masters" Then
ws.Activate
Range("B2:B18").Select
Selection.Copy
Sheets("Masters").Activate
Range("A" & row_count).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Application.CutCopyMode = False
row_count = row_count + 1
End If
Next
End Sub
I have a workbook that serves as source data for another Excel-based form (not a Userform, just a formatted spreadsheet). The source file has anywhere from 2-40 rows of data - starting from row 18 - and each row needs to be copied into the form and saved separately, i.e. 15 rows from the Source file equates to 15 distinct Form files.
Each cell within the row must be copied separately and pasted to specific cells on the Form. The Source form contains Clients and their relevant info. I am trying to use a macro on the Form to automatically pull line items from the Source file, save the Form as the client's name in a specified folder, and continue until a blank row is reached on the Source file. I have some basic VBA experience, but have little knowledge of loops, variables, or functions, which seem to be my best course of action here.
Here's what I have so far. All I've been able to accomplish is the copy/pasting of the first row from the Source file.
Range("B18").Select
Selection.Copy
Windows("Form.xls").Activate
Range("F7:K7").Select
ActiveSheet.Paste
Windows("Source.xls").Activate
Range("C18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Processing Form.xls").Activate
Range("D8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Source.xls").Activate
Range("D18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Processing Form.xls").Activate
Range("H29").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Source.xls").Activate
Range("E18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Processing Form.xls").Activate
Range("E29").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("Source.xls").Activate
Range("F18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Processing Form.xls").Activate
Range("D33").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range(“F7:K7”).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWorkbook.SaveAs
I can't even get my macro to save the Form by the client's name. I also know that my extensive use of "Range" and "Select" will slow my code down to a crawl, I just don't know how to make it more efficient. I've tried using a reference cell to that tells the macro which row of the Source file to copy but haven't had any luck down that road either. Any and all help will be greatly appreciated!
Here's a simple demo:
Note: Not Tested
Option Explicit
Sub CopyToForm()
Dim wbSource As Workbook, wbForm As Workbook
Dim wsSource As Worksheet, wsForm As Worksheet
Dim formpath As String, foldertosavepath as string
Dim lrow As Long, i As Integer
Set wbSource = Thisworkbook '~~> assuming you write your code in Source.xls
Set wsSource = wbSource.Sheets("NameOfYourSheet") '~~> put the source sheet name
'~~> put the path where your form template is saved here
formpath = "C:\Users\Username\FolderName\Processing Form.xls"
'~~> put the path where you want to save individual updated forms.
foldertosavepath = "C:\Users\Username\FolderDestination\"
With wsSource
'~~> get the number of rows with data
lrow = .Range("B" & .Rows.Count).End(xlUp).Row
If lrow < 18 Then Msgbox "No data for transfer": Exit Sub
For i = 18 to lrow
Set wbForm = Workbooks.Open(formpath) '~~> open the form
Set wsForm = wbForm.Sheets("Sheetname") '~~> put the form sheet name
'~~> proceed with the copying
.Range("B" & i).Copy: wsForm.Range("F7:K7").PasteSpecial xlPasteValue
.Range("C" & i).Copy: wsForm.Range("D8").PasteSpecial xlPasteValues
.Range("D" & i).Copy: wsForm.Range("H29").PasteSpecial xlPasteValues
.Range("E" & i).Copy: wsForm.Range("E29").PasteSpecial xlPasteValues
.Range("F" & i).Copy: wsForm.Range("D33").PasteSpecial xlPasteValues
'~~> Save the form using the client name, I assumed it is in B?
wbForm.SaveAs foldertosavepath & .Range("B" & i).Value & ".xls"
wbForm.Close True
Set wbForm = Nothing
Set wsForm = Nothing
Next
End With
End Sub
In above code, I assumed that Form.xls is the same as Processing Form.xls.
This should give you the logic.
I hope this get's you started.
This is not test as I've noted, so if you encounter errors, comment it out.
You are frequently activating the workbooks. that's your code slows down..Below code will work faster
Sub test()
Dim dwb As Workbook
Dim swb As Workbook
Set dwb = Workbooks("Form.xls")
Set swb = Workbooks("Source.xls")
Set awb = Workbooks("Processing Form.xls")
With swb
.ActiveSheet.Range("B18").Copy Destination:=dwb.Sheet1.Range("F7:K7")
.ActiveSheet.Range("C18").Copy Destination:=awb.Sheet1.Range("D8")
.ActiveSheet.Range("D18").Copy Destination:=awb.Sheet1.Range("H29")
.ActiveSheet.Range("e18").Copy Destination:=awb.Sheet1.Range("E29")
.ActiveSheet.Range("F18").Copy Destination:=awb.Sheet1.Range("D33")
End With
End Sub
This might help steer you in the right direction:
Dim i As Long
For i = 1 To 10
With Range("A" & i)
.Copy Workbooks("ToWorkbook.xlsx").Worksheets("Sheet1").Range("B" & i + 9)
.Copy Workbooks("ToAnother.xlsx").Worksheets("Sheet2").Range("C" & i + 8)
.Copy Workbooks("AnotherOne.xlsx").Worksheets("SheetA").Range("D" & i + 2)
End With
Next i
i To 10 is used as a counter to loop through the rows in the source workbook.
For each i, you're taking the range from column A (i.e., with this, do something), copying and pasting it into different cells in different workbooks. In the first round, Range("A1") is being copied into 3 different workbooks at Range("B10"), Range("C9") and Range("D3"), respectively. The next turn, Range("A2") from the source book is going to be copied and pasted into the same destination workbooks from last time, but in Range("B11"), Range("C10") and Range("D4"). It's just a matter of finding the pattern for the different forms you need to paste into.