[英]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.