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

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

Related

Excel VBA to another workbook

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!

Copying specific ranges from one excel file and distribute them into their appropriate sheets in another workbook

I have a table in the "Source" file, range: AG5:AN5,AG6:AN6...AG16:AN16 and my task is to copy these data and paste them in the appropriate sheets in another "Destination" workbook with 12 sheets in it. Each sheet has the name. Although the range in the source workbook doesn't change, the data contained there changes on daily basis. Therefore these data should be copied not in one fixed cell but should slide down based on last filled-in cell. Copying range in the destination file will be started from "C6" and down. I did record macros and made small corrections but the problem is that when this task is performed workbooks are activated several times and it has a blinking effect. Is it possible to use loop in this example and how can I avoid activation of workbooks during copy-paste operation?
"GL Rates Calculation.xlsm" - Source file
"DPR_ALS_September_2017.xlsx" - Destination file
Here's my code:
Sub GL_DPR_FillIn()
Range("AG5:AN5").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch24").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG6:AN6").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch30").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG7:AN7").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch54").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG8:AN8").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch56").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG9:AN9").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch60 ").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG10:AN10").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch62").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG11:AN11").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch65").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG12:AN12").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch67").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG13:AN13").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch117").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG14:AN14").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch123").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG15:AN15").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch51").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
Range("AG16:AN16").Select
Selection.Copy
Windows("DPR_ALS_September_2017.xlsx").Activate
Sheets("Ch124").Select
lastRow = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row
ActiveSheet.Range("C" & lastRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Windows("GL Rates Calculation.xlsm").Activate
End Sub
Give this a try and let me know if you have any problems.
Sub GL_DPR_FillIn()
Dim wbA As Workbook
Dim wbB As Workbook
Dim wsName, lastRow
Dim j
Set wbA = Workbooks("GL Rates Calculation.xlsm") 'This one should already be open
Set wbB = Workbooks.Open("C:\pathname\DPR_ALS_September_2017.xlsx") 'Open the destination workbook
For j = 5 To 16 Step 1
wbA.Worksheets("GL").Range("AG" & j & ":" & "AN" & j).Copy
If j = 5 Then wsName = "Ch24"
If j = 6 Then wsName = "Ch30"
If j = 7 Then wsName = "Ch54"
If j = 8 Then wsName = "Ch56"
If j = 9 Then wsName = "Ch60"
If j = 10 Then wsName = "Ch62"
If j = 11 Then wsName = "Ch65"
If j = 12 Then wsName = "Ch67"
If j = 13 Then wsName = "Ch117"
If j = 14 Then wsName = "Ch123"
If j = 15 Then wsName = "Ch51"
If j = 16 Then wsName = "Ch124"
lastRow = Worksheets(wsName).Cells(Rows.Count, "C").End(xlUp).Row
Worksheets(wsName).Range("C" & lastRow + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Next j
'To Close the Destination Workbook, uncomment the following line
'wbB.Close
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

How can I shorten this particular bit of VBA code to make it smaller?

I've reached the point where I'm receiving a procedure too large errors, and it's because my code is very clunky. The section in question follows:
If patientsperrespondentpertimepoint = 1 Then
Sheets("Work").Select
Range("D2:D" & patientprofiles + 1).Select
Selection.Copy
Sheets("Output").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ElseIf patientsperrespondentpertimepoint = 2 Then
Sheets("Work").Select
Range("D2:D" & patientprofiles + 1).Select
Selection.Copy
Sheets("Output").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("Work").Select
Range("D" & patientprofiles + 2 & ":D" & 2 * patientprofiles + 1).Select
Selection.Copy
Sheets("Output").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ElseIf patientsperrespondentpertimepoint = 3 Then
Sheets("Work").Select
Range("D2:D" & patientprofiles + 1).Select
Selection.Copy
Sheets("Output").Select
Range("B2").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("Work").Select
Range("D" & patientprofiles + 2 & ":D" & 2 * patientprofiles + 1).Select
Selection.Copy
Sheets("Output").Select
Range("B3").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Sheets("Work").Select
Range("D" & 2 * patientprofiles + 2 & ":D" & 3 * patientprofiles + 1).Select
Selection.Copy
Sheets("Output").Select
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
This continues, and patientsperrespondentpertimepoint grows one by one from 3 to 4 to 5 all the way up to 12, and a corresponding copy and paste command is added at each step of the ladder. My question is, how can I shorten this? There's a lot of code being repeated, so I'm wondering if I can find a way to make it shorter, and more elegant to boot. Thanks!
Dim i As Long
For i = 0 To patientsperrespondentpertimepoint - 1
Worksheets("Work").Range("D" & (i * patientprofiles + 2) & ":D" & ((i + 1) * patientprofiles + 1)).Copy
Worksheets("Output").Range("B2").Offset(i, 0).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Next
Try this. There are some more optimizations that could be made, but this gives you an idea of what makes code more concise...
Sub Foo()
Dim shtWork As Worksheet
Dim shtOut As Worksheet
'I've qualified the workbook as ThisWorkbook, but you might want to be more specific if the sheets are in a different workbook
Set shtWork = ThisWorkbook.Sheets("Work")
Set shtOutput = ThisWorkbook.Sheets("Output")
If patientsperrespondentpertimepoint = 1 Then
shtWork.Range("D2:D" & patientprofiles + 1).Copy
shtOut.Range("B2").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ElseIf patientsperrespondentpertimepoint = 2 Then
shtWork.Range("D2:D" & patientprofiles + 1).Copy
shtOut.Range("B2").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
shtWork.Range("D" & patientprofiles + 2 & ":D" & 2 * patientprofiles + 1).Copy
shtOut.Range("B3").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
ElseIf patientsperrespondentpertimepoint = 3 Then
shtWork.Range("D2:D" & patientprofiles + 1).Copy
shtOut.Range("B2").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
shtWork.Range("D" & patientprofiles + 2 & ":D" & 2 * patientprofiles + 1).Copy
shtOut.Range("B3").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
shtWork.Range("D" & 2 * patientprofiles + 2 & ":D" & 3 * patientprofiles + 1).Copy
shtOut.Range("B4").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
'I've added a closing 'End If here
End If
End Sub

trouble with variable in Range.select

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