Excel:我想用B列的内容填充A列,只有当B列值以F开头时

时间:2016-08-22 14:06:06

标签: excel excel-vba excel-formula excel-2010 vba

我有两列不同的值; A和B,如果列B的值以字母F开头,我想要该值(以F开头的那个)来替换A列中的当前值。这是我到目前为止的VBA代码,不知道我是不是朝着正确的方向前进。

Private Sub UpdateColumnA()
Dim x As Long
For x = A To Z
    If InStr(F, Sheet1.Range("$B$" & x), "thisvalue) > 0 Then
        Sheet1.Range("$A$" & x) = Sheet1.Range("$A$" & x) & "sometext"
    End If
Next
End Sub

另外,我不确定哪些内容应该放在代码片段“thisvalue”和“sometext”

2 个答案:

答案 0 :(得分:0)

Private Sub UpdateColumnA()
Dim x As Long
lastRow = Sheet1.Cells(Sheet1.Rows.Count, 2).End(xlUp).Row
For x = 1 to lastRow
    If Left(Sheet1.Range("B" & x), 1)= "F" Then Sheet1.Range("A" & x) = Sheet1.Range("B" & x)
Next
End Sub

答案 1 :(得分:0)

Private Sub UpdateColumnA()
Dim x As Long
With Sheet1
    For x = 1 To .Cells(.Rows.Count , 2).End(xlUp).Row
        If UCase(Left(.Cells(x, 2), 1)) = "F" Then
            .Range("$A$" & x) = .Range("$B$" & x)
        End If
    Next x
End With
End Sub