Excel VBA to another workbook - vba

I have an excel document with 3 tabs (Report, MI & CSR Data Dump).
When the report tab is complete and "submit" button pushed. The current VBA writes to the data dump sheet perfectly.
However I want to move the data dump into a separate workbook. I have tried and failed. Location:- K:\Call Quality\Quality MI\Quality MI.XLSM This will be the same worksheet name "Data Dump"
My current VBA which works perfectly is
Private Sub generate_report()
Dim i As Long
Dim fullcount As Long
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ws = Worksheets("CSR Data Dump")
With ws
fullcount = Excel.WorksheetFunction.CountA(.Range("A:A"))
i = fullcount + 1
Range("XER2").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("a" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("XER5").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("b" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("XER3:xer4").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("c" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("XER6:xer7").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("e" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("d11:d17").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("g" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("d19").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("o" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("e19:e22").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("s" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("d23").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("w" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("e23:e28").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("aa" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("d29").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("ag" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("e29:e33").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("ak" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("d34").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("ap" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("e34:e37").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("at" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("d38").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("ax" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("e38:e39").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("az" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("d40").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("bb" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("E40:E42").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("Be" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("d44").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("bh" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("E44:e46").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("Bk" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("d47").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("bn" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("E47:e54").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("Bv" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("d55").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("cd" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("E55:e56").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("ch" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("d58").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("cl" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("E60:e63").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("cp" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("h65:h67").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("ct" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("j11").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("cw" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("j19").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("cx" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("j44").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("cy" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("j60").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("cz" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
Range("j65").Select
Selection.Copy
Sheets("CSR Data dump").Select
Range("da" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Sheets("CM Form").Select
Application.CutCopyMode = False
Range("j64").Select
End With
Application.ScreenUpdating = True
End Sub

You are refering to ws, but then you are not using this With reference anywhere. You have to refer to it with a point, like this:
With ws
fullcount = WorksheetFunction.CountA(.Range("A:A"))
i = fullcount + 1
.Range("XER2").Select
Selection.Copy
.Sheets("CSR Data dump").Select
.Range("a" & i).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
.Sheets("CM Form").Select
Application.CutCopyMode = False
Concerning the ws, if it stays in another workbook, then it should be refered like this:
Set wbk = Workbooks.Open("K:\Call Quality\Quality MI\Quality MI.XLSM")
Set ws = wbk.Worksheets("CSR Data dump")
How do I reference to another (open or closed) workbook, and pull values back, in VBA? - Excel 2007

The below should take care of pasting into a different workbook
Private Sub generate_report()
Dim i As Long
Dim fullcount As Long
Dim ws As Worksheet
Dim wbk as Workbook
Dim srcWbk as Workbook
Application.ScreenUpdating = False
Set srcWbk = ActiveWorkbook
Set wbk = Workbooks.Open("K:\Call Quality\Quality MI\Quality MI.XLSM")
Set ws = wbk.Worksheets("CSR Data dump")
srcWbk.Activate
'You are not using With appropriately so let's drop this till you learn how to use it.
'With ws
fullcount = Excel.WorksheetFunction.CountA(ws.Range("A:A"))
i = fullcount + 1
'Do this only once
Sheets("CM Form").Select
ws.Range("a" & i).Value=Range("XER2").Value
ws.Range("b" & i).Value=Range("XER5").Value
ws.Range("c" & i & ":c" & (i+1)).Value=Range("XER3:xer4").Value
ws.Range("e" & i & ":e" & (i+1)).Value=Range("XER6:xer7").Value
ws.Range("g" & i & ":g" & (i+6)).Value=Range("d11:d17").Value
ws.Range("o" & i).Value=Range("d19").Value
ws.Range("s" & i & ":s" & (i+3)).Value=Range("e19:e22").Value
ws.Range("w" & i).Value=Range("d23").Value
ws.Range("aa" & i & ":aa" & (i+5)).Value=Range("e23:e28").Value
ws.Range("ag" & i).Value=Range("d29").Value
ws.Range("ak" & i & ":ak" & (i+4)).Value=Range("e29:e33").Value
ws.Range("ap" & i).Value=Range("d34").Value
ws.Range("at" & i & ":at" & (i+3)).Value=Range("e34:e37").Value
ws.Range("ax" & i).Value=Range("d38").Value
ws.Range("az" & i & ":az" & (i+1)).Value=Range("e38:e39").Value
ws.Range("bb" & i).Value=Range("d40").Value
ws.Range("Be" & i & ":be" & (i+2)).Value=Range("E40:E42").Value
ws.Range("bh" & i).Value=Range("d44").Value
ws.Range("Bk" & i & ":bk" & (i+2)).Value=Range("E44:e46").Value
ws.Range("bn" & i).Value=Range("d47").Value
ws.Range("Bv" & i & ":bv" & (i+7)).Value=Range("E47:e54").Value
ws.Range("cd" & i).Value=Range("d55").Value
ws.Range("ch" & i & ":ch" & (i+1)).Value=Range("E55:e56").Value
ws.Range("cl" & i).Value=Range("d58").Value
ws.Range("cp" & i & ":cp" & (i+3)).Value=Range("E60:e63").Value
ws.Range("ct" & i & ":ct" & (i+2)).Value=Range("h65:h67").Value
ws.Range("cw" & i).Value=Range("j11").Value
ws.Range("cx" & i).Value=Range("j19").Value
ws.Range("cy" & i).Value=Range("j44").Value
ws.Range("cz" & i).Value=Range("j60").Value
ws.Range("da" & i).Value=Range("j65").Value
'End With
'Save and close the target workbook
wbk.Close(True)
Application.ScreenUpdating = True
End Sub
Below is completely your choice, and just a suggestion
I will keep the whole function as is(With only changes at start of Sub to fullcount= statement as shown) and change the signature of the Sub as below
Private Sub generate_report(ws As Worksheet)
Dim i As Long
Dim fullcount As Long
Application.ScreenUpdating = False
.
.
.
Application.ScreenUpdating = True
End Sub
This allows you to paste the data into any target sheet that you pass in. It could be in the same workbook or a different workbook. And I would call it as below
Dim wbk as Workbook
Dim srcWbk as Workbook
Dim ws as Worksheet
Application.ScreenUpdating = False
Set srcWbk = ActiveWorkbook
Set wbk = Workbooks.Open("K:\Call Quality\Quality MI\Quality MI.XLSM")
Set ws = wbk.Worksheets("CSR Data dump")
srcWbk.Activate
Call generate_report(ws)
wbk.Close(True)
OR
Dim srcWbk as Workbook
Dim ws as Worksheet
Application.ScreenUpdating = False
Set ws = ActiveWorkbook.Worksheets("CSR Data dump")
Call generate_report(ws)
Cheers!

Related

VBA Autofilter with criteria as a cell in a differant worksheet. Getting "runtime error 9"

I'm work on this project where I have a summary sheet and a data sheet where I want to drop in raw data from a separate report weekly into the data sheet and filter through what I need and copies to the summary sheet.
The start of the code is simply clearing out the cells for a new run each time. The problem I'm having is the one autofilter criteria that is a cell value from the summary sheet. It is a drop down box with that will when changed, copy different data to the summary sheet. Here is my code. Thanks for any advice in advance!
Sub Macro3()
Sheets("Summary").Select
Range("C5").Select
Selection.ClearContents
Range("C6").Select
Selection.ClearContents
Range("C10").Select
Selection.ClearContents
Range("C11").Select
Selection.ClearContents
Range("C16").Select
Selection.ClearContents
Range("C17").Select
Selection.ClearContents
Range("C21").Select
Selection.ClearContents
Range("C22").Select
Selection.ClearContents
Range("F11").Select
Selection.ClearContents
Range("F10").Select
Selection.ClearContents
Range("F6").Select
Selection.ClearContents
Range("F5").Select
Selection.ClearContents
Sheets("Data").Select
Range("K200000").Select
Selection.ClearContents
Range("J200000").Select
Selection.ClearContents
i = Sheets("Summary").Range("i2")
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=13, Criteria1:=i
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=7, Criteria1:="<>"
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11, Criteria1:="<>0"
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:=i
Range("K200000").Select
ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,R[-199996]C:R[-1]C)"
Range("J200000").Select
ActiveCell.FormulaR1C1 = "=SUBTOTAL(2,R[-199996]C:R[-1]C)"
Selection.NumberFormat = "#,##0.0"
Selection.NumberFormat = "#,##0"
Selection.Copy
Sheets("Summary").Select
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
Range("k200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("C10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:="<>" & i
Range("j200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("C6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
Range("k200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("C11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=7
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=12, Criteria1:= _
"<>"
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:=i
Range("j200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("C16").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
Range("k200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("C21").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:="<>" & i
Range("j200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("C17").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
Range("k200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("C22").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=12
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=4, Criteria1:="<>0"
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:=i
Range("j200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("F5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
Range("k200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("F10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15, Criteria1:="<>" & i
Range("j200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("F6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
Range("k200000").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Summary").Select
Range("F11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Data").Select
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=13
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=4
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=15
Sheets("Summary").Select
End Sub
As Peh says, this is pretty much unreadable. From your question though, I think your problem refers to setting the criteria to i? If so, try changing
i = Sheets("Summary").Range("i2")
to
i = Sheets("Summary").Range("i2").Value

vba macro - sometimes it works, sometimes it doesn't

This is my first macro, and I need some help. I keep changing my variables in sheet 1, and run another macro in sheet 2 to get my results. So this is a sensitivity test and I'm writing the following macro to run an already existing marco. Some of the rows it generates seem to be correct, but some of them are not. I can't figure out what went wrong. Any tips are appreciated.
Sub SensitivityTest()
For i = 8 To 11
Range("G" & i + 1).Select
Selection.Copy
Range("D10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Call AnotherMacro
Range("Q76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("H" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("I" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("J" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("K" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("L" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("M" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("Q28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("N" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AD28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("O" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("Q" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("R" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("S" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI20").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("T" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("U" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI27").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("V" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("V28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("W" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Sheet2").Select
Range("AI28").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("X" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next i
End Sub
To follow up on #bruceWayne's comment:
Current copy/paste operation:
Sheets("Sheet2").Select
Range("AD76").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
Range("I" & i + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Without selecting/activating:
Sheets("Sheet2").Range("AD76").Copy
Sheets("Sheet1").Range("I" & i + 1).PasteSpecial Paste:=xlPasteValues
Because I was bored while eating my lunch, I decided to rewrite the code to see how much it would reduce to after getting rid of all the .Select, Selection. bits (plus a few other bits of tidying). I came up with this:
Sub SensitivityTest()
With Sheets("Sheet1")
For i = 8 To 11
.Range("D10").Value = .Range("G" & i + 1).Value
.Range("D15").Value = .Range("G" & i + 1).Value
'This next line shouldn't be required if "AnotherMacro" was suitably changed
'to fully qualify all ranges, etc, being referred to
Sheets("Sheet2").Select
Call AnotherMacro
'Because the original code was pasting values, I have changed the
'code to just set the destination cell's Value equal to the
'source cell's Value. This avoids using the clipboard, which
'often leads to problems if the user is doing something else
'while a macro is running.
.Range("H" & i + 1).Value = Sheets("Sheet2").Range("Q76").Value
.Range("I" & i + 1).Value = Sheets("Sheet2").Range("AD76").Value
.Range("J" & i + 1).Value = Sheets("Sheet2").Range("Q20").Value
.Range("K" & i + 1).Value = Sheets("Sheet2").Range("AD20").Value
.Range("L" & i + 1).Value = Sheets("Sheet2").Range("Q27").Value
.Range("M" & i + 1).Value = Sheets("Sheet2").Range("AD27").Value
.Range("N" & i + 1).Value = Sheets("Sheet2").Range("Q28").Value
.Range("O" & i + 1).Value = Sheets("Sheet2").Range("AD28").Value
.Range("Q" & i + 1).Value = Sheets("Sheet2").Range("V76").Value
.Range("R" & i + 1).Value = Sheets("Sheet2").Range("AI76").Value
.Range("S" & i + 1).Value = Sheets("Sheet2").Range("V20").Value
.Range("T" & i + 1).Value = Sheets("Sheet2").Range("AI20").Value
.Range("U" & i + 1).Value = Sheets("Sheet2").Range("V27").Value
.Range("V" & i + 1).Value = Sheets("Sheet2").Range("AI27").Value
.Range("W" & i + 1).Value = Sheets("Sheet2").Range("V28").Value
.Range("X" & i + 1).Value = Sheets("Sheet2").Range("AI28").Value
Next i
'Include a final select of Sheet1, just to get around the effect of
'doing the Select of Sheet2 during the macro. This wouldn't be
'needed if AnotherMacro was similarly tidied up to not require
'Sheet2 to be Selected before running.
.Select
End With
End Sub
I find this much easier to read, and therefore it would be a lot easier to maintain and debug when necessary.
P.S. All the i + 1 statements could be changed to just i if the loop was changed from For i = 8 To 11 to be For i = 9 To 12.
P.P.S. My guess as to why your code sometimes worked and sometimes didn't is that your code was dependent on Sheet1 being the active sheet when you invoked the macro. If Sheet2 was active, it would almost certainly not do what you wanted it to do.
Thank you all for the help! When I ran the following codes on Friday, it got stuck at the last few rows, and the same results kept repeating itself. But when I let it ran after work and not doing other things on the computer, it worked !
Sub SensitivityTest()
With Sheets("Sheet1")
For i = 9 To 40
.Range("D10").value = .Range("G" & i).value
.Range("D15").value = .Range("G" & i).value
Call AnotherMacro
.Range("H" & i).value = Sheets("Sheet2").Range("Q76").value
.Range("I" & i).value = Sheets("Sheet2").Range("AD76").value
.Range("J" & i).value = Sheets("Sheet2").Range("Q20").value
.Range("K" & i).value = Sheets("Sheet2").Range("AD20").value
.Range("L" & i).value = Sheets("Sheet2").Range("Q23").value
.Range("M" & i).value = Sheets("Sheet2").Range("AD23").value
.Range("N" & i).value = Sheets("Sheet2").Range("Q28").value
.Range("O" & i).value = Sheets("Sheet2").Range("AD28").value
.Range("Q" & i).value = Sheets("Sheet2").Range("V76").value
.Range("R" & i).value = Sheets("Sheet2").Range("AI76").value
.Range("S" & i).value = Sheets("Sheet2").Range("V20").value
.Range("T" & i).value = Sheets("Sheet2").Range("AI20").value
.Range("U" & i).value = Sheets("Sheet2").Range("V23").value
.Range("V" & i).value = Sheets("Sheet2").Range("AI23").value
.Range("W" & i).value = Sheets("Sheet2").Range("V28").value
.Range("X" & i).value = Sheets("Sheet2").Range("AI28").value
Next i
End With
End Sub

Excel VBA copy range to a new sheet after 1,048,576 rows

So I wrote a fairly simple Macro in VBA that updates a set of variables, then copying and pasting the updated values into a new sheet. The problem is that the volume is getting a bit overwhelming now, thus reaching the 1,048,576 row limit in Excel, causing the code to crash.
I would like to update it so that whenever the rows limitation is reached, the script begins copying the cells to a new sheet (say, "FinalFile2","FinalFile3", etc) until it's fully executed.
Sub KW()
'
' Exact KWs
'
Dim i, j, LastRow As Long
Dim relativePath As String
i = 2
j = 2
'LastRowValue'
Sheets("Output").Select
LastRow = Rows(Rows.Count).End(xlUp).Row - 1
'Clean final output'
Sheets("FinalFile").Select
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A1").Select
'Set Variables in Variables sheet'
Do
'Var 1'
Sheets("Names").Select
Range("A" & i).Select
Selection.Copy
Sheets("Variables").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 2'
Sheets("Names").Select
Range("B" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 3'
Sheets("Names").Select
Range("C" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 4'
Sheets("Names").Select
Range("D" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 5'
Sheets("Names").Select
Range("E" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 6'
Sheets("Names").Select
Range("F" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("F2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 7'
Sheets("Names").Select
Range("G" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("G2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 8'
Sheets("Names").Select
Range("H" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("H2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 9'
Sheets("Names").Select
Range("I" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("I2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 10'
Sheets("Names").Select
Range("J" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("J2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Var 11'
Sheets("Names").Select
Range("K" & i).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Variables").Select
Range("K2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy and Paste'
Sheets("Output").Select
Range("A2:AP2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("FinalFile").Select
Range("A" & j).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'update counters'
i = i + 1
j = j + LastRow
'end of loop condition'
Sheets("Names").Select
Loop Until IsEmpty(Cells(i, 1))
End Sub
Here are some tips how to improve your code. I am not going into the issues I mentioned in my comment on the original question but just concentrate on specific parts of the code:
Remove Selections. The general pattern is instead of
something.Select
Selection.Dosomenthing
you use
something.Dosomething
In your case:
Sheets("Names").Select
Range("A" & i).Select
Selection.Copy
Sheets("Variables").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
becomes
Sheets("Names").Range("A" & i).Copy
Sheets("Variables").Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Use variables to reference your sheets like this:
Dim nameSheet as Worksheet
Dim varSheet as Worksheet
Dim finalSheet as Worksheet
Set nameSheet = Sheets("Names")
Set varSheet = Sheets("Variables")
Set finalSheet = Sheets("FinalFile")
Now you can use
finalSheet.Range(...).Pastespecial ...
and use Set finalSheet = Sheets("FinalFile2") once you run out of space
Don't copy cells next to each other one by one. You are copying cell Ai to A2 then Bi to B2. Just copy the range Ai:Ki to A2:K2 (although I don't see the point of this)
Don't use Copy if you don't need to. Instead of
someRange.Copy
someOtherRange.PasteSpecial Paste:=xlPasteValues
you can use
someOtherRange.Value = someRange.Value
(make sure the sizes are the same)
Disable Screenupdating using Application.Screenupdating = False (set it to True after you're done) when you're doing a lot of insertions. It can speed up a macro a lot.
As to your actual question, do as Tom suggests, add
If j > 1048576 Then
j = 2
Set finalSheet = Sheets("FinalFile2") 'maybe create the new sheet at this point
End If
You can add
j = j + lastRow
If j = 1048576 Then j = 2
BUT you should definitely clean up this code. .selections are a really slow way to do stuff like this. Look into this and try to avoid .Copy & .Paste. Just set your target cells to the values of your source with an =. This also saves a lot of time.
Edit: And definitely take a look at the link posted by #arcadeprecinct

Code won't move down the available cells

I have a booking system (plain drop down list feeding into VLOOKUP fields). I want to be able to copy the information across and I can do this easily. The problem is that I want the sheet to allow multiple bookings and every time one booking is done to allow a second one to be done. At the moment it just rewrites the previous booking, it won't move down the available cells!
Below is the code used, I know it could be neater but I am looking as to why it doesn't work. I have an error displaying on the With section 424 it comes up with and usually complains about UsedRange.
Sub Bookingtry()
'
' Bookingtry Macro
'
' Keyboard Shortcut: Ctrl+h
'
Range("A2").Select
Sheets("Booking Form").Select
Range("B2").Select
Selection.Copy
Sheets("Booking sheet").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B2").Select
Sheets("Booking Form").Select
Range("B4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Booking sheet").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C2").Select
Sheets("Booking Form").Select
Range("B6").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Booking sheet").Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("D2").Select
Sheets("Booking Form").Select
Range("B8").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Booking sheet").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("E2").Select
Sheets("Booking Form").Select
Range("B10").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Booking sheet").Select
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Booking Form").Select
Range("B12").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Booking sheet").Select
Range("F2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("G2").Select
Sheets("Booking Form").Select
Range("B14").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Booking sheet").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
With Sheets("Booking sheet").Cells(UsedRange.Columns(1).Rows.Count + 1, 1).Paste
End With
End Sub
I clean you code to see what you were trying to do, I guess that I'm ok for the first part.
But for your last "line" (the With with UsedRange), I don't really get what you are trying to paste... Everything else was already pasted. Anyway, I corrected the syntax of that last part too so that you can use it.
Give this a look :
Sub Bookingtry()
' Keyboard Shortcut: Ctrl+h
Dim FirstEmptyRow As Long, _
WsBF As Worksheet, _
WsBS As Worksheet
Set WsBF = ThisWorkbook.Sheets("Booking Form")
Set WsBS = ThisWorkbook.Sheets("Booking sheet")
FirstEmptyRow = WsBS.Range("A" & WsBS.Rows.Count).End(xlUp).Row + 1
WsBF.Range("B2").Copy
WsBS.Range("A" & FirstEmptyRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
WsBF.Range("B4").Copy
WsBS.Range("B" & FirstEmptyRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
WsBF.Range("B6").Copy
WsBS.Range("C" & FirstEmptyRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
WsBF.Range("B8").Copy
WsBS.Range("D" & FirstEmptyRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
WsBF.Range("B10").Copy
WsBS.Range("E" & FirstEmptyRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
WsBF.Range("B12").Copy
WsBS.Range("F" & FirstEmptyRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
WsBF.Range("B14").Copy
WsBS.Range("G" & FirstEmptyRow).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End Sub

How do I access a specific workbook that I have open, without using its name?

I'm recording this macro that transfers data between a few different documents. One of the workbooks, "Transfer Template", stays constant. But the other will change. Here is the code that I am using. (I know it's slow and a lot of it is irrelevant, but I just need to make it work).
What I'm assuming is that I have to replace 'Windows("RFQ_14446.xlsm") with ActiveWorkbook or something similar.
Sub Initial_Transfer_Macro()
'
' Initial_Transfer_Macro Macro
'
'
Windows("RFQ_14446.xlsm").Activate
Range("J51").Select
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B1").Select
ActiveSheet.Paste
Windows("RFQ_14446.xlsm").Activate
Range("D27").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B2").Select
ActiveSheet.Paste
Windows("RFQ_14446.xlsm").Activate
Range("D5").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B3").Select
ActiveSheet.Paste
Windows("RFQ_14446.xlsm").Activate
Range("F2").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B4").Select
ActiveSheet.Paste
Windows("RFQ_14446.xlsm").Activate
Range("D7").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B5").Select
ActiveSheet.Paste
Windows("RFQ_14446.xlsm").Activate
Range("D18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B6").Select
ActiveSheet.Paste
Windows("RFQ_14446.xlsm").Activate
Range("K6").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("D12").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B8").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("D22").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("D11").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B10").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("K18").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B11").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("K3").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B12").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("I13").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("I26").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("C14").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("I27").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B17").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("I5").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "36"
Range("I5").Select
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("B20").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("RFQ_14446.xlsm").Activate
Range("I26").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Transfer Template.xlsm").Activate
Range("C20").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Cells.Replace What:=" Rev. ", Replacement:="-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="RFQ ", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
Easiest way is to set references to each workbook at the start of your code:
Sub SO()
Dim thisWorkbook As Excel.Workbook
Dim otherWorkbook As Excel.Workbook
Set thisWorkbook = ActiveWorkbook
Set otherWorkbook = Workbooks("Transfer Template.xlsm")
'// ... Rest of code here
End Sub
Once this is done you can refer to that variable instead, for example:
Debug.Print thisWorkbook.Sheets.Count
or
otherWorkbook.Sheets(1).Range("A1").Value = thisWorkbook.Sheets(2).Range("B1").Value
Just crude examples but should give you the base of the logic...
Another thing worth noting is that if the code is being run from the workbook that you want to refer to, then simply using ThisWorkbook will suffice:
Sub Example()
Workbooks("Transfer Template.xlsm").Activate
MsgBox ActiveWorkbook.Name
MsgBox ThisWorkbook.Name
End Sub
If you don't know the workbook name but it is the only other one open at the same time (in the same instance of Excel), you can loop through them like this:
Sub TransferTemplate()
Dim wbTemplate As Workbook: Set wbTemplate = ActiveWorkbook
Dim wbDestination As Workbook
Dim wb As Workbook
For Each wb In Workbooks
If wb.Name <> wbTemplate.Name Then
Set wbDestination = wb
End If
Next wb
'Example copy
wbTemplate.Worksheets(1).Range("B1").Value = wbDestination.Worksheets(1).Range("J51").Value
End Sub