简体   繁体   中英

Excel VBA, If cell is not numeric and empty in a column, selected cell copy and paste to another column

A   B         C   
a   rm        rm
b   
c   
d   4000
e   5000
f   r1        r1
g   c1        c1
h   103
i   1.8

For example, in B Column, if cell value are not number, copy the value to C Column

how to make that code?

please, inquiry for that!

The formula to do it would be: =IF(ISNUMBER(CELL), " ", CELL)

For your case put this snippet right in C2 and copy it down: =IF(ISNUMBER(B2), " ", B2)

At first you need to find the last non blank row number of column A. Then run a loop and check if the value of column A is numeric or not.

Public Sub M()
Dim sh As Worksheet
Set sh = Worksheets("Worksheet name here")
Dim lastrow As Long, i As Long
lastrow = sh.Cells(1048576, 2).End(xlUp).Row
For i = 1 To lastrow
If IsNumeric(.Cells(i, 2).Value) = False Then
.Cells(i, 3).Value = .Cells(i, 2).Value
End If
Next i

End Sub

You could try:

Formula:

=IF(AND($B1<>"",ISNUMBER($B1)=FALSE),$B1,"")

VBA:

Sub test()
    
    Dim LastRow As Long, i As Long
    
    With ThisWorkbook.Worksheets("Sheet1")
        
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        For i = 1 To LastRow
            If .Range("B" & i).Value <> "" And Not IsNumeric(.Range("B" & i).Value) Then
                .Range("C" & i).Value = .Range("B" & i).Value
            End If
        Next i
    End With
    
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM