Copy & Paste VBA

walkerl8

Board Regular
Joined
Apr 30, 2013
Messages
201
Hi All,

Does anyone have a suggestion to help get the following code to work. Debugger is highlighting Rng as variable not defined.

Hopefully the code is understandable but I am just trying to get those cells to select and copy in that worksheet and paste to the other worksheet.

Thanks in advance,

Code:
Sub Recall_From_Database()

'This code goes to the database and returns a stored entry to the template
'instead of referencing each cell this code copies and pastes three sections


Dim RR As Long 'RR stands for Recall Row - the row in the DB where the entry will be recalled from
Dim Database As Worksheet
Dim Template As Worksheet
Dim ExNo, Category, Exercise, Sets, Reps, Load As Long


Set Database = Sheet4
Set Template = Sheet3


'Find the last
Database.Select
RR = Database.Cells(3, 4).Value


'Recall the Descriptors
Template.Cells(2, 26) = Database.Cells(RR, 3).Value 'Athlete Name
Template.Cells(3, 26) = Database.Cells(RR, 4).Value 'Period
Template.Cells(4, 32) = Database.Cells(RR, 5).Value 'Phase Objective


'
''Recall the ExNo
'select and copy the 21 columns from the database
Set Rng = Nothing
For x = 1 To 21
    ExNo = Choose(x, 14, 20, 26, 32, 38, 44, 50, 56, 62, 68, 74, 80, 86, 92, 98, 104, 110, 116, 122, 128, 134)
    If x = 1 Then
        Set Rng = Cells(RR, ExNo)
    Else
        Set Rng = Union(Rng, Cells(RR, ExNo))
    End If
Next x
Rng.Copy
Template.Select
Cells(9, 1).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=True
    Application.CutCopyMode = False


''Recall the Category
'select and copy the 21 columns from the database
Set Rng = Nothing
For x = 1 To 21
    Category = Choose(x, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105, 111, 117, 123, 129, 135)
    If x = 1 Then
        Set Rng = Cells(RR, Category)
    Else
        Set Rng = Union(Rng, Cells(RR, Category))
    End If
Next x
Rng.Copy
Template.Select
Cells(9, 2).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=True
    Application.CutCopyMode = False
    
''Recall the Exercises
'select and copy the 21 columns from the database
Set Rng = Nothing
For x = 1 To 21
    Exercise = Choose(x, 16, 22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94, 100, 106, 112, 118, 124, 130, 136)
    If x = 1 Then
        Set Rng = Cells(RR, Exercise)
    Else
        Set Rng = Union(Rng, Cells(RR, Exercise))
    End If
Next x
Rng.Copy
Template.Select
Cells(9, 3).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=True
    Application.CutCopyMode = False
        
''Recall the Sets
'select and copy the 21 columns from the database
Set Rng = Nothing
For x = 1 To 21
    Sets = Choose(x, 17, 23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 83, 89, 95, 101, 107, 113, 119, 125, 131, 137)
    If x = 1 Then
        Set Rng = Cells(RR, Sets)
    Else
        Set Rng = Union(Rng, Cells(RR, Sets))
    End If
Next x
Rng.Copy
Template.Select
Cells(9, 5).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=True
    Application.CutCopyMode = False
    
''Recall the Reps
'select and copy the 21 columns from the database
Set Rng = Nothing
For x = 1 To 21
    Reps = Choose(x, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 102, 108, 114, 120, 126, 132, 138)
    If x = 1 Then
        Set Rng = Cells(RR, Reps)
    Else
        Set Rng = Union(Rng, Cells(RR, Reps))
    End If
Next x
Rng.Copy
Template.Select
Cells(9, 6).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=True
    Application.CutCopyMode = False
    
''Recall the Load
'select and copy the 21 columns from the database
Set Rng = Nothing
For x = 1 To 21
    Load = Choose(x, 19, 25, 31, 37, 43, 49, 55, 61, 67, 73, 79, 85, 91, 97, 103, 109, 115, 121, 127, 133, 139)
    If x = 1 Then
        Set Rng = Cells(RR, Load)
    Else
        Set Rng = Union(Rng, Cells(RR, Load))
    End If
