简体   繁体   English

Excel VBA-根据另一列的数据,使用“偏移”获取单元格值

[英]Excel VBA - Get a Cells value by using Offset, based on the data from another column

Column K can contain the string 'Item Cost'. K列可以包含字符串“ Item Cost”。 When Column K contains 'Item Cost' I would like to offset to Column U and copy the value from that cell within the same ROW as the string 'Item Cost'. 当K列包含“项目成本”时,我想偏移到U列,并将该单元格中的值复制到与“项目成本”字符串相同的ROW中。

I can get the code to read and find the value in Column K, but am having a problem with the coping portion of the code for Column U. 我可以获取代码来读取和查找列K中的值,但是列U的代码对应部分有问题。

Dim range1 As Range
Dim Answer4 As Variant
LstRw = Cells(Rows.Count, "K").End(xlUp).Row
Set List = CreateObject("Scripting.Dictionary")


For Each range1 In wbFrom.Sheets("Sheet0").Range("K9:K" & LstRw)
  If range1.Offset(0, 0) = "Item Cost " Then
        'MsgBox "found"
         Answer4 = range1.Offset(0, 10).Value  '<---- PROBLEM
  End If
Next

'Msgbox Answer4 'returns nothing

 wbTo.Sheets("Sheet1").Range("D10").Value = Answer4  'returns nothing

Seems like you will want to make your destination range dynamic ( Range("D10") ). 似乎您将要使目标范围成为动态Range("D10") )。 The code as is will continousely re-write over your value in D10 . 照原样的代码将连续重写D10的值。 Do you maybe want the value to be in Col D on the same row as the target range? 您是否希望该值与目标范围在同一行中? If so, swap 如果是这样,请交换

wbTo.Sheets("Sheet1").Range("D10") = range1.Offset(0, 10) 

for 对于

wbTo.Sheets("Sheet1").Range("D" & range1.Row) = range1.Offset(0, 10) 

For Each range1 In wbFrom.Sheets("Sheet0").Range("K9:K" & LstRw)
  If range1 = "Item Cost " Then
        'MsgBox "found"
         wbTo.Sheets("Sheet1").Range("D10") = range1.Offset(0, 10) 
  End If
Next

暂无
暂无

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

相关问题 在excel中使用vba根据另一列更改列值 - Changing column value based on another column using vba in excel 使用另一列中的值填充空白 Excel 单元格? - Fill in blank Excel cells using value from another column? 使用VBA根据列名将数据从一个Excel工作表复制到另一个(复杂)工作表 - Copy data from one excel sheet to another (complex) using VBA based on column name Excel VBA - 根据另一列中的值填充一列中的单元格 - Excel VBA - populate cells in one column based on values in another one 从Excel VBA中的一系列单元格中获取列x的单元格 - Get column x's Cells from a Range of Cells in Excel VBA Excel VBA-根据另一个单元格的值选择单元格,然后选择“自动求和”-是否可以 - Excel VBA - Select cells based on the value of another cell and then Autosum - Is it possible VBA根据另一列中的值检查列中的空白单元格 - VBA to check for blank cells in columns based on value in another column Excel VBA:获取另一列中的数据列表 - Excel VBA: Get list of data in another column 如何使用 VBA 过滤 Excel 表、复制可见列(具有公式)并将值粘贴到另一个可见列“偏移(0,-13)”? - How to filter an Excel Table, copy visible Column (that has a formula), and paste the value into another visible column `Offset(0,-13)` using VBA? 根据其他列从excel中的列中选择特定的单元格 - Selecting specific cells from a column in excel based on another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM