将粘贴链接复制到用户input单元格引用

我犯了很多数据input错误,我正在想办法validation我的input。 我把值从单元格复制到另一个,我认为这将是最好的,如果我直接链接到单元格,然后让这些单元格自动着色。

这是我的build议:

Selection.Copy Selection.Interior.ColorIndex = 37 Set rng = Application.InputBox("Cell reference", Type:=8) 

现在我想不出将链接粘贴到input单元格引用的方法。 这似乎通过select一个单元格与input框,select丢失。

所以,你想要select一个单元格,并根据另一个单元格内容更改其内容,对吧? 您正在使用set语句创build对源单元的引用。 现在,您只需使用范围的.address属性来获取表示macros语言中的范围引用的string值(请参阅此属性的帮助)。

 Option Explicit Sub CopyingCellContents() Dim rng As Range Selection.Copy Selection.Interior.ColorIndex = 37 Set rng = Application.InputBox("Cell reference", Type:=8) Selection.Value = activesheet.range(rng.Address) End Sub 

提示:总是在您的代码中设置需求variables声明。

考虑到你的进一步解释和你自己的代码,我试图更新你的。

 Sub xxx Dim rng As Range Dim inp As Range Dim Sh as worksheet 'Worksheet where your range is. set Sh= workbooks("Name Of The Workbook").worksheets("Name Of The Worksheet") Set inp = Selection inp.Interior.ColorIndex = 37 Set rng = Application.InputBox("Copy to", Type:=8) sh.activate inp.Copy Destination:=rng, Link:=True End sub 

改变“工作簿的名称”和“工作表的名称”分别在工作簿和工作表的名称,你想操纵的范围是。 不要忘记使用“”。

我重写了一些代码。 这里是 :

 Dim rng As Range Dim inp As Range Selection.Interior.ColorIndex = 37 Set inp = Selection Set rng = Application.InputBox("Copy to", Type:=8) inp.Copy rng.Select ActiveSheet.Paste Link:=True 

这似乎工作,谢谢!