简体   繁体   中英

Copy specific cell contents into new sheet with VBA

I am in need of some help. I am trying to copy contents of cells in one sheet into another sheet in the same project. Below I have VBA code that helps me copy cells to another sheet. My only problem is that each cell contains more content than I need them to.

For instance I have:

"Packets: Sent = 10, Received = 10, Lost = 0 (0% loss)"

All I need from the above is "0" (or whatever percentage it happens to be. )How can I automate this by adding functionality to the below code.

Private Sub CommandButton1_Click()

Dim LR As Long, i As Long, cls
cls = Array("A13", "A14", "A15", "A16")
With Sheets("Sheet2")
    LR = WorksheetFunction.Max(6, .Range("A" & Rows.Count).End(xlUp).Row + 1)
    For i = LBound(cls) To UBound(cls)
        .Cells(LR, i + 1).Value = Me.Range(cls(i)).Value
    Next i
End With
End Sub

Thank you!

Assuming the text is Me.Range(cls(i)).Value change the contents of the for loop to

percentStr = Me.Range(cls(i)).Value
.Cells(LR, i + 1).Value = Val(Mid$(percentStr, InStr(percentStr, "(") + 1))

One way to extract only the percentage value:

Sub qwerty()
    Dim s As String
    s = "Packets: Sent = 10, Received = 10, Lost = 0 (0% loss)"
    ary = Split(s, "(")
    bry = Split(ary(1), "%")

    MsgBox bry(0)
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