[英]VBA: How to copy part of the string from one cell and replace part of another cell with it?
[英]Replace part of text in a cell with text from another cell using VBA
我有一个工作表,其中A列包含名和姓,中间用空格分隔。 同一工作表中的L列具有“首选名字”,但仅适用于某些行。 我想从第2行滚动到UsedRange,如果L列中存在一个值,则采用该值并将A列中的First Name替换为该值。
示例:A列:Thomas Edison L列:Tom
我想将A栏更改为Tom Edison。
任何帮助将不胜感激。
Sub names()
Dim nick As String
Dim last As String
Dim full As String
Dim midd As Integer
Dim lastRow As Long
lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If Cells(i, 12).Value <> "" Then
nick = Cells(i, 12).Value
midd = InStr(1, Cells(i, 1), " ")
last = Mid(Cells(i, 1), midd + 1, 99)
full = nick & " " & last
Cells(i, 1).Value = full
End If
Next
End Sub
Option Explicit
Public Sub updateNames()
Dim ws As Worksheet, n As Range, p As String, ur As Range
Set ws = Sheet1
Set ur = ws.Range("A2:A" & ws.Cells(ws.UsedRange.Rows.Count + 1, 1).End(xlUp).Row)
For Each n In ur
p = n.Offset(0, 11).Value2
If Len(p) > 0 Then n.Value2 = p & " " & Split(n.Value2)(1)
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.