Next x
Rng.Copy
Template.Select
Cells(9, 8).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=True
    Application.CutCopyMode = False
        
End Sub
 
As I stated I don't use merged cells but I will be very surprised if it is the paste range that is the issue as your error you have stated is on Rng.Copy which is before the paste action.
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
I appreciate you don't use merged cells - I am not using them either so do you have any idea why Rng.Copy is causing an error? I am literally copying from a row of individual cells - simply selecting the range of cells I want and copying that
 
Upvote 0
I appreciate you don't use merged cells - I am not using them either

I have ran your code multiple times on a workbook with no merged cells and get no error.
I then put some merged cells on the Template sheet and got the error you described but not on the line you stated.
It erred on
Code:
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=True


and no I don't have any suggestions.
 
Upvote 0
Does anyone know why the Run Time Error 1004 - Cannot Do That To A Merge Cell is occurring in the above code? I am not trying to copy from any merged cells and as far as I can tell I am not trying to paste into any. The sheet on I am pasting to has merged cells but shouldnt be involved in any of the code

The only thing to give an error like that is merged cells.

You're probably not copying what you think you are.
Not specifying what sheet you're copying from means you're using the active sheet.
Using Select to get to the paste location will be changing the active sheet.

Very seldom do you need to use Select.
 
Upvote 0
Code:
Sub Recall_From_Database()

'This code goes to the database and returns a stored entry to the template
'instead of referencing each cell this code copies and pastes three sections


Dim RR As Long 'RR stands for Recall Row - the row in the DB where the entry will be recalled from
Dim Database As Worksheet, x As Long
Dim Templates As Worksheet, Rng As Range
Dim ExNo, Category, Exercise, Sets, Reps, Load As Long


Set Database = Sheet4
Set Templates = Sheet3


'Find the last
Database.Select
RR = Database.Cells(3, 4).Value


'Recall the Descriptors
Templates.Cells(2, 26) = Database.Cells(RR, 3).Value 'Athlete Name
Templates.Cells(3, 26) = Database.Cells(RR, 4).Value 'Period
Templates.Cells(4, 32) = Database.Cells(RR, 5).Value 'Phase Objective


