简体   繁体   English

将数据粘贴到VBA-Excel中的可变位置

[英]Pasting data in variable locations in VBA-Excel

I am trying to copy data from one Excel sheet to another, but the pasting destination needs to be a variable. 我正在尝试将数据从一个Excel工作表复制到另一个工作表,但是粘贴目标需要是一个变量。

Essentially, what I want to do is open two excel sheets. 本质上,我想做的是打开两个Excel工作表。 On the first sheet, there will be some string, whose coordinates I save like this: 在第一张纸上,将有一些字符串,我将其保存如下:

sCurrentAddress = Selection.Address(True, True, xlA1, True)

Based on the data already populating Sheet 2, an algorithm neatly finds the desired pasting location and saves these coordinates as follows: 基于已经在Sheet 2中填充的数据,算法会巧妙地找到所需的粘贴位置,并按以下方式保存这些坐标:

sDestinationAddress = Selection.Address(True, True, xlA1, True)

Then what I'd like to do is paste the information from the first sheet into the second as follows: 然后,我想做的是将信息从第一张纸粘贴到第二张纸,如下所示:

Selection.Copy (sDestinationAddress) but I receive error 1004: Runtime Error: Select method of range class failed. Selection.Copy (sDestinationAddress)但收到错误1004:运行时错误:范围类的选择方法失败。

Now, the first value I'm working on happens to be A4, and when I hover my mouse over "sDestinationAddress", it shows me that the value saved in sDestinationAddress is '[exampleFile.xlsx]Sheet 1'!$A$4 现在,我要处理的第一个值恰好是A4,当我将鼠标悬停在“ sDestinationAddress”上时,它显示出保存在sDestinationAddress中的值是'[exampleFile.xlsx]Sheet 1'!$A$4

The macro fails unless I plug in the actual value like this: 除非我这样插入实际值,否则宏将失败:

'Selection.Copy (Worksheets(1).Range("A4"))

What do I need to do to use my sDestinationAddress as the argument for Selection.Copy? 要使用sDestinationAddress作为Selection.Copy的参数,我需要做什么?


(I'm sorry this lengthy and still inarticulate, yesterday was literally my first time looking at visual basic.) (很抱歉,这冗长而又不清晰,昨天确实是我第一次看视觉基础。)

About the algorithm: I have 144 different headers to label the information in the 6 rows below. 关于算法:我有144个不同的标头来标记下面6行中的信息。 For example, I'm moving the data from a column headed: Inventory_S1_1 to a new spreadsheet. 例如,我将数据从标题为Inventory_S1_1的列移动到新的电子表格。 'S1_1' on this one sheet implies for the other sheet: S=Diamond, 1 = Black, _1 = Player 1. So my algorithm finds a header it hasn't dealt with yet (suppose here Inventory_S1_1 is the first header it hasn't dealt with), copies the column underneath the header, finds the location on the new spreadsheet corresponding to Player 1's Black Diamond, then pastes. 一张纸上的'S1_1'代表另一张纸:S = Diamond,1 =黑色,_1 =玩家1。因此,我的算法找到了一个尚未处理的标头(假设Inventory_S1_1是它尚未使用的第一个标头) (已处理),复制标题下的列,在新电子表格中找到与播放器1的“黑钻石”相对应的位置,然后粘贴。 The pasting algorithm works fine since the template was made by hand, but the searching algorithm gets lost looking for the headers, since it searches potentially jumbled data. 由于模板是手工制作的,因此粘贴算法效果很好,但是由于搜索算法可能会发现混乱的数据,因此搜索算法无法找到标头。

The problem is returning back to the sheet with the raw data on it. 问题是返回到包含原始数据的工作表。
Hovering my mouse over where I call set sCurrentAddress = Selection shows that sCurrentAddress= Inventory_S1_2 once I've moved on to the second header, and then after I've pasted the sDestinationAddress = 0 将鼠标悬停在我叫set sCurrentAddress = Selection显示sCurrentAddress= Inventory_S1_2一旦我移至第二个标头,然后粘贴sDestinationAddress = 0
And I'm fairly sure this is the exactly the problematic line: 我相当确定这是有问题的行:

Set rCurrentRange = .Find(What:=sCurrentAddress, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) which I follow with Set rCurrentRange = .Find(What:=sCurrentAddress, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)

Application.Goto rCurrentRange, True ActiveCell.Select Selection.Offset(-1, 1).Select Set sCurrentAddress = Selection

It's with this line that I'm looking for the next header. 我正在此行中寻找下一个标题。 When I was trying to use exact coordinates, this .Find seemed to work, but now it .Finds the first instance of 0 (sDestinationAddress), which was the first value in the copied material. 当我尝试使用精确的坐标时,这个.Find似乎可以工作,但是现在它.Find可以找到0的第一个实例(sDestinationAddress),这是复制材料中的第一个值。 Since it's always copying 0, then looking for the first 0, it never Finds the next header. 因为它总是复制0,然后寻找第一个0,所以它从不查找下一个标头。

I realize that the Range Object has an .Address property, but I don't know if it would be helpful in this case. 我意识到Range对象具有.Address属性,但是我不知道在这种情况下是否有帮助。

You can just use the ranges directly instead of using the Address 您可以直接使用范围而不是使用地址

Dim rngSrc as Range, rngDest as range

Set rngSrc = Selection
'do your destination-finding thing
Set rngDest = Selection

rngSrc.Copy rngDest 'no need for parentheses here

EDIT 编辑

You rarely need to select ranges to work with them, and storing the actual range object in a variable is nearly always better than trying to work with range adresses. 您很少需要选择范围来使用它们,并且将实际范围对象存储在变量中几乎总是比尝试使用范围地址更好。

If you need to keep the coordinates of a particular cell or range, then storing it in a range variable is the easiest approach. 如果需要保留特定单元格或范围的坐标,则将其存储在范围变量中是最简单的方法。

One thing to keep in mind is that the default property of a range object is "Value" - this would explain why you see a range's value when hovering your cursor over a range variable when stepping through code - it's the Value which is displayed. 要记住的一件事是,范围对象的默认属性是“值”-这可以解释为什么在单步执行代码时将光标悬停在范围变量上时会看到范围的值-显示的是值。

If you're just copying cell values from one place to another then you don't even need to use .Copy if the two ranges are of the same dimensions: 如果您只是将单元格值从一个位置复制到另一个位置,则即使两个范围的尺寸相同,您甚至不需要使用.Copy

rngDestination.Value = rngSource.Value 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM