开发者

Excel VBA to insert line until text is matched

开发者 https://www.devze.com 2023-04-08 17:49 出处:网络
I\'ve got an Excel file that should have the text \'UNK\' as the first 3 letters开发者_如何学Python of cell A10 (I don\'t care what comes after the UNK). If it does NOT match this text, I need to inse

I've got an Excel file that should have the text 'UNK' as the first 3 letters开发者_如何学Python of cell A10 (I don't care what comes after the UNK). If it does NOT match this text, I need to insert a blank line at the top of the Excel file. For some reason my code is not evaluating correctly and I'm not sure why. I'm using:

If Left(A10, 3) <> "UNK" Then
    Rows("1:1").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If

As you might be able to tell, I'm a novice with this type of code. Any help is much appreciated.


What about this? It loops until either A10 is blank or it finds UNK.

  Do While Range("A10") <> ""
    If Left(Range("A10"), 3) <> "UNK" Then
      Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Else
      Exit Do
    End If
  Loop


I would change this to:

If Left(Range("A10"), 3) <> "UIC" Then
    Range("A1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End If
0

精彩评论

暂无评论...
验证码 换一张
取 消