I am writing a macro to copy some data from one sheet to another but I keep getting an error 1004 "application-defined or object-defined error" on Range("A1").End(xlDown).Offset(1, 0).Select and I cannot figure out why. The sheet isn't locked or protected and, as far as I can tell, the syntax is correct on everything. Any ideas on why I might be having this issue? Code below.
Sub TransferData()
Sheets("Defect Input").Select
ActiveSheet.Range("C1:C3").Cut
Sheets("Defect Table").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Sheets("Defect Input").Select
Range("C5:C30").Cut
Sheets("Defect Table").Select
Range("D1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Sheets("Defect Input").Select
Range("C33:C34").Cut
Sheets("Defect Table").Select
Range("AC1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Sheets("Defect Input").Select
Application.CutCopyMode = False
Range("C1:C3").Select
Selection.ClearContents
Range("C5:C30").Select
Selection.ClearContents
Range("C33:34").Select
Selection.ClearContents
Range("C1").Select
ActiveWorkbook.Save
End Sub
Related
I have a question about the macro that I am running. I want to copy / paste the values cross sheet, but the target sheet I want to compare the values and remove duplicates of intercalated columns.
So, the copy and paste is working well, but to comparing and removing duplicates is not working.
Is there something else that I should try in my Macro?
Sub GetInformation()
'
' Macro2 Macro
'
'some comments
Sheets("SpExtra").Select
Columns("A:B").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRToday").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("5lbExt").Select
Columns("A:B").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRInformationToday").Select
Range("D1").Select
ActiveSheet.Paste
Sheets("20LBExtra").Select
Columns("A:B").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRToday").Select
Range("G1").Select
ActiveSheet.Paste
Sheets("JRExtra").Select
Columns("A:B").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRToday").Select
Range("J1").Select
ActiveSheet.Paste
Sheets("SExtra").Select
Columns("A:B").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRToday").Select
Range("M1").Select
ActiveSheet.Paste
'some comments
Sheets("CRToday").Select
Range("J2:J4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRSts").Select
Range("J3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("CRToday").Select
Range("A2:A12").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRSts").Select
Range("N3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("CRToday").Select
Range("M2:M4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRSts").Select
Range("R3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("CRToday").Select
Range("D2:D14").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRSts").Select
Range("V3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("CRToday").Select
Range("D15:D27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRSts").Select
Range("V17").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("CRToday").Select
Range("G2:G10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("CRSts").Select
Range("Z3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.UsedRange.RemoveDuplicates Columns:=Array(10, 14, 18, 22, 26), Header _
:=xlYes
End Sub
I have recorded a macro that is attempting to copy information from cells outside of a table and paste them into a new row in a table on the same sheet. When trying to run the macro I receive "Run-time error '1004': PasteSpecial method of Range class failed." The issue seems to be with the first line stating:
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
I have a collection of paste special code in this module so I am afraid that this first line might not be the only issue. Below is the code I have so far.
Sub PlaceOrder()
'
' PlaceOrder Macro
'
'
Range("A3").Select
Selection.Copy
Range("Table1[[#Headers],[Balance]]").Select
Selection.End(xlDown).Select
Selection.ListObject.ListRows.Add AlwaysInsert:=False
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Range("A1").Select
Range("B3").Select
Application.CutCopyMode = False
Selection.Copy
Range("B23").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Range("A1").Select
Range("C3:E3").Select
Application.CutCopyMode = False
Selection.Copy
Range("C23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 3).Range("A1").Select
Range("F3").Select
Application.CutCopyMode = False
Selection.Copy
Range("F23").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 3).Range("A1").Select
Range("E3").Select
Application.CutCopyMode = False
Selection.ClearContents
Range("D3").Select
Selection.ClearContents
Range("C3").Select
Selection.ClearContents
Range("B3").Select
Selection.ClearContents
Selection.ConvertToLinkedDataType ServiceID:=268435456, LanguageCulture:= _
"en-US"
End Sub
Any help will is greatly appreciated!
Edit:
Worksheet
Attached is screenshot of the worksheet I am working with. I would like to be able to paste the values of A3 & C3-F3, and the formula in B3 into the table seen below. A new row needs to be inserted prior to pasting all of this information.
This should work. It's basically just a clearer version of your code.
Sub PlaceOrder()
Dim tbl As ListObject
Dim LastRow As Long
Set tbl = ActiveSheet.ListObjects("Table1")
LastRow = tbl.Range.Rows.Count 'get # of last row
With ActiveSheet
'copy and paste A3
.Range("A3").Copy
tbl.Range(LastRow, 1).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
'copy and paste B3
.Range("B3").Copy
tbl.Range(LastRow, 2).Offset(1, 0).PasteSpecial Paste:=xlPasteFormulas
'copy and paste C3:F3
.Range("C3:F3").Copy
tbl.Range(LastRow, 3).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
'clear value in B3:F3
.Range("B3:F3").ClearContents
End With
End Sub
Your original macro did not work because the system forgot the copied value after this line:
Selection.ListObject.ListRows.Add AlwaysInsert:=False
New to VBA - Recorded the macro below and every time I run it it always selects 309 rows. I am wanting the rows to be fluid (Example: Could be 400 rows, could be 10 depending on data.
Columns("H:H").Select
`Selection.Style = "Comma"
Range("I2").Select
Selection.EntireColumn.Insert
Range("H1").Select
Selection.Copy
Range("I1").Select
ActiveSheet.Paste
Range("I2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=IF(RC[-3]=""C"",RC[-1]*-1,RC[-1])"
Range("I2").Select
Selection.AutoFill Destination:=Range("I2:I309")
Range("I2:I309").Select
Columns("I:I").Select
Selection.Copy
Range("I1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("H4").Select
Application.CutCopyMode = False
Selection.EntireColumn.Delete
Range("L7").Select
Selection.EntireColumn.Insert
Range("K1").Select
Selection.Copy
Range("L1").Select
ActiveSheet.Paste
Range("L2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RIGHT(RC[-1],4)"
Range("L3").Select
ActiveWindow.SmallScroll Down:=-12
Range("L2").Select
Selection.AutoFill Destination:=Range("L2:L309")
Range("L2:L309").Select
Columns("L:L").Select
Selection.Copy
Range("L1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("K2").Select
Application.CutCopyMode = False
Selection.EntireColumn.Delete
Range("M23").Select
Sheets.Add After:=ActiveSheet
Sheets("Sheet1").Select
Range("K2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Sheet2").Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet1").Select
Range("C281").Select
Selection.End(xlUp).Select
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet2").Select
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.NumberFormat = "m/d/yyyy"
Range("C2:D2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Range("$C$2:$D$309").RemoveDuplicates Columns:=Array(1, 2), _
There's too much going on here for me to translate all of this code for you, but I believe the word you're looking for here is dynamic. In order to replace 309 with a dynamic last row number, you'll need to change:
Selection.AutoFill Destination:=Range("I2:I309")
to
Selection.AutoFill Destination:=Range("I2:I" & Cells(Rows.Count, "I").End(xlUp).Row)
and
Selection.AutoFill Destination:=Range("L2:L309")
to
Selection.AutoFill Destination:=Range("L2:L" & Cells(Rows.Count, "L").End(xlUp).Row)
etcetera, etcetera. Also like #cybernetic.nomad said, you should read that link he shared with you.
I have 3 versions of a workbook, 1 has 4 tabs, one has 8, and one has 12. All tabs in the last one consist of the other two workbooks and the same for the second contains the first. What I need is for my macro to skip searching for the other 8/4 tabs in the earlier versions so that I do not have to click debug each time i have an old version of the workbook loading. I am VERY new to this, so any help would be great. Thank you in advance.
The error happens because the tab does not exist in certain versions of the workbook.
Sub TO_LOAD_OctDec()
' Macro to load data from workbook to master workbook.
'
' Open master database and prepare for transfer
Workbooks.Open Filename:="S:\Property & Casualty\PPE\Wildfires\California Wildfires 2017\Submissions\CWF2017-MasterDatabase.xlsx"
' Focus is given to workbook to obtain correct filename.
ActiveWindow.ActivatePrevious
Sheets("Ready").Select
Range("A9").Select
Selection.ClearContents
' Transfer company info
Application.GoTo Reference:="CoInfo"
Selection.Copy
ActiveWindow.ActivateNext
Sheets("CoInfo").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer personal property data
ActiveWindow.ActivatePrevious
Range("PersonalP").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer commercial property data
ActiveWindow.ActivatePrevious
Range("CommercialP").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer Auto data
ActiveWindow.ActivatePrevious
Range("Auto").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer Other Lines data
ActiveWindow.ActivatePrevious
Range("OtherLines").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer DF personal property data
ActiveWindow.ActivatePrevious
Range("DF_Residential").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer DF commercial property data
ActiveWindow.ActivatePrevious
Range("DF_Commercial").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer DF Auto data
ActiveWindow.ActivatePrevious
Range("DF_Auto").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer DF Other Lines data
ActiveWindow.ActivatePrevious
Range("DF_Other").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer MM Personal Property data
ActiveWindow.ActivatePrevious
Range("MM_Personal").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer MM Commercial Property data
ActiveWindow.ActivatePrevious
Range("MM_Commercial").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer MM Auto data
ActiveWindow.ActivatePrevious
Range("MM_Auto").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Transfer MM Other Lines data
ActiveWindow.ActivatePrevious
Range("MM_Other").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
' Save and close master database
Sheets("CoInfo").Select
ActiveWorkbook.Save
ActiveWorkbook.Close
' TimeStamp
ActiveWindow.ActivateNext
Range("F11").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub
Here's a function to find out if a sheet exists:
Public Function HasSheet(SheetName As String) As Boolean
Dim i As Integer
Dim sheetNameUcase As String
sheetNameUcase = UCase(SheetName)
For i = 1 To Sheets.Count
If UCase(Sheets.Item(i).Name) = sheetNameUcase Then
HasSheet = True
Exit Function
End If
Next
HasSheet = False
End Function
Then use it like in this example:
' Transfer DF Auto data
If HasSheet("Data") Then
ActiveWindow.ActivatePrevious
Range("DF_Auto").Select
Selection.Copy
ActiveWindow.ActivateNext
Sheets("Data").Select
Range("A1").Select
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.End(xlToLeft).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A1").Select
End If
Also, avoid using On Error Resume Next. Essentially it disables all error trapping. It should be treated like an atomic bomb.
I want to transfer data that is in one column (D4:D21 on sheet 'dispersed') to the next empty row in another sheet (B$:N$ on 'sheet4'). Also in the A column on sheet4, I want the date that is in 'dispersed'!b4 I then want the original cells cleared (so that it can be filled out again in a month) and the workbook saved.
I recorded a macro to do this but it is very long. I also can't work out how to change it so that it fills the data on the next empty row as when I recorded the macro it lists the specific cells to paste to.
The end result in 'sheet4' should give me a running total of amounts paid.
Here is the macro that I recorded.
Sub Transfer_dispersments()
'
' Transfer_dispersments Macro
' Botton to transfer data from dispersment to totals
'
'
Range("D4").Select
Selection.Copy
Sheets("Sheet4").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D6").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D7").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D8").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("F2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D9").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("G2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("H2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("I2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D12").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("J2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D13").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D14").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("L2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D15").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet4").Select
Range("M2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Dispersed").Select
Range("D4:D18").Select
Range("D18").Activate
Application.CutCopyMode = False
Selection.ClearContents
Range("B4").Select
Selection.Copy
Sheets("Sheet4").Select
Range("A2").Select
ActiveSheet.Paste
Sheets("Dispersed").Select
Application.CutCopyMode = False
Selection.ClearContents
ActiveWorkbook.Save
End Sub
There are many ways of determining the "last row" of a worksheet. I used one method in the below code:
Sub Transfer_dispersments()
'
' Transfer_dispersments Macro
' Botton to transfer data from dispersment to totals
'
Dim newRow As Long
'Find last non-empty cell in column B
'(and then add 1 so that we point to the row we want to write to)
newRow = Sheets("Sheet4").Cells(Sheets("Sheet4").Rows.Count, "B").End(xlUp).Row + 1
'Copy values from D4:D18 on Dispersed sheet
' to Bx:Px on Sheet4 sheet
Sheets("Sheet4").Cells(newRow, "B").Resize(1, 15).Value = Application.Transpose(Sheets("Dispersed").Range("D4:D18").Value)
'Copy cell from B4 on Dispersed sheet
' to Ax on Sheet4 sheet
Sheets("Dispersed").Range("B4").Copy Sheets("Sheet4").Cells(newRow, "A")
'Clear contents of copied cells
Sheets("Dispersed").Range("D4:D18").ClearContents
Sheets("Dispersed").Range("B4").ClearContents
'Save workbook
ActiveWorkbook.Save
End Sub