简体   繁体   English

Excel VBA-将变量的值粘贴到单元格中

[英]Excel VBA - Paste a variable's value into a cell

I have a For loop which will find a cell with a certain value in it (eg the word "Item"). 我有一个For循环,它将在其中找到具有某个值的单元格(例如单词“ Item”)。 Once it finds this cell, I want to paste a value into the row below it, in column I. 找到该单元格后,我想将值粘贴到它下面的第一列中的行中。

For example, if the For loop finds "Item" in cell A1, I want to paste the value of a variable into cell I2. 例如,如果For循环在单元格A1中找到“项目”,我想将变量的值粘贴到单元格I2中。

I'd like to be able to do this with both a Long variable and a String variable. 我希望能够同时使用Long变量和String变量。

Right now I'm trying something like: 现在我正在尝试类似的东西:

Cells(i.Offset(1, 0), 9).Value = myLongVariable

or 要么

Cells(i.Offset(1, 0), 9).Text = myStringVariable

and other variations, but I keep getting an overflow error at this line. 和其他变体,但在此行中我不断收到溢出错误。

I've tried this: Range("I" & i.Offset(1, 0)).Value = myLongVariable and slight variations, but I get an error that says Run-time error 1004: Method Range of object _Worksheet failed 我已经尝试过: Range("I" & i.Offset(1, 0)).Value = myLongVariable并稍有变化,但是我收到一条错误,指出Run-time error 1004: Method Range of object _Worksheet failed

Or workBook.Sheets("Sheet1").Range("I" & i.Offset(1, 0)).Value = myLongVariable but that gives Run-time error 1004: Application-defined or object-defined error . workBook.Sheets("Sheet1").Range("I" & i.Offset(1, 0)).Value = myLongVariable但会给出Run-time error 1004: Application-defined or object-defined error Not sure what that refers to since I've defined workBook and i and myLongVariable . 由于我已经定义了workBook以及imyLongVariable ,因此不确定所指的是什么。

As a slightly different approach, I've tried pasting the variable data into the next blank cell of the row, which happens to be the cell in column I. Here's the code I used for that: 'i.Offset(1, 0).End(xlToRight).Offset(0, 1).Value = myLongVariable . 作为一种略有不同的方法,我尝试将变量数据粘贴到该行的下一个空白单元格中,该空白单元'i.Offset(1, 0).End(xlToRight).Offset(0, 1).Value = myLongVariable是列I中的单元格。这是我用于此的代码: 'i.Offset(1, 0).End(xlToRight).Offset(0, 1).Value = myLongVariable It works great, except in this scenario: cells A1:C1 are blank, D1:H1 have values. 效果很好,除了在这种情况下:单元格A1:C1为空,D1:H1具有值。 It should then paste the value into I1, but instead it thinks D1 is the end and posts it there. 然后,应将值粘贴到I1中,但相反,它认为D1是结束并将其发布到那里。 Any ideas on that? 有什么想法吗?

Thanks! 谢谢!

Its wrong to use offset here, because you are not referencing a cell, 在此处使用偏移量是错误的,因为您没有引用单元格,

i is just a number, so you cant offset it 我只是一个数字,所以你不能抵消它

Cells(i.Offset(1, 0), 9).Value = myLongVariable
Cells(i.Offset(1, 0), 9).Text = myStringVariable

should be 应该

Cells(i + 1, 9).Value = myLongVariable
Cells(i + 1, 9).Value = myStringVariable

Just to be different from the other answer you also can use this: 只是为了与其他答案不同,您还可以使用以下方法:

i.Offset(1).EntireRow.Cells(9).Value = myLongVariable
i.Offset(1).EntireRow.Cells(9).Value = myStringVariable

Or to ensure to use the correct column: 或确保使用正确的列:

i.Offset(1).EntireRow.Columns("I").Value = myWhateverVariable

still, like said in my comment Cells(i.Row + 1, 9) also is possible or Cells(i.Row + 1, "I") ;) 仍然,如我的评论中所述, Cells(i.Row + 1, 9)还是可能的,或者Cells(i.Row + 1, "I") ;)

This assumes that i is a range because you had that in another question: i.Offset(1,0) returns a Range object, not a number. 假设i是一个范围,因为您在另一个问题中遇到了这个问题: i.Offset(1,0)返回一个Range对象,而不是数字。 The way you use it in your code looks like you are trying to use it as the number of a row. 在代码中使用它的方式看起来像是您尝试将其用作行数。 To get the Row of a range, just use the .Row property: 要获取范围的行,只需使用.Row属性:

i.Offset(1,0).Row
i.Row + 1 'same thing 

Then 然后

Cells(i.Offset(1, 0), 9).Value = myLongVariable

should be 应该

Cells(i.Row+1, 9).Value = myLongVariable

Next thing: 下一件事:

 Cells(i.Offset(1, 0), 9).Text = myStringVariable

This is the same problem with Offset but also the .Text property. 这是与Offset相同的问题,而且还有.Text属性。 The .Text property is read only and returns the string that is displayed on screen. .Text属性是只读的,它返回屏幕上显示的字符串。 It differs from the value for example if the cell is too small, it becomes ##### . 它与值不同,例如,如果单元格太小,它将变为##### Use the .Value property for strings as well as numbers. .Value属性用于字符串和数字。


Now for the last thing: 现在最后一件事:

Somecell.End(xlToRight)

End(xlToRight) looks for the end of the region, which means that if the cell is empty, it looks for the end of the empty region, which is the next cell that contains something or the end of the sheet. End(xlToRight)查找区域的末端,这意味着如果单元格为空,则查找空白区域的末端,该区域是包含某些内容的下一个单元格或工作表的末端。 If the cell is not empty, it looks for the first empty cell. 如果单元格不为空,则查找第一个空单元格。 What you want is to search from the right for the first non-empty cell: 您想要从右边搜索第一个非空单元格:

Cells(Somecell.Row,Columns.Count).End(xlToLeft) 'this is the last nonempty cell in the Row of Somecell

you have to move one column to the right to get the first empty cell, for example using Offset . 您必须向右移动一列以获取第一个空白单元格,例如使用Offset

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

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