簡體   English   中英

在范圍內找到特定的字符串並將其復制到VBA中的另一個字段

[英]Find a specific String in a range and copy it to another field in VBA

所以我想在范圍內找到一個特定的字符串,獲取其地址並將其值復制到一個新的單元格中,這取決於舊地址

到目前為止,我一直在嘗試以下方法:

Dim c As Range

For Each c In Range("F1:F1500")
    If InStr(1, c.Text, "Overall Result") Then
        Range(c).Select
        Selection.Cut
        Range(newAddress).Select
        ActiveSheet.Paste
    End If
Next c

但是我不確定如何獲取正確的單元地址。 新的單元格應該是舊的address.row和舊的address.column + 1

您可以使用c.Offset(ColumnOffset:=1)將一列從范圍c向右移出。

Dim c As Range

For Each c In Range("F1:F1500")
    If InStr(1, c.Text, "Overall Result") Then
        c.Cut Destination:=c.Offset(ColumnOffset:=1)
    End If
Next c

還可以看看如何避免在Excel VBA中使用“選擇”,這是一種不好的做法,會使您的速度變慢並且不太可靠。

Dim c As Range

For Each c In Range("F1:F1500")
    If InStr(1, c.Text, "Overall Result") Then
        Range(c.address).offset(0,1).value = c.text
    End If
Next c

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM