简体   繁体   English

vba excel:如何将字符从一个单元格复制到另一个单元格

[英]vba excel: how to copy characters from a cell to another

I'm new in VBA world and I'm learning by your suggestions.我是 VBA 世界的新手,我正在学习您的建议。

I've looked for several solutions, tried them but they were't ideal for my problem.我已经寻找了几种解决方案,尝试了它们,但它们并不适合我的问题。

Here are the links这是链接

Find a string within a cell using VBA 使用 VBA 在单元格中查找字符串

How to Count the Number of a Specific Character in a Cell with Excel VBA 如何使用 Excel VBA 计算单元格中特定字符的数量

Which command in VBA can count the number of characters in a string variable? VBA 中的哪个命令可以计算字符串变量中的字符数?

What I need is the possibility to check the characters in Input and match with Match list key .我需要的是检查Input的字符并与Match list key匹配的可能性。 After matched the string, copy the characters I in output.匹配字符串后,复制输出中的字符 I。

这里和工作表中的例子

As you can see, first rows are simple to match but in A8 cell (for example) there is a string with fourteen characters.如您所见,第一行很容易匹配,但在A8单元格中(例如)有一个包含 14 个字符的字符串。 In this case I need that, when there is a string that begin with CMXXAB, the match is with WT (ALWAYS!).在这种情况下,我需要的是,当有一个以 CMXXAB 开头的字符串时,匹配的是 WT(总是!)。 The same thing happen when we have A12: it begins with ETRxxx and, after match will begin in output like JAZZ.当我们有 A12 时也会发生同样的事情:它以 ETRxxx 开头,匹配后将在输出中开始,如 JAZZ。

I think this will help you:我认为这会帮助你:

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

Thank you so much for help.非常感谢您的帮助。 after few days i've found solution for problem in my question.几天后,我在我的问题中找到了问题的解决方案。 infact, this is my solution and I hope to be good for helping anyone need something like this.事实上,这是我的解决方案,我希望能够帮助任何需要这样的人。

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

End Sub结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Excel VBA中将单元格从一行复制到另一行 - How to copy cell from one row to another in excel vba Excel VBA如何将单元格内容从一本书复制到另一本书 - Excel VBA how to copy cell content from one book to another VBA - 将前三个字符从字符串复制到另一个单元格 - VBA - Copy first three characters from string into another cell VBA - 将字符串中的字符拆分,复制并粘贴到另一个单元格中(具有条件) - VBA - Split, copy and paste characters from string into another cell (with conditions) Excel VBA:将单元格值从一个工作簿复制到另一个? - excel vba: copy cell value from one workbook to another? Excel VBA:从另一个工作簿循环中复制和粘贴特定单元格 - Excel VBA: Copy and Paste Specific Cell from Another Workbook Loop Excel 使用 VBA 将单元格数据从一张纸复制到另一张纸 - Excel Copy Cell Data from one sheet to another using VBA VBA Excel-复制单元格值并将值粘贴在另一个单元格的字符之间 - VBA Excel - copy cell values and paste value in between characters of another cell VBA | 如何在Excel中将值从单元格复制到单元格 - VBA | How to Copy value from Cell to Cell in Excel Excel VBA如何使用超链接在工作表之间将信息从一个单元格复制到另一个单元格 - Excel VBA how to copy information from one cell to another between work sheets using a hyperlink
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM