how to insert numbers before every word in excel

前端 未结 2 1861
梦毁少年i
梦毁少年i 2021-01-16 06:02

http://imgur.com/ekrjv0D

Hi please check above image. Is there any way to insert number before each word in worksheet.
I have a table like this:

         


        
相关标签:
2条回答
  • 2021-01-16 06:30

    Try this short macro:

    Sub marine()
       Dim n As Long
       Dim r As Range
       n = 1
    
       For Each r In ActiveSheet.UsedRange
          If r.Value <> "" Then
             r.Value = n & "/" & r.Value
             n = n + 1
          End If
       Next r
    End Sub
    
    0 讨论(0)
  • 2021-01-16 06:36

    another additional variant to already posted:

    Sub test()
        Dim cl As Range, i&
        Set cl = Cells.Find("*")
        For i = 1 To WorksheetFunction.CountA(Cells)
            If Not cl Is Nothing Then
                cl.Value2 = i & "/" & cl.Value2
                Set cl = Cells.FindNext(cl)
            Else
                Exit For
            End If
        Next i
    End Sub
    

    updated against additional question:

    Is there anyway to remove back the numbers and slash at the beginning of each word

    you can use this:

    Sub test2()
       Dim n&, r As Range:  n = 1
       For Each r In ActiveSheet.UsedRange
          If r.Value2 Like "*/*" Then
             r.Value2 = Split(r.Value2, "/")(1)
             n = n + 1
          End If
       Next r
    End Sub
    
    0 讨论(0)
提交回复
热议问题