excel - vba excel:如何将字符从一个单元格复制到另一个单元格
问题描述
我是 VBA 世界的新手,我正在根据您的建议学习。
我已经寻找了几种解决方案,尝试了它们,但它们并不适合我的问题。
这是链接
我需要的是检查字符Input
并匹配的可能性Match list key
。匹配字符串后,复制输出中的字符 I。
如您所见,第一行很容易匹配,但在A8
单元格中(例如)有一个包含 14 个字符的字符串。在这种情况下,我需要这样,当有一个以 CMXXAB 开头的字符串时,匹配是 WT(总是!)。当我们有 A12 时也会发生同样的事情:它以 ETRxxx 开头,并且在匹配之后将在像 JAZZ 这样的输出中开始。
解决方案
我认为这会帮助你:
Option Explicit
Sub test()
Dim LastrowA As Long, i As Long, AppearA As Long, AppearB As Long
Dim strA As String, strB As String
With ThisWorkbook.Worksheets("Sheet1")
LastrowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastrowA
strA = .Range("A" & i).Value
strB = .Range("C" & i).Value
'Check if strA appears in strB
AppearA = InStr(1, strB, strA)
If AppearA > 0 Then
.Range("B" & i).Value = strA
Exit For
End If
'Check if strB appears in strA
AppearB = InStr(1, strA, strB)
If AppearB > 0 Then
.Range("B" & i).Value = strB
Exit For
End If
Next i
End With
End Sub
非常感谢您的帮助。几天后,我在我的问题中找到了问题的解决方案。事实上,这是我的解决方案,我希望能够帮助任何需要这样的人。
Sub AssociazioneRotabiliPerPivot()
Dim LastrowA, LastrowC As Long, i, j As Long, AppearA As Long, AppearB As Long
Dim strA As String, strB As String
With ThisWorkbook.Worksheets("sheet1")
LastrowA = .Cells(.Rows.count, "A").End(xlUp).Row
LastrowC = .Cells(.Rows.count, "C").End(xlUp).Row
For j = 1 To LastrowC
For i = 1 To LastrowA
strA = .Range("A" & i).Value
strB = .Range("C" & j).Value
AppearC = InStr(1, strA, strB)
If AppearB > 0 Then
.Range("B" & i).Value = strB
End If
If (InStr(1, strA, "CM") Or InStr(1, strA, "C4551R")) > 0 Then
.Range("B" & i).Value = "WT"
ElseIf InStr(1, strA, "ETR425") > 0 Then
.Range("B" & i).Value = "JAZZ"
End If
Next i
Next j
End With
结束子
推荐阅读
- java - Mapstruct 和 Lombok 升级导致不同的初始化行为
- spring - Spring Security - 从安全性中排除 POST 请求
- java - 如何使用hibernate将三个表映射在一起
- python - PostgreSQL - 为已存在的列创建生成的列
- csv - 使用 logstash 从 CSV 向现有索引添加新字段
- javascript - 如何添加另一个 div id 到 setInterval 加载?
- javascript - 使用复选框禁用动态输入
- javascript - A-Frame:屏幕截图颜色管理正在中断
- google-cloud-platform - 在 Google Cloud Platform 上的项目之间复制 IAM 配置
- reactjs - React : 当用户停止输入时触发函数