简体   繁体   English

Excel VBA,剪切粘贴添加和编辑文本

[英]Excel VBA, cut paste add and edit text

I have the Excel data as seen in picture one. 我具有如图一所示的Excel数据。 原始数据

I try to find the text Statetext01 and make 24 copies of it with the right index from 1-24 and I will also change the text in column A from State_text_1 to State_text_24 . 我尝试查找文本Statetext01并使用正确的索引(从1-24)对其进行24个拷贝,并且还将A列中的文本从State_text_1State_text_24

需要改变的地方

After that, I need to find the next one in the Excel sheet and do the same. 之后,我需要在Excel工作表中找到下一个并执行相同的操作。 The Excel sheet is in over 20000 rows. Excel工作表中的行超过20000。

Is that possible? 那可能吗?

Something like this works for me and should work for you assuming your data is on 'Sheet1': 这样的事情对我有用,并且假设您的数据在“ Sheet1”上,它也应该对您有用:

Option Explicit


Sub copyMultiple()
 Dim sht As Worksheet
 Dim idx As Integer, lastRow As Integer, r As Integer, r2 As Integer
 Dim val As String
 Set sht = Worksheets("Sheet1")

 With sht
   lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

   For r = lastRow To 1 Step -1
     val = .Cells(r, "D").Value
     idx = InStr(1, val, "Statetext01", vbTextCompare)
     If idx <> 0 Then
       Rows(r + 1).Resize(23).Insert shift:=xlShiftDown
       .Rows(r).Copy Destination:=.Rows(r + 1 & ":" & r + 23)
       For r2 = 1 To 24
         With .Cells(r + r2 - 1, "D")
           .Value = Replace(.Value, "Statetext01", "Statetext" & Right("00" & r2, 2))
         End With
         With .Cells(r + r2 - 1, "A")
          .Value = Left(.Value, InStrRev(.Value, "_")) & "State_text_" & r2
         End With
       Next
     End If
   Next
 End With

End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM