我是很新的VBA(今天第一次使用它),我试图写一个程序。
我有一个数据集,并在C列,我希望它看起来一定字母(B),当它发现这封信,复制上面行,粘贴到,这封信是在找到行,然后改变在列C单元返回到原始信(b)中。
我已经得到了迄今为止寻找不同的教程,但我无法找到任何会复制上面一整行,并将其粘贴。
Sub TestProgram()
Dim det As String, Result As String
det = Range(C1, C214511).Value
If det = b Then
Replace(
我不知道该用什么样的替代功能,因为我无法弄清楚如何使它与上面行替换整行。也许我需要一个不同的功能?
或者,也许我只是完全失去了!
谢谢!
你犯了一个良好的开端。这应该做的伎俩(测试)。正如你刚才提到你是新的VBA,我评论的代码,以显示这是怎么回事,在每一行。
Sub testProgram()
Dim lastRow As Long ' This will be our last active row
Dim lastCol As Long ' This will be our last active column
Dim ws As Worksheet ' This will be our worksheet
Dim r As Long ' This will be used to represent the row number when looping
' Set our worksheet
Set ws = ThisWorkbook.Worksheets(1) ' Change to whichever sheet number/name you're working on
' Define the last row and last column on the working sheet
lastRow = ws.Range("A" & Rows.Count).End(xlUp).Row ' Change "A" to whichever column you like
lastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column ' Change "1" to whichever row you like
' Loop through rows starting at the top
For r = 1 To lastRow Step 1
' Check if column C contains the value 'b'
If ws.Range("C" & r).Value = "b" Then
' Grab the row above and copy it to the active row
ws.Range(Cells(r - 1, 1), Cells(r - 1, lastCol)).Copy Destination:=ws.Range(Cells(r, 1), Cells(r, lastCol))
' Reset col C of active row to value 'b'
ws.Range("C" & r).Value = "b"
End If
Next r
End Sub