Excel Macro Copy and Paste Between WorkBooks - excel

So I've run in to a brick wall regarding copy and pasting from one Workbook to another using Macros
I have about 800 Work Books that I need to copy certain cells from and paste in to a separate "tracker" work book. Macro's are going to be the easiest way to do this.
The problem I'm running into is how do I tell the macro that the COPYFROM.XLSX workbook will be changing, and when it's being pasted it needs to the paste to the next line so as not to overwrite information.
Any help you guys have will be super useful, thanks.
Windows("COPYFROM.xlsx").Activate
Range("E39:F39").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("B8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F13").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("C8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C13").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("D8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C15").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("E8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F17").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("F8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C17:C18").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("G8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C27").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("H8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F21").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("J8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C21").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("K8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C23").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("N8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F25").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("O8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C37").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("Q8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F59").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("S8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F61").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("T8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F19").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("U8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C31").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("V8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F49").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("W8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F31").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("X8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F37").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("Y8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F15").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("AA8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("C37").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("AE8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("COPYFROM.xlsx").Activate
Range("F45").Select
Selection.Copy
Windows("Paste.XLSX").Activate
Range("AF8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

Like this:
List the files you need to copy, either manually or using (another) macro. For instance, like this Get list of Excel files in a folder using VBA
Using this list, set a range to run through
Copy-paste the data onto the next free row
Sub test()
Dim LastColumn As Long, LastRow As Long, LR As Long, n As Long
Dim Thiswb As Workbook, Openwb As Workbook
Dim Source As Worksheet, wsTO As Worksheet, wsM As Worksheet
Dim FileRange As Range
Dim sSource As String, FileName As String
Dim cell As Variant, FilePath As Variant
Set Thiswb = ThisWorkbook
' Here you put the list of the files you want to copy from
Set Source = Thiswb.Worksheets("Source")
' Here you will paste your data
Set wsTO = Thiswb.Worksheets("HereComesYourData")
' Find the last row of column A. The list of files to look for is in this column
LastRow = Source.Cells(Rows.Count, 1).End(xlUp).Row
'Set the range in which to look
Set FileRange = Source.Range(Source.Cells(2, 1), Source.Cells(LastRow, 1))
n = 2
On Error Resume Next
For Each cell In FileRange 'Run through the whole range
'Error handling when file or worksheet isn't found
FilePath = Source.Cells(n, 2).Value
FileName = Source.Cells(n, 1).Value
Workbooks.Open (FilePath)
Set Openwb = Workbooks(FileName)
'Depending on what you want to copy - declare the correct variable
Set wsM = Openwb.Worksheets("Master")
'Calculate last column number of source
LastColumn = wsM.Cells(1, Columns.Count).End(xlToLeft).Column
'Calculate last row number of source
LastRow = wsM.Cells(Rows.Count, 1).End(xlUp).Row
'Calculate last row number of destination
LR = wsTO.Cells(Rows.Count, 1).End(xlUp).Row
'Paste values
wsTO.Range(wsTO.Cells(LR, 1), wsTO.Cells(LR + LastRow, LastColumn)).Value = wsM.Range(wsM.Cells(2, 1), wsM.Cells(LastRow, LastColumn)).Value
Openwb.Close SaveChanges:=False
Next cell
End sub

Something along these lines. Presuming you are moving along row 8. You should use sheet names rather than the indexes below, and use more meaningful procedue/variable names.
Sub x()
Dim c As Long
Windows("COPYFROM.xlsx").Sheets(1).Range("E39:F39").Copy
With Windows("Paste.XLSX").Sheets(1)
c = .Cells(8, Columns.Count).End(xlToLeft).Column + 1
.Cells(8, c).PasteSpecial Paste:=xlPasteValues
End With
'etc
End Sub

Related

Listing names in a dropdown list, when any part of the name is typed in a ComboBox on a UserForm

I have a workbook with a list of pallet names. I also have userform and 7 comboboxes with the names in it. Comboboxes are populated from a range like this:
Me.ComboBox1.List = Worksheets("palety").Range("E7:E1341").Value
Right now I have to type the exact name of the pallet to be able to search for it. I want to just type a part of it and have it appear in the dropdown list. Tried this code:
Private Sub cboProgrammeName_Change()
Dim ws As Worksheet
Dim x, dict
Dim i As Long
Dim str As String
Set ws = Sheets("XXX")
x = ws.Range("ProgrammeNameList").Value
Set dict = CreateObject("Scripting.Dictionary")
str = Me.cboProgrammeName.Value
If str <> "" Then
For i = 1 To UBound(x, 1)
If InStr(LCase(x(i, 1)), LCase(str)) > 0 Then
dict.Item(x(i, 1)) = ""
End If
Next i
Me.cboProgrammeName.List = dict.keys
Else
Me.cboProgrammeName.List = x
End If
Me.cboProgrammeName.DropDown
End Sub
The code itself worked, but my insert and copy macro went wild, and stopped working properly. Inserted it to the wrong row, and thus copied the wrong pallet. I added the code to ComboBox1_change
For example:
we have a pallet with name TestPallet 123, PalletTest 123, 123 Test. When I type in 123 i would like to have appear every pallet that contain 123 in it.
This is my code, it searches the chosen pallet from ComboBoxes, then inserts the number from TextBoxes. After that autofilters the field and copies the visible cells into another sheet. When I tried the code above the search and insert part did not worked.
Public Sub Import_Click()
Dim palety As Worksheet
Dim skracovanie As Worksheet
Dim harmonogram As Worksheet
Dim i As Long
Set palety = ThisWorkbook.Worksheets("palety")
Set skracovanie = ThisWorkbook.Worksheets("skracovanie")
Set harmonogram = ThisWorkbook.Worksheets("harmonogram")
palety.Range("M7:M1331").ClearContents
Dim idx As Long
Dim idxList As Long
Dim lngRow As Long
For idx = 1 To 7
idxList = Me.Controls("ComboBox" & idx).ListIndex
If idxList <> -1 Then
lngRow = idxList + 7
Worksheets("palety").Range("M" & lngRow).Value = Me.Controls("TextBox" & idx).Value
End If
Next idx
skracovanie.Activate
skracovanie.Range("A3:M100").ClearContents
skracovanie.Range("P3:Q100").ClearContents
skracovanie.Range("X:X").ClearContents
Sheets("palety").Select
ActiveSheet.Range("M:M").AutoFilter Field:=13, Criteria1:=">=1"
'név
palety.Range("E1347:E4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'kód vstupného reziva
palety.Range("V1347:V4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'kód palety
palety.Range("C1347:C4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 3).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'harmonogram fulre
Worksheets("harmonogram").Range("A5:A62").ClearContents
palety.Range("C7:C1341").SpecialCells(xlCellTypeVisible).Copy
harmonogram.Cells(5, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'kód prírezov
palety.Range("G1347:G4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 4).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'méretek
palety.Range("H1347:J4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 5).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'darabszám
palety.Range("M1347:M4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 8).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'poznámka
palety.Range("BI1347:BI4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 9).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'objem celkom
palety.Range("N1347:N4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 12).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'výťaž
palety.Range("X1347:X4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 13).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'príprava
palety.Range("P1347:P4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 16).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'spotreba času na 100 ks
palety.Range("Q1347:Q4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 17).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'prírez V szog
palety.Range("F1347:F4000").SpecialCells(xlCellTypeVisible).Copy
skracovanie.Cells(3, 24).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
my code that was solved; code copied from here

VBA Fixed workbook to various workbooks

I think I'd like to do my everyday things easier. I do not have any wide knowleadge of VBA so i use it with "record VBA".
I have 2 workbooks:
(1) "DATABASE.xlsx" - fixed which always collect data
(2) many workbooks i.e. "xxx.xlsx" , "yyyy.xlxs", "zzzz.xlsx" etc. - variable workbook, changing every time depends on what i copy from the workbook (1) - I will open (2) manually by myself
My issue is how to write my VBA to copy data from (1) to various workbooks (2). I do not know how to define the (2) workbooks which alwyas change. I can choose (2) workbooks manually. It does not have to be something really professional. It's only about coping from (1) to (2)....
I have done as below:
Sub Makro10() ' ' Makro10 Makro ' ' Klawisz skrótu: Ctrl+Shift+X '
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 2).Range("A1:H1").Select
Selection.Copy
Windows("xxx.xlsx").Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 8).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("xxx.xlsx").Activate
ActiveCell.Offset(0, 9).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 1).Range("A1:C1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("xxx.xlsx").Activate
ActiveCell.Offset(0, 7).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 3).Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("xxx.xlsx").Activate
ActiveCell.Offset(0, 14).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False End Sub
It's working for only specific (2) workbook "xxx.xlsx", but i would like to use it for various (2).
I'm asking for support, thanks in advance.
The easiest way to achieve this is by replacing all the instances of Windows("xxx.xlsx").Activate with Workbooks(2).Activate in your code.
But you have to make sure that no other workbooks are open as the code can accidentally reference another workbook based on how you opened excel files. So technically, you need to open workbook (1) first which is DATABASE.xlsx. Then open whichever workbook (2) you need and run the code.
Sub Makro10() ' ' Makro10 Makro ' ' Klawisz skrótu: Ctrl+Shift+X '
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 2).Range("A1:H1").Select
Selection.Copy
Workbooks(2).Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 8).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(2).Activate
ActiveCell.Offset(0, 9).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 1).Range("A1:C1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(2).Activate
ActiveCell.Offset(0, 7).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("DATABASE.xlsx").Activate
ActiveCell.Offset(0, 3).Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(2).Activate
ActiveCell.Offset(0, 14).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False End Sub
What is happening here is that VBA references the 2nd workbook that was opened (which is workbooks(2)) and pastes the data there as per the code. in your earlier code, because you recorded it, it was always using xxx.xlsx file because that is how you recorded it.
I would suggest that if possible save the code in DATABASE.xlsx and save the file as a macro file (DATABASE.xlsm). That way, you can open workbook (1) and workbook (2) (whichever one you want) and run the code from workbook (1).
Hope this helps! Happy coding!
Sub Makro2()
'
' Makro2 Makro
'
' Klawisz skrótu: Ctrl+Shift+X
'
Dim OthWB As String
Windows.Application.ActiveSheet.Select
OthWB = ActiveWorkbook.Name
Workbooks("202102 PO.xlsx").Activate
ActiveCell.Offset(0, 2).Range("A1:H1").Select
Selection.Copy
Workbooks(OthWB).Activate
ActiveCell.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("202102 PO.xlsx").Activate
ActiveCell.Offset(0, 8).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(OthWB).Activate
ActiveCell.Offset(0, 9).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("202102 PO.xlsx").Activate
ActiveCell.Offset(0, 1).Range("A1:C1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(OthWB).Activate
ActiveCell.Offset(0, 7).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks("202102 PO.xlsx").Activate
ActiveCell.Offset(0, 3).Range("A1:B1").Select
Application.CutCopyMode = False
Selection.Copy
Workbooks(OthWB).Activate
ActiveCell.Offset(0, 14).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
#Bharath Raja
Thanks for your answer. I will try your solution also
I was thinking also about issue & found the resolve. We must active (2) workbook & then it is defined as OthWB. Maybe somebody uses it also.

If Statement which I use to copy paste rows based on specific criteria in a different sheet

So I want to copy/paste fixed numbers in a different tab. I have one parameter (which is the period name) and based on the number the data should go to different rows. I prepared a code so far but it is so big! If you could advice of how I can reduce the lines that would be helpful. I have 12 periods but I just show you the first two as the rest are identical and I change only the row number.
Thanks so much in advance
If Range("D2") = 1 Then
Range("D10:E10").Select
Selection.Copy
Sheets(7).Select
Range("D10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("G10:H10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("G10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("K10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("K10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("M10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("M10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("O10:S10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("O10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("V10:Z10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("V10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ElseIf Range("D2") = 2 Then
Range("D11:E11").Select
Selection.Copy
Sheets(7).Select
Range("D11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("G11:H11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("G11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("K11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("K11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("M11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("M11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("O11:S11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("O11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(6).Select
Range("V11:Z11").Select
Application.CutCopyMode = False
Selection.Copy
Sheets(7).Select
Range("V11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End if
You don't need to select anything.
Dim row_offset As Long
row_offset = Sheets(6).Range("D2").Value - 1
Sheets(7).Range("D10:E10").Offset(row_offset, 0).Value = Sheets(6).Range("D10:E10").Offset(row_offset, 0).Value
Sheets(7).Range("G10:H10").Offset(row_offset, 0).Value = Sheets(6).Range("G10:H10").Offset(row_offset, 0).Value
Sheets(7).Range("K10").Offset(row_offset, 0).Value = Sheets(6).Range("K10").Offset(row_offset, 0).Value
Sheets(7).Range("M10").Offset(row_offset, 0).Value = Sheets(6).Range("M10").Offset(row_offset, 0).Value
Sheets(7).Range("O10:S10").Offset(row_offset, 0).Value = Sheets(6).Range("O10:S10").Offset(row_offset, 0).Value
Sheets(7).Range("V10:Z10").Offset(row_offset, 0).Value = Sheets(6).Range("V10:Z10").Offset(row_offset, 0).Value

Short way of doing this in Excel. Transferring data to another sheet, then clearing cells and saving

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

Copying in Excel while picking up multiple rows that are feeding input into the copied Excel formula

I am a starting VBA enthusiast and I would like some help on the below formula as I have no idea how to make sure the formula applies to all rows in the book. As you can see, I have started copying the actual code, but as I have to do this for up to 100 rows this will be too manually.
Thanks
Sub Charts()
' Charts Macro
' Run charts
Range("D7").Value = Range("D11")
Range("E7:G7").Select
Selection.Copy
Range("E11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D7").Value = Range("D12")
Range("E7:G7").Select
Selection.Copy
Range("E12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D7").Value = Range("D13")
Range("E7:G7").Select
Selection.Copy
Range("E13").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D7").Value = Range("D14")
Range("E7:G7").Select
Selection.Copy
Range("E14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Is this what you are trying?
Option Explicit
Sub Charts()
Dim i As Long
'~~> Change this to the relevant sheet
With Sheets("Sheet1")
For i = 11 To 14 '<~~ Change 14 to whatever row you want to go to
.Range("D7").Value = .Range("D" & i).Value
.Range("E7:G7").Copy
.Range("E" & i).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Next i
End With
End Sub

Resources