I have looked for an answer to this but cannot find one that fits.
I have a two sheet excel spreadsheet which I wish to add a VBA macro to. The second sheet has column 1 with a number of skills, columns 2 to 13 have the "scores" for those skills with each column representing a month. Sheet 1 has a form where the following are selected from drop down lists.. skill being scored, month that the person was last assessed and month that the person is being assessed. I have managed to display the relevant "last month" score for the skill being assessed but now wish to copy the scores for all the skills from that month's column to the column representing the month that the person is being assessed. I will then overwrite just the specific skill score with the new data. This is what I've tried so far...
With Worksheets("Sheet2")
Range(Cells(2,Sheet1!D17),Cells(Sheet1!D19,Sheet1!D17).Copy Range(Cells(2,Sheet1!D20),Cells(Sheet1!D19,Sheet1!D20)
End With
Where D17 contains the column number of the column to be copied, D19 contains the calculated last row of the column, D20 contains the column number of the column where the data needs to be pasted. I've tried using cells on Sheet2 to contain these values in case it was a problem referencing Sheet1 but get the same problem. If I replace variables by numbers it works.
This looks to be a problem with using variables within the Cells() construct. Any pointers please?
I've since tried the following:
With Worksheets("Sheet1")
oldMonthCol = Range("D17").Value
newMonthCol = Range("D20").Value
lastRow = Range("D19").Value
End With
These variables were defined as Long prior to this code block. They were then used in the expression previously given to replace Sheet1!D17 etc. The code compiles but when I look at the values of oldMonthCol etc it is 0 when it should be a non-zero number. Any thoughts please?
Just found out that it is modifying Sheet1 despite having Worksheets set to Sheet2...
Solved this bit by using the following:
With Worksheets("Sheet2").Select
Range(Cells(2,oldMonthCol),Cells(lastRow,oldMonthCol).Copy Range(Cells(2,newMonthCol),Cells(lastRow,newMonthCol)
End With
This only works with values that I have entered into the cells rather than the result of a formula.
Solved! Sub Button2_Click()
Dim skillName As String
Dim newScore As Long
Dim skillNameRow As Long
Dim oldMonth As String
Dim newMonth As String
Dim oldMonthCol As Integer
Dim newMonthCol As Integer
Dim lastRow As Integer
Dim oldMonthCol2 As Integer
Dim newMonthCol2 As Integer
Dim lastRow2 As Integer
Dim skillNameRow2 As Integer
Dim newSkillValue As Integer
With Worksheets("Sheet1")
skillName = Range("D3").Value
skillNameRow = Range("D16").Value
skillNameRow2 = skillNameRow + 1
oldMonth = Range("G3").Value
newMonth = Range("G5").Value
oldMonthCol = Range("F17").Value
oldMonthCol2 = oldMonthCol + 1
newMonthCol = Range("F20").Value
newMonthCol2 = newMonthCol + 1
lastRow = Range("F19").Value
lastRow2 = lastRow + 1
newSkillValue = Range("F21").Value
End With
With Worksheets("Sheet2").Select
Range(Cells(2, oldMonthCol2), Cells(lastRow, oldMonthCol2)).Copy Range(Cells(2, newMonthCol2), Cells(lastRow, newMonthCol2))
Worksheets("Sheet2").Activate
Cells(skillNameRow2, newMonthCol2).Select
ActiveCell.Value = newSkillValue
End With
End Sub
Not elegant but it works. There are some variables that are not yet used but they are intended for a future add on...