excel - Copying a variable based range of cells to another Range -
hello code should evident trying @ point. attempting copy range of cells static portion of worksheet created column keep running error on part of formula i'm hoping here has either solution error, or better method of taking 1 range of cells can static , bringing hard reference point
sub mapping() dim map worksheet dim ath worksheet dim lastmap long dim lastath 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 selection.interior .pattern = xlsolid .patterncolorindex = xlautomatic .themecolor = xlthemecolordark1 .tintandshade = -0.149998474074526 .patterntintandshade = 0 end clastath = ath.cells(1, columns.count).end(xltoleft).column x = clastath 1 step -1 if ath.cells(1, x) = "the principals book" 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
transfer data between ranges :
ath.range("d2: d" & lastath).value2 = ath.range(ath.cells(2, x), ath.cells(lastath, x)).value2
the main difference between these two :
.value2
gives underlying value of cell (unformatted data).value
gives formatted value of cell
for more details, take @ charles william's blog here.
as seem working on 2 sheets (not on "mapping" 1 in code gave, if got right. if not change ath.
map.
need be), don't forget use references created (i added them everywhere, before rows.count
, columns.count
avoid errors if open old document on new excel version)
i got rid of select
s , shorten code could, let "mapping" sheet guessed you'll use later in code.
also don't forget free abject variables this, when won't use afterwards :
set ath = nothing set map = nothing
here your code corrected, cleaned , tested :
sub mapping() dim map worksheet, _ ath worksheet, _ lastath long, _ lastmap long, _ clastath long, _ x integer set ath = sheets("athena greek god") set map = sheets("mapping") lastmap = map.cells(map.rows.count, "d").end(xlup).row lastath = ath.cells(ath.rows.count, "a").end(xlup).row ath.columns("a:d").insert shift:=xltoright, copyorigin:=xlformatfromleftorabove ath.range("a1:d1").value = array("edited", "edited 2", "edited 3", "edited 4") ath.columns("a:d").autofit ath.range("a1:d" & lastath).interior .pattern = xlsolid .patterncolorindex = xlautomatic .themecolor = xlthemecolordark1 .tintandshade = -0.149998474074526 .patterntintandshade = 0 end clastath = ath.cells(1, ath.columns.count).end(xltoleft).column x = clastath 1 step -1 if ath.cells(1, x) <> "the principals book" else ath.range("d2: d" & lastath).value = ath.range(ath.cells(2, x), ath.cells(lastath, x)).value end if next x set ath = nothing set map = nothing end sub
Comments
Post a Comment