gpt4 book ai didi

excel - 在vba中将单元格的值剪切并粘贴到另一个单元格

转载 作者:行者123 更新时间:2023-12-03 00:07:34 26 4
gpt4 key购买 nike

如果 C 列等于“RRR”,我需要将 F 列的值转移或移动到 D 列的最后一个单元格。我无法突出显示或选择从“RRR”位置到最后一个值为“SSS”的单元格的范围。相反,它从 C4:C9 中选择范围,这是错误的。

    Dim ws As Worksheet, lRow As Long

Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

Dim lCol As Long

With ws
For x = 1 To lRow
If .Cells(x, 3).Value = "RRR" Then
lCol = Cells(x, Columns.Count).End(xlToLeft).Column
Range("C" & x & ":C" & lCol).Select
End If
Next x
End With

示例: enter image description here

预期:

enter image description here

谁能告诉我代码中的问题。

最佳答案

您已经很接近了,只有应该修改的选择范围。

这样你就可以建立你的范围:

Range(A1:D1) -> Range(Cells(A1), Cells(D1)) -> 

Range(Cells(row number, column number), Cells(row number, column number)) ->

Range(Cells(1, 1), Cells(1, 4))

这应该可以解决问题:

Dim ws As Worksheet, lRow As Long
Dim x As Long

Set ws = ThisWorkbook.ActiveSheet
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row

Dim lCol As Long

With ws
For x = 1 To lRow
If .Cells(x, 3).Value = "RRR" Then
lCol = Cells(x, Columns.Count).End(xlToLeft).Column 'Find the last column number
Range(Cells(x, 6), Cells(x, lCol)).Cut Cells(x, 4) 'Cut from row x and Column F (Column F = 6) to row x and column "lCol". Then paste the range into row x and column 4.
End If
Next x
End With

End Sub

关于excel - 在vba中将单元格的值剪切并粘贴到另一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52951539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com