trouble with variable in Range.select - vba

Can anyone give me a clue as to why I can't seem to get a variable to function in my Range.select?
As you can see from the commented statements I have tried a number of different syntax's and commands but I always get a run-time error 1004, method Range of object global failed
I am trying to take data from sheet one in a specific section and copy it to specific cells in the current row (by loop count) in sheet two. Ignore the unfinished loop, haven't been able to get it to run through once so i haven't completed writing the loop yet.
Sub PutDataSht2()
Dim rowVal As Integer
rowVal = 1
'
' PutDataSht2
'
'
'ThisWorkbook.Activate
'Sheets("Sheet1").Activate
Sheets("Sheet1").Select
Range("A38:H38").Select
Selection.Copy
'Sheets("Sheet2").Activate
Sheets("Sheet2").Select
'Range("A1:H1").Select
Range("A[XrowVal]:H[XrowVal]").Select
'Range("A & rowVal:H & rowVal").Select
'Application.Goto ActiveWorkbook.Sheets("Sheet2").Range("A & rowVal:H & rowVal")
'ActiveSheet.Range(Cells(1, rowVal), Cells(8, rowVal)).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Sheets("Sheet1").Activate
Sheets("Sheet1").Select
Range("B85:H85").Select
Application.CutCopyMode = False
Selection.Copy
'Sheets("Sheet2").Activate
Sheets("Sheet2").Select
'Range("J1:P1").Select
Range("J & rowVal:P & rowVal").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Sheets("Sheet1").Activate
Sheets("Sheet1").Select
Range("B132:D132").Select
Application.CutCopyMode = False
Selection.Copy
'Sheets("Sheet2").Activate
Sheets("Sheet2").Select
'Range("R1:T1").Select
Range("R & rowVal:T & rowVal").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
rowVal = (rowVal + 1)
End Sub
`

One of your options is almost correct!
Range("A & rowVal:H & rowVal").Select
should be:
Range("A" & rowVal & ":H" & rowVal).Select

Related

Vba/Macro to paste in next available row

I am trying to copy and paste certain cells that are not in the same column or row and paste them in Specific columns (it will be the same columns everytime). Once each entry is done, I want the next set of entries to paste on the next available row. The first set of code for Paste_NextRow() was ran as a macro and this was the code that was returned. The ranges I selected have formulas in them that will have different values each month. I am pasting them in a row with headers in row A. The second set of code for LastRow() I found this online and it will return the last row that is empty. I'm unsure how to utilize the second set of code to paste in the next available row. If you need additional context in order to help modify the code please let me know. Thanks. I've edited the text to show the code accordingly.
Sub Paste_NextRow()
'
' Paste_NextRow Macro
'
'
Sheets("SUMMARY DATA SHEET").Select
Range("F3").Select
Selection.Copy
Sheets("Invoice Number").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SUMMARY DATA SHEET").Select
Range("F2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Invoice Number").Select
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SUMMARY DATA SHEET").Select
Range("B4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Invoice Number").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SUMMARY DATA SHEET").Select
Range("F4").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Invoice Number").Select
Range("D2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SUMMARY DATA SHEET").Select
Range("F5").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Invoice Number").Select
Range("E2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A3").Select
End Sub
Sub LastRow()
NextRow = ThisWorkbook.Sheets("Invoice Number").Cells(Rows.Count, 2).End(xlUp).Row + 1
End Sub
Here is a quick rewrite setting a variable (called lastRow) to the last row in your invoice number tab.
Sub Paste_NextRow()
'
' Paste_NextRow Macro
'
'
'Get the last used row into a variable
Dim lastRow as Long
lastRow = ThisWorkbook.Sheets("Invoice Number").Cells(Rows.Count, 2).End(xlUp).Row + 1
'Copy Summary Data Sheet F3
Sheets("SUMMARY DATA SHEET").Range("F3").Copy
'And paste it into the last row (column F) of Invoice Number sheet
Sheets("Invoice Number").Range("F" & LastRow).Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Use similar logic for the remaining cells
Sheets("SUMMARY DATA SHEET").Range("F2").Copy
Sheets("Invoice Number").Range("C" & lastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SUMMARY DATA SHEET").Range("B4").Copy
Sheets("Invoice Number").Range("B" & lastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SUMMARY DATA SHEET").Range("F4").Copy
Sheets("Invoice Number").Range("D" & lastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("SUMMARY DATA SHEET").Range("F5").Copy
Sheets("Invoice Number").Range("E" & lastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
You'll see that there are no .Select happening here since Selecting a sheet or a cell is something a human does. There really isn't a need to do that in VBA where instead we can just specify exactly what we want to copy and where we want to paste it.
While this is cleaned up, it's still a little cumbersome to just copy and paste VALUES around the workbook. It uses the clipboard and has multiple statements for each copy/paste.
Instead we can just set the value of one cell equal to the value of another cell:
Sub Paste_NextRow()
'
' Paste_NextRow Macro
'
'
'Get the last used row into a variable
Dim lastRow as Long
lastRow = ThisWorkbook.Sheets("Invoice Number").Cells(Rows.Count, 2).End(xlUp).Row + 1
Sheets("Invoice Number").Range("F" & LastRow).Value = Sheets("SUMMARY DATA SHEET").Range("F3").value
Sheets("Invoice Number").Range("C" & lastRow).value = Sheets("SUMMARY DATA SHEET").Range("F2").value
Sheets("Invoice Number").Range("B" & lastRow).Value = Sheets("SUMMARY DATA SHEET").Range("B4").value
Sheets("Invoice Number").Range("D" & lastRow).Value = Sheets("SUMMARY DATA SHEET").Range("F4").value
Sheets("Invoice Number").Range("E" & lastRow).Value = Sheets("SUMMARY DATA SHEET").Range("F5").value
End Sub
Lastly, typing out those worksheet names over and over again is cumbersome. We can use a couple of variables to hold the two worksheets we care about. This is nice if you ever want to change worksheet names as you only have one place in the code to make the change:
Sub Paste_NextRow()
'
' Paste_NextRow Macro
'
'
'Set some variables to hold our worksheets
Dim wsCopy as Worksheet
Dim wsPaste as Worksheet
Set wsCopy = Sheets("SUMMARY DATA SHEET")
Set wsPaste = Sheets("Invoice Number")
'Get the last used row into a variable
Dim lastRow as Long
lastRow = ThisWorkbook.Sheets("Invoice Number").Cells(Rows.Count, 2).End(xlUp).Row + 1
'Copy values over
wsPaste.Range("F" & LastRow).Value = wsCopy.Range("F3").value
wsPaste.Range("C" & lastRow).value = wsCopy.Range("F2").value
wsPaste.Range("B" & lastRow).Value = wsCopy.Range("B4").value
wsPaste.Range("D" & lastRow).Value = wsCopy.Range("F4").value
wsPaste.Range("E" & lastRow).Value = wsCopy.Range("F5").value
End Sub

Open workbook prompts multiple times

A few times a day I receive a file. I'm trying to automate it as much as possible and one part would include having the macro that lets you select a file to vlookup into (the file name is different every time). My macro runs, but for some reason it prompts you to select your file 3 times. I've tried a few variations on the code, but nothing worked. Does anyone have any insight as to why? It is prompting once when first opening the file, once when filling in the first cell with the formula, and again when the macro fills down column with the vlookup formula. I've pasted the relevant part below:
Dim MyFile As String
MyFile = Application.GetOpenFilename
Set firstWB = ActiveWorkbook
Set mySheet = ActiveSheet
Set wbLookup = Workbooks.Open(MyFile)
firstWB.Activate
mySheet.Range("T2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-18],'[wbLookup]tempemail'!R2C2:R123C20,19,0)"
Range("S1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Columns("t:t").EntireColumn.AutoFit
Columns("T:T").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
wbLookup.Close False
Range("U1").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("U1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False
Columns("u:u").EntireColumn.AutoFit
End Sub
Thanks!
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-18],'[wbLookup]tempemail'!R2C2:R123C20,19,0)"
This will not work unless wbLookup is literally the name of your file. Excel sees this and prompts you for the actual name.
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-18],'[" & wbLookup.Name & "]tempemail'!R2C2:R123C20,19,0)"
might work better
This:
Columns("T:T").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
...could be replaced by this:
Columns("T:T").Value = Columns("T:T").Value
A lot of selecting/activating is unneeded and is better avoided: How to avoid using Select in Excel VBA

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

Calling a sub within a loop

was trying to call a sub(trialMacro) from inside another sub(Macro2) but somehow it does not get activated. Is there a reason why? The call function works fine if its not within the loop. I.e. if I just do a plain call function under another sub. Is my coding right? I'm not sure if the loop disrupts the execution
' trial Macro
Sub trialMacro()
Dim PrevCell As Range
Set PrevCell = ActiveCell
SolverOk SetCell:=ActiveCell, MaxMinVal:=2, ValueOf:="0", ByChange:= _
"$M$2,$M$3,$M$5,$M$7"
SolverSolve UserFinish:=True
SolverFinish KeepFinal:=1
'Copy in sample and out of sample error
PrevCell.Resize(1, 3).Copy
'Paste Values of in sample and out of sample errors
PrevCell.Offset(0, 4).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copy Co-efficient
Range("M2:M7").Select
Application.CutCopyMode = False
Selection.Copy
'Select paste destination
PrevCell.Offset(0, 7).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'Copy Paste Following months data
PrevCell.Offset(1, -1).Resize(12, 1).Copy
'Select target destination
PrevCell.Offset(0, 13).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
PrevCell.Offset(1, 0).Select
End Sub
Sub Macro2()
'
' Macro2 Macro
Dim i, j As Integer
For i = 50 To 162
For j = 0 To 113
Sheets("Model v2 DUBDAT >0").Cells(i, 17).Select
Call trialMacro
Range("P50:BA50").Offset(j, 0).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Reference Sheet").Select
Range("D6").Offset(j, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Model v2 DUBDAT >0").Select
Next
Next
End Sub
Give this a read...
https://msdn.microsoft.com/en-us/library/office/gg251432.aspx
Remove call from this line
Call trialMacro
Should be..
trialMacro
If you use the call statement the subname must be followed by parentheses.