'Recall the Exercises and Relevant Sets, Reps, Load
Templates.Cells(9, 1) = Database.Cells(RR, 14).Value 'ExNo1
Templates.Cells(9, 2) = Database.Cells(RR, 15).Value 'Category1
Templates.Cells(9, 3) = Database.Cells(RR, 16).Value 'Exercise1
Templates.Cells(9, 5) = Database.Cells(RR, 17).Value 'Set1
Templates.Cells(9, 6) = Database.Cells(RR, 18).Value 'Rep1
Templates.Cells(9, 8) = Database.Cells(RR, 19).Value 'Load1
Templates.Cells(10, 1) = Database.Cells(RR, 20).Value 'ExNo2
Templates.Cells(10, 2) = Database.Cells(RR, 21).Value 'Category2
Templates.Cells(10, 3) = Database.Cells(RR, 22).Value 'Exercise2
Templates.Cells(10, 5) = Database.Cells(RR, 23).Value 'Set2
Templates.Cells(10, 6) = Database.Cells(RR, 24).Value 'Rep2
Templates.Cells(10, 8) = Database.Cells(RR, 25).Value 'Load2
Templates.Cells(11, 1) = Database.Cells(RR, 26).Value 'ExNo3
Templates.Cells(11, 2) = Database.Cells(RR, 27).Value 'Category3
Templates.Cells(11, 3) = Database.Cells(RR, 28).Value 'Exercise3
Templates.Cells(11, 5) = Database.Cells(RR, 29).Value 'Set3
Templates.Cells(11, 6) = Database.Cells(RR, 30).Value 'Rep3
Templates.Cells(11, 8) = Database.Cells(RR, 31).Value 'Load3
Templates.Cells(12, 1) = Database.Cells(RR, 32).Value 'ExNo4
Templates.Cells(12, 2) = Database.Cells(RR, 33).Value 'Category4
Templates.Cells(12, 3) = Database.Cells(RR, 34).Value 'Exercise4
Templates.Cells(12, 5) = Database.Cells(RR, 35).Value 'Set4
Templates.Cells(12, 6) = Database.Cells(RR, 36).Value 'Rep4
Templates.Cells(12, 8) = Database.Cells(RR, 37).Value 'Load4
Templates.Cells(13, 1) = Database.Cells(RR, 38).Value 'ExNo5
Templates.Cells(13, 2) = Database.Cells(RR, 39).Value 'Category5
Templates.Cells(13, 3) = Database.Cells(RR, 40).Value 'Exercise5
Templates.Cells(13, 5) = Database.Cells(RR, 41).Value 'Set5
Templates.Cells(13, 6) = Database.Cells(RR, 42).Value 'Rep5
Templates.Cells(13, 8) = Database.Cells(RR, 43).Value 'Load5
Templates.Cells(14, 1) = Database.Cells(RR, 44).Value 'ExNo6
Templates.Cells(14, 2) = Database.Cells(RR, 45).Value 'Category6
Templates.Cells(14, 3) = Database.Cells(RR, 46).Value 'Exercise6
Templates.Cells(14, 5) = Database.Cells(RR, 47).Value 'Set6
Templates.Cells(14, 6) = Database.Cells(RR, 48).Value 'Rep6
Templates.Cells(14, 8) = Database.Cells(RR, 49).Value 'Load6
Templates.Cells(15, 1) = Database.Cells(RR, 50).Value 'ExNo7
Templates.Cells(15, 2) = Database.Cells(RR, 51).Value 'Category7
Templates.Cells(15, 3) = Database.Cells(RR, 52).Value 'Exercise7
Templates.Cells(15, 5) = Database.Cells(RR, 53).Value 'Set7
Templates.Cells(15, 6) = Database.Cells(RR, 54).Value 'Rep7
Templates.Cells(15, 8) = Database.Cells(RR, 55).Value 'Load7
Templates.Cells(16, 1) = Database.Cells(RR, 56).Value 'ExNo8
Templates.Cells(16, 2) = Database.Cells(RR, 57).Value 'Category8
Templates.Cells(16, 3) = Database.Cells(RR, 58).Value 'Exercise8
Templates.Cells(16, 5) = Database.Cells(RR, 59).Value 'Set8
Templates.Cells(16, 6) = Database.Cells(RR, 60).Value 'Rep8
Templates.Cells(16, 8) = Database.Cells(RR, 61).Value 'Load8
Templates.Cells(17, 1) = Database.Cells(RR, 62).Value 'ExNo9
Templates.Cells(17, 2) = Database.Cells(RR, 63).Value 'Category9
Templates.Cells(17, 3) = Database.Cells(RR, 64).Value 'Exercise9
Templates.Cells(17, 5) = Database.Cells(RR, 65).Value 'Set9
Templates.Cells(17, 6) = Database.Cells(RR, 66).Value 'Rep9
Templates.Cells(17, 8) = Database.Cells(RR, 67).Value 'Load9
Templates.Cells(18, 1) = Database.Cells(RR, 68).Value 'ExNo10
Templates.Cells(18, 2) = Database.Cells(RR, 69).Value 'Category10
Templates.Cells(18, 3) = Database.Cells(RR, 70).Value 'Exercise10
Templates.Cells(18, 5) = Database.Cells(RR, 71).Value 'Set10
Templates.Cells(18, 6) = Database.Cells(RR, 72).Value 'Rep10
Templates.Cells(18, 8) = Database.Cells(RR, 73).Value 'Load10
Templates.Cells(19, 1) = Database.Cells(RR, 74).Value 'ExNo11
Templates.Cells(19, 2) = Database.Cells(RR, 75).Value 'Category11
Templates.Cells(19, 3) = Database.Cells(RR, 76).Value 'Exercise11
Templates.Cells(19, 5) = Database.Cells(RR, 77).Value 'Set11
Templates.Cells(19, 6) = Database.Cells(RR, 78).Value 'Rep11
Templates.Cells(19, 8) = Database.Cells(RR, 79).Value 'Load11
Templates.Cells(20, 1) = Database.Cells(RR, 80).Value 'ExNo12
Templates.Cells(20, 2) = Database.Cells(RR, 81).Value 'Category12
Templates.Cells(20, 3) = Database.Cells(RR, 82).Value 'Exercise12
Templates.Cells(20, 5) = Database.Cells(RR, 83).Value 'Set12
Templates.Cells(20, 6) = Database.Cells(RR, 84).Value 'Rep12
Templates.Cells(20, 8) = Database.Cells(RR, 85).Value 'Load12
Templates.Cells(21, 1) = Database.Cells(RR, 86).Value 'ExNo13
Templates.Cells(21, 2) = Database.Cells(RR, 87).Value 'Category13
Templates.Cells(21, 3) = Database.Cells(RR, 88).Value 'Exercise13
Templates.Cells(21, 5) = Database.Cells(RR, 89).Value 'Set13
Templates.Cells(21, 6) = Database.Cells(RR, 90).Value 'Rep13
Templates.Cells(21, 8) = Database.Cells(RR, 91).Value 'Load13
Templates.Cells(22, 1) = Database.Cells(RR, 92).Value 'ExNo14
Templates.Cells(22, 2) = Database.Cells(RR, 93).Value 'Category14
Templates.Cells(22, 3) = Database.Cells(RR, 94).Value 'Exercise14
Templates.Cells(22, 5) = Database.Cells(RR, 95).Value 'Set14
Templates.Cells(22, 6) = Database.Cells(RR, 96).Value 'Rep14
Templates.Cells(22, 8) = Database.Cells(RR, 97).Value 'Load14
Templates.Cells(23, 1) = Database.Cells(RR, 98).Value 'ExNo15
Templates.Cells(23, 2) = Database.Cells(RR, 99).Value 'Category15
Templates.Cells(23, 3) = Database.Cells(RR, 100).Value 'Exercise15
Templates.Cells(23, 5) = Database.Cells(RR, 101).Value 'Set15
Templates.Cells(23, 6) = Database.Cells(RR, 102).Value 'Rep15
Templates.Cells(23, 8) = Database.Cells(RR, 103).Value 'Load15
Templates.Cells(24, 1) = Database.Cells(RR, 104).Value 'ExNo16
Templates.Cells(24, 2) = Database.Cells(RR, 105).Value 'Category16
Templates.Cells(24, 3) = Database.Cells(RR, 106).Value 'Exercise16
Templates.Cells(24, 5) = Database.Cells(RR, 107).Value 'Set16
Templates.Cells(24, 6) = Database.Cells(RR, 108).Value 'Rep16
Templates.Cells(24, 8) = Database.Cells(RR, 109).Value 'Load16
Templates.Cells(25, 1) = Database.Cells(RR, 110).Value 'ExNo17
Templates.Cells(25, 2) = Database.Cells(RR, 111).Value 'Category17
Templates.Cells(25, 3) = Database.Cells(RR, 112).Value 'Exercise17
Templates.Cells(25, 5) = Database.Cells(RR, 113).Value 'Set17
Templates.Cells(25, 6) = Database.Cells(RR, 114).Value 'Rep17
Templates.Cells(25, 8) = Database.Cells(RR, 115).Value 'Load17
Templates.Cells(26, 1) = Database.Cells(RR, 116).Value 'ExNo18
Templates.Cells(26, 2) = Database.Cells(RR, 117).Value 'Category18
Templates.Cells(26, 3) = Database.Cells(RR, 118).Value 'Exercise18
Templates.Cells(26, 5) = Database.Cells(RR, 119).Value 'Set18
Templates.Cells(26, 6) = Database.Cells(RR, 120).Value 'Rep18
Templates.Cells(26, 8) = Database.Cells(RR, 121).Value 'Load18
Templates.Cells(27, 1) = Database.Cells(RR, 122).Value 'ExNo19
Templates.Cells(27, 2) = Database.Cells(RR, 123).Value 'Category19
Templates.Cells(27, 3) = Database.Cells(RR, 124).Value 'Exercise19
Templates.Cells(27, 5) = Database.Cells(RR, 125).Value 'Set19
Templates.Cells(27, 6) = Database.Cells(RR, 126).Value 'Rep19
Templates.Cells(27, 8) = Database.Cells(RR, 127).Value 'Load19
Templates.Cells(28, 1) = Database.Cells(RR, 128).Value 'ExNo20
Templates.Cells(28, 2) = Database.Cells(RR, 129).Value 'Category20
Templates.Cells(28, 3) = Database.Cells(RR, 130).Value 'Exercise20
Templates.Cells(28, 5) = Database.Cells(RR, 131).Value 'Set20
Templates.Cells(28, 6) = Database.Cells(RR, 132).Value 'Rep20
Templates.Cells(28, 8) = Database.Cells(RR, 133).Value 'Load20
Templates.Cells(29, 1) = Database.Cells(RR, 134).Value 'ExNo21
Templates.Cells(29, 2) = Database.Cells(RR, 135).Value 'Category21
Templates.Cells(29, 3) = Database.Cells(RR, 136).Value 'Exercise21
Templates.Cells(29, 5) = Database.Cells(RR, 137).Value 'Set21
Templates.Cells(29, 6) = Database.Cells(RR, 138).Value 'Rep21
Templates.Cells(29, 8) = Database.Cells(RR, 139).Value 'Load21


