简体   繁体   中英

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

Column K can contain the string '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'.

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.

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") ). The code as is will continousely re-write over your value in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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