2015-09-22 84 views
-1

我想將某些數據偏移到另一列。在A列中的數據存在和B.我使用的代碼是:基於特定條件將數據從一列移到另一列

Sub data_shifter() 
Dim A As Range, r As Range 
Set A = Intersect(ActiveSheet.UsedRange, Range("A:A")) 

For Each r In A 

    If Mid(r, 4, 1) = "-" Then 
     r.Copy r.Offset(0, 15) 
     r.Clear 
    End If 
Next r 

ActiveWorkbook.Save 

End Sub 

此代碼是尋找在A列中所有的字符串,其中-是第四個字符。我想要的是前三個字符應該是數字,第四個字符是-。另外,如果上述情況爲真,B列中的數據也應該被抵消。

例如,如果列A2具有333-m1233232應該偏移列B2 P2和數據應該偏移Q2

+0

嘗試把這個'檔(R ,r.offset(,1))。複製r.offset(,15)'代替'r.Copy r.Offset(0,15)' –

+0

謝謝是的,它的工作原理,但我只想要數字是數字前三個字符和 - 第四個字符像888-fdersfdf – mb1987

回答

0
Sub data_shifter() 
    Dim r As Range 
    For Each r In ActiveSheet.UsedRange.Columns(1).Cells 
     If IsNumeric(Left(r, 3)) And Mid(r, 4, 1) = "-" Then 
     r.Resize(1, 2).Copy r.Offset(0, 15) 
     r.Clear 
     End If 
    Next r 
    ActiveWorkbook.Save 
End Sub 
0

我的兩分錢,

Sub Button1_Click() 

    Dim rng As Range, c As Range  
    Set rng = Range("A:A").SpecialCells(xlCellTypeConstants, 23) 
    For Each c In rng.Cells 
     If IsNumeric(Mid(c, 1, 3)) Then 

      MsgBox " Do your thing" 'what ever you want after that 
     End If 
    Next c 

End Sub 
相關問題