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

标签 vba excel

K 列可以包含字符串“项目成本”。当 K 列包含“项目成本”时,我想偏移到 U 列并从该单元格中复制与字符串“项目成本”相同的 ROW 中的值。

我可以让代码读取并找到 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

最佳答案

似乎您希望使目标范围动态化( Range("D10") )。原样的代码将继续重写 D10 中的值.您是否希望该值与目标范围位于同一行的 Col D 中?如果是这样,交换

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

为了
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

关于vba - Excel VBA - 根据另一列的数据使用偏移量获取单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51141029/

相关文章:

excel - 将Excel公式转换为VBA函数

c# - EPPlus 日期单元格数据类型不工作

excel - VBA - 从头开始​​查找字符串

excel - 如何在 Excel 中使用 VBA 遍历所有列

vba 代码在 f8 中运行,但不在 f5 中

c# - 打印时将行设置为在顶部重复 - 打开 XML 和 Excel

excel - 如何在Excel VBA中获取选定的形状?

vba - 运行时错误 7 Out of Memory 使用 vba 集合

excel - 预期编译错误数组 - Ubound - VBA

vb.net - .Net 中的 Excel 互操作 - 捕获 VBA 错误?