2015-11-03 46 views
0

我目前正在处理一个工作表,该工作表对A6(我没有包含在代码中)中输入初始数据(比如A1:A3)进行求和,然后将数据从A1:A3复制到B1:B3,从B1:B3的每个单元格中添加1,从B1:B3获取数据并重复此过程直到第十列。下面的代码显示了我尝试过的内容。碰到问题与来自连续列复制数据,当我运行以下行为:插入数据,复制结果并使结果生效活动单元格

LR = Cells.Find(What:="*",SearchDirection:=xlPrevious,SearchOrder:=xlByRows).Row 
LC = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column 
Cells(LR, LC).CurrentRegion.Select 

它选择而不是在数据(A1:A3)的求和值。代码:

Sub test() 

Dim LR As Long, LC As Long 
For X = 1 To 10 
Application.CutCopyMode = False 
Cells(1, X) = X + 1 
Cells(2, X) = X + 1 
Cells(3, X) = X + 1 
LR = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row 
LC = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column 
Cells(LR, LC).CurrentRegion.Select 
Selection.Copy 
Cells(LR - 2, LC + 1).PasteSpecial Paste:=xlPasteFormulas 
Next X 

End Sub 

回答

0

对不起,我写这作为一个答案(也许我得到你错了),但其多少它写在评论...

IM困惑......

Cells(1, X) = X + 1 
Cells(2, X) = X + 1 
Cells(3, X) = X + 1 

总是将A1:A3设置为2 ...还有A6中的公式总是把它粘贴到B4上......你想要它做些什么?

你看这样的事情:

Sub test() 
    Dim X As Byte 
    For X = 2 To 10 
    Cells(1, X) = Cells(1, X - 1) + 1 
    Cells(2, X) = Cells(2, X - 1) + 1 
    Cells(3, X) = Cells(3, X - 1) + 1 
    Cells(6, X - 1).Copy 
    Cells(6, X).PasteSpecial Paste:=xlPasteFormulas 
    Next X 
End Sub 

或本:

Sub test2() 
    Dim X As Byte 
    For X = 2 To 10 
    Cells(1, X) = Cells(1, X - 1) + 1 
    Cells(2, X) = Cells(2, X - 1) + 1 
    Cells(3, X) = Cells(3, X - 1) + 1 
    Cells(4, X) = Cells(6, X - 1).Value 
    Next X 
End Sub 

但是......每个解决方案仅仅通过1对每列增加了一排1到3的值 - > B1将是=A1 + 1,您可以将其复制并粘贴到范围B1:J3
也看起来B4只是=A6也可以复制粘贴(到C4:J4)

至少,我不知道你为什么要使用VBA这样的东西......也许我想念一件大事......