[英]Replace part of text in a cell with text from another cell using VBA
I have a worksheet with Column A containing First and Last Name separated by a space. 我有一个工作表,其中A列包含名和姓,中间用空格分隔。 Column L in the same sheet has Preferred First Name but for only some rows.
同一工作表中的L列具有“首选名字”,但仅适用于某些行。 I would like to scroll from row 2 to UsedRange and if a value exists in Column L, then take that value and replace First Name in Column A with that value.
我想从第2行滚动到UsedRange,如果L列中存在一个值,则采用该值并将A列中的First Name替换为该值。
Example: Column A: Thomas Edison Column L: Tom 示例:A列:Thomas Edison L列:Tom
I would like to change Column A to Tom Edison. 我想将A栏更改为Tom Edison。
Any help would be highly appreciated. 任何帮助将不胜感激。
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.