我有一个Excel问题分为两部分:首先,如果我想将数据从一个工作表复制到另一个工作表,那么Sheet 1具有:
A
B
C
D
E
对于Sheet 2我希望每个单元格重复三次,这样当我将前五个单元格粘贴到Sheet 2中时,每个单元格出现三次:
A
A
A
B
B
B
C
C
C
D
D
D
E
E
E
我该怎么做?我个人现在知道任何可以让我这样做的公式或功能,所以真的很期待你的建议。
一旦完成,有没有办法用VBA写它?我对VBA非常陌生,只是在考虑是否有可能。否则,我只会录制一个宏。非常感谢你!
答案 0 :(得分:4)
使用Sheet1中的数据:
在Sheet2中,单元格 A1 输入:
=INDEX(Sheet1!A:A,ROUNDUP(ROW()/3,0))
并复制下来:
(如果您想要每个数据项的4个副本,请在公式中使用 4 )
答案 1 :(得分:0)
VBA方式:
Option Explicit
Sub main()
Dim data As Variant, datum As Variant
Dim iDatum As Long, nTimes As Long
With Worksheets("Sheet 1") '<--| reference your "source" worksheet (change "Sheet 1" to your actual "source" sheet name
data = Application.Transpose(.Range("A1", .Cells(.Rows.count, 1).End(xlUp)).Value) '<--| store its column A cells values from row 1 down to last not empty one into an array
End With
nTimes = 3 '<--| set how many times you want to copy the same value
With Worksheets("Sheet 2") '<--| reference your "target" worksheet (change "Sheet 2" to your actual "target" sheet name
.Range("A1").Resize(nTimes) = data(LBound(data)) '<--| write 1st 'data' array value 'nTimes' from its cell A1 down
For iDatum = LBound(data) + 1 To UBound(data) '<--| loop through other 'data' array values
.Cells(.Rows.count, 1).End(xlUp).Offset(1).Resize(nTimes) = data(iDatum) '<--| write them 'nTimes' from first empty row after last not empty one down
Next iDatum
End With
End Sub
答案 2 :(得分:0)
最后,我采用老式的方式逐步完成它并且有效:
\set n :nrl
DROP FUNCTION km_test(integer, text);
CREATE FUNCTION km_test(n integer, t text)
RETURNS void AS
$BODY$
DECLARE
j smallint;
s integer;
BEGIN
DROP TABLE IF EXISTS km_test CASCADE;
CREATE UNLOGGED TABLE km_test (
lnr smallint,
km_id character varying(16),
flatenr smallint,
geo geometry(Linestring,25833)
);
j = 1;
WHILE j < n+1 LOOP
s:=j;
RAISE NOTICE 'Verdi j er : %', j;
RAISE NOTICE 'Tabell t er : %', t;
EXECUTE 'INSERT INTO km_test (lnr, km_id, flatenr, geo)
SELECT d.i,
p.km_id,
CAST(substring(p.flatenr from 5 for 4) AS smallint),
ST_MakeLine(p.geo,(ST_Translate(p.geo, d.dx, d.dy)))
FROM '||t||' as p, org_tre2.km_dxdy1 AS d
WHERE '||s||' = d.i';
j = j + 1;
END LOOP;
COMMENT ON TABLE org_tre2.km_test IS 'KM innsyn: n innsynslinjer for kulturminnepunkt utenfor IK i tre2-flater';
END;
$BODY$
LANGUAGE plpgsql;
\set ptab 'org_tre2.km_punkter_des2016'
select km_test(:n,:'ptab');