End Sub

Above is the code I have had to do. As you can see because I couldn't get the previous code to work (which you can see earlier on in the thread) - I have had to resort to simply copying the entire row of data from Sheet4 (Database) and pasting manually back into Sheet3 (Program Template).

Alls I was trying to do was simplify the code so that I could pull the cells containing ExNo. -Category etc. and paste as previously mentioned.

Any help would be great,
 
Upvote 0
Are your merged cells all horizontally merged and is it done purely for appearance?
 
Upvote 0
Mark,

The sheet I am recalling the data from doesn't have any merged cells.

The sheet I am pasting to has merged cells within the sheet, but not in the ranges I am pasted to. The merged cells on that sheet however are all horizontal.

Below is a link to the workbook if you fancy taking a look to understand the problem.

On the 'Programme Template' sheet - if you use the drop downs under each of the headers to create a basic programme and click 'Save Entry' you will see it goes to the database sheet.

On the database sheet if you then type the relevant row number in the recall row box and then return to the programme template sheet and click recall entry - you will the data is recalled.

However, if you look at the code it is long winded as I said because it is looking at every cell. I was just trying to simplify the code you see there using something like a posted earlier in the thread.

If an error occurs upon opening the sheet - don't worry its just that I am linking to another workbook on my computer.

https://www.dropbox.com/s/0umosuzikohwb4x/S&C Database.xlsm?dl=0
 
Last edited:
Upvote 0
Can't look at the file until I get in tonight but is centre across selection an option rather than merging the cells in the destination sheet?

To test it manually to see if it does look like an option unmerge one of the merged cells, select the cells that were merged, right click, format, alignment then in the horizontal alignment scroll down and you will find centre across selection.

If it is an option I do have a macro at home which will convert all the merged cell (if there are a lot of merged cells) and it is an option.
 
Last edited:
Upvote 0
Yeah take a look at the file when you get 5 minutes.

I have had a look at centre across selection and the problem I have is I may have a column that is exercise - a column next to it that is sets a column next to it that is reps which are all across different merged cells - when I centre them all across selection I am then unable to apply my borders to separate them and they cross closely to eachother
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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