Hello from my code it should be evident what I am trying to do at this point. I am attempting to copy a range of cells from a static portion of a worksheet to a created column but I keep running into an error on a certain part of the formula I'm hoping that someone here has either a solution to the error, or a better method of taking one range of cells that can be static and bringing to a hard a reference point
Sub Mapping()
Dim Map As Worksheet
Dim Ath As Worksheet
Dim lastmap As Long
Dim lastath As Long
Set Ath = Sheets("Athena Greek God")
Set Map = Sheets("Mapping")
lastmap = Map.Cells(Rows.Count, "D").End(xlUp).Row
lastath = Ath.Cells(Rows.Count, "A").End(xlUp).Row
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1") = "EDITED"
Range("B1") = "EDITED 2"
Range("C1") = "EDITED 3"
Range("D1") = "EDITED 4"
Columns("A:D").AutoFit
Range("A1:D" & lastath).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Clastath = Ath.Cells(1, Columns.Count).End(xlToLeft).Column
For x = Clastath To 1 Step -1
If ath.Cells(1, x) = "The Principals Book" Then
ath.Range("D2: D" & lastath) = ath.Range(ath.Cells(2, x), ath.Cells(lastath, x))
End If
Next
End Sub
Error occurs here:
ath.Range("D2: D" & lastath) = ath.Range(ath.Cells(2, x), ath.Cells(lastath, x))
You should use
.Value
or.Value2
to transfer data between to ranges like this :The main difference between these two is :
.Value2
gives you the underlying value of the cell (unformatted data).Value
gives you the formatted value of the cellFor more details, take a look at Charles William's blog here.
As you seem to be working on two sheets (not on the "mapping" one in the code you gave, if I got that right. If not just change
Ath.
toMap.
where it need to be), don't forget to use the references you created (I added them everywhere, even beforeRows.Count
andColumns.Count
to avoid errors if you open an old document on a new Excel version)I got rid of the
Select
s and shorten code where I could, but I let the "Mapping" sheet as I guessed you'll use it later in your code.Also don't forget to free your abject variables like this, when you won't use it afterwards :
Here is your code corrected, cleaned and tested :
Your code deserves several comments. To begin with, you have to solve your problem (see point 1). In addition, several points can reduce the chances of error upon modification, and improve efficiency.
Use other methods for copying
Range
s.You have to specify what you want to copy (data, formulas, number formats, etc.) to decide which method to use.
Copy only data.
or
Copy (part or all of) number formats. See this.
or
Copy formulas.
Copy all.
or
Fully qualify your
Range
s.This issue shows up once and again (e.g., this).
What does this mean? Do not use
Cells
,Range
,Rows
orColumns
without specifying whichWorksheet
they belong to, unless you specifically want to do that (and even in that case, explicitly usingActiveSheet
improves readability and reduces the chances of errors, similar to usingOption Explicit
). For instance,will take
Rows.Count
from theActiveSheet
, which might not beAth
. You likely do not want that. The correct form isFix all other code. Note: in this case, code continues execution and the mistake may go unnoticed, as it produces a valid result. In other cases, code without fully qualified
Range
s will throw an error (e.g., with something likesheet1.Range(Cells(...
, whensheet1
is not theActiveSheet
).Your code seems inefficient.
You might be copying data many times into the same
Range
. It is better to find the leftmost cell in row 1, containing"The Principals Book"
, and copy the range for that column intoRange("D2:D" & lastath)
. UseIt is not clear in which
Worksheet
you want columns inserted.It seems to be
Ath
. The otherWorksheet
is not used.You can insert many columns at once. You can also enter data into a range at once.
Actually, it is not really evident what this code should achieve, tell you why: Two worksheets are defined but only one of them is used, also It's not clear to which worksheet the code is to be applied. As it stand now, the code is applied to whatever worksheet is active.
See the code below with adjustments and comments. The code assumes the procedure should be apply to the Ath worksheet (change as needed)
Although the changes are explained, do let me know of any questions you might have about.
1.Remove the space in your string address: Before:
After:
2a. If you only want to copy the values, then use .value at the end of your range references: Before:
After:
2b. If you want values and formats then use .copy (destination): Before:
After:
Also, you should always reference the worksheet of a references range (e.g.
ws.range("A1").value
). You might also consider using a worksheet's .codename rather then .name if this isn't just a quick an dirty project.Take out the space after the :
I have also chopped your code down, Dimmed X and removed the selects for you:
Edit: Also dimmed Clastath as long