How to simplify a copy and paste function?

baumgarj

New Member
Joined
Aug 30, 2011
Messages
14
I have a requirement to copy the value of certain cells from a.CSV file to my workbook. The data to be copied resides in a single column but it is separated by dates. In any case I created the macro you see below (it works) and was wondering if there was a way to simplify the macro?</SPAN>


Sub SplitSkillInterval()</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
'*************************1******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F3:F50").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("B207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'*************************2******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F51:F98").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("C207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'************************3*******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F99:F146").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("D207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'************************4*******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F147:F194").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("E207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'************************5*******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F195:F242").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("F207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'************************6*******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F243:F290").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("G207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'************************7*******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F291:F338").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("H207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'************************8*******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F339:F386").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("I207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'************************9*******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F387:F434").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("J207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'************************10*******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F435:F482").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("K207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'*************************11******************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F483:F530").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("L207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'**************************12*****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F531:F578").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("M207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************13****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F579:F626").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("N207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************14****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F627:F674").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("O207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************15****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F675:F722").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("P207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************16****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F723:F770").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("Q207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************17****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F771:F818").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("R207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************18****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F819:F866").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("S207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************19****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F867:F914").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("T207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************20****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F915:F962").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("U207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'***************************21****************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F963:F1010").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("V207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************22***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1011:F1058").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("W207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************23***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1059:F1106").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("X207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************24***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1107:F1154").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("Y207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************25***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1155:F1202").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("Z207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************26***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1203:F1250").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("AA207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************27***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1251:F1298").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("AB207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************28***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1299:F1346").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("AC207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************29***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1347:F1394").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("AD207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************30***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1395:F1442").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("AE207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'****************************31***************************</SPAN>
Windows("Split-Skill-751.csv").Activate</SPAN>
Range("F1443:F1490").Select</SPAN>
Selection.Copy</SPAN>

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Range("AF207").Select</SPAN>
ActiveSheet.Paste</SPAN>
'*******************************************************</SPAN>
End Sub</SPAN>
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You could set you workbooks then use destinataion if the ranges are always the same.

Dim SourceWB as Workbook
Dim TargetWB as Workbook

'you can experiment with setting the workbooks there is likely a better way to do this, but I usually use the activeworkbook so.....

Windows("2013 - Call Distribution.xlsm").Activate</SPAN>
Set TargetWB = Activeworkbook
Windows("Split-Skill-751.csv").Activate</SPAN>
Set SourceWB = Activeworkbook

Sourcewb.Range("F3:F50").copy destination:= Targetwb.Range("B207")
Sourcewb.Range("F51:F98").copy destination:= Targetwb.Range("C207")
ect...

see if that helps.
 
Upvote 0
Something like this should get you started:

Code:
Sub SplitSkillInterval()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim j As Integer
Dim NumCols As Integer
Dim ColLetter As String
Dim i As Integer
Dim k As Integer

Set wb1 = Windows("Split-Skill-751.csv")
Set wb2 = Windows("2013 - Call Distribution.xlsm")

Set ws1 = wb1.Sheets(1)
Set ws2 = wb2.Sheets(2)

j = 1
NumCols = 2
i = 3
k = 50

Do Until j > 31

    ws1.Range("A" & i & ":A" & k).Copy
    
    'find column
    ColLetter = ColumnLetter(NumCols)
    ws2.Range(ColLetter & 2).PasteSpecial xlPasteAll
        
    j = j + 1
    i = i + 48
    k = i + 47
    
    NumCols = NumCols + 1

Loop

End Sub
Function ColumnLetter(ColumnNumber As Integer) As String
  If ColumnNumber > 26 Then

    ' 1st character:  Subtract 1 to map the characters to 0-25,
    '                 but you don't have to remap back to 1-26
    '                 after the 'Int' operation since columns
    '                 1-26 have no prefix letter

    ' 2nd character:  Subtract 1 to map the characters to 0-25,
    '                 but then must remap back to 1-26 after
    '                 the 'Mod' operation by adding 1 back in
    '                 (included in the '65')

    ColumnLetter = Chr(Int((ColumnNumber - 1) / 26) + 64) & _
                   Chr(((ColumnNumber - 1) Mod 26) + 65)
  Else
    ' Columns A-Z
    ColumnLetter = Chr(ColumnNumber + 64)
  End If
End Function

HTH,
Roger
 
Upvote 0
nice code rconverse, a pity the initiator hasn't been back since raising this, so it may be a whole heap of good work gone to waste.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,688
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top