XL VBA宏用于根据标准向右插入空白单元格,即B列包含$

时间:2016-09-29 06:31:23

标签: excel-vba vba excel

我需要一个宏来插入2个正确移动现有数据的单元格。听起来很容易,但一整天都在这里。

问题是B列包含文字和货币。如果单元格包含货币,比如$ 4661.52,我需要插入2个单元格,正确移动现有货币数据。

长度因货币单元格而异。当天晚些时候 并且困惑。

试过这个和很多变化:

Public Sub insertcells()
Const CLIENT As String = "B"
Dim Lastrow As Long
Dim i As Long
     Application.ScreenUpdating = False
      With ActiveSheet
         Lastrow = .Cells(.Rows.Count, CLIENT).End(xlUp).Row
         For i = Lastrow To 1 Step -1
             If Cells(i, CLIENT).Value2 Like "$*" Then
                 .Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
             End If
         Next i
     End With
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:1)

查看此代码。

Public Sub insertcells()
Const CLIENT As String = "B"
Dim Lastrow As Long
Dim i As Long
     Application.ScreenUpdating = False
      With ActiveSheet
         Lastrow = .Cells(.Rows.Count, CLIENT).End(xlUp).Row
         For i = Lastrow To 1 Step -1
             If Cells(i, CLIENT).Value2 Like "$*" Or IsNumeric(Cells(i, CLIENT).Value) Then
                 .Cells(i, CLIENT).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
                 .Cells(i, CLIENT).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
             End If
         Next i
     End With
Application.ScreenUpdating = True
End Sub