excel - VBA 运行宏然后遍历其他工作表
问题描述
我正在尝试(但失败)让一些代码在每个工作表上运行,除了一个特定的工作表。我希望代码只剪切单元格 n2:s2 中的数据并将其粘贴到 t1:y1 中,然后对在 n3:s3、n4:s4、n5:s5 列中具有数据的任何其他行重复。
一旦没有数据(我相信第 6 行),它应该移动到下一张表(“报告”表除外)。我调试时面临的问题是它按预期移动数据,然后在同一张纸上重新开始,因此用空单元格覆盖数据。
Sub MovethroughWB()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets 'This statement starts the loop
If ws.Name <> "Report" Then 'Perform the Excel action you wish (turn cell yellow below)
Range("N2:S2").Select
Selection.Cut Destination:=Range("T1:Y1")
Range("T1:Y1").Select
Range("N3:S3").Select
Selection.Cut Destination:=Range("Z1:AE1")
End If
Next ws
End Sub
我确定它是基本的,但找不到什么!
解决方案
尝试:
Sub MovethroughWB()
Dim ws As Worksheet
Dim i As Long, Lastrow As Long, Lastcolumn As Long
For Each ws In ThisWorkbook.Worksheets 'This statement starts the loop
If ws.Name <> "Report" Then 'Perform the Excel action you wish (turn cell yellow below)
With ws
Lastrow = .Cells(.Rows.Count, "N").End(xlUp).Row
For i = 2 To Lastrow
If .Range("N" & i).Value <> "" And .Range("O" & i).Value <> "" And .Range("P" & i).Value <> "" _
And .Range("Q" & i).Value <> "" And .Range("R" & i).Value <> "" And .Range("S" & i).Value <> "" Then
If .Range("T1").Value = "" Then
.Range("N" & i & ":S" & i).Cut .Range("T1:Y1")
Else
Lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
.Range("N" & i & ":S" & i).Cut .Range(.Cells(1, Lastcolumn), .Cells(1, Lastcolumn + 5))
End If
End If
Next i
.Rows("2:" & Lastrow).EntireRow.Delete
End With
End If
Next ws
End Sub
推荐阅读
- java - 在 Java 中格式化对象数组 toString() 输出
- flask - 使用 gunicorn 在 Heroku 上部署烧瓶应用程序
- javascript - 从每个迭代器块中的函数返回语句
- wordpress - 如何更改 Wordpress Gutenberg 可视化编辑器默认字体系列
- powershell - 有条件地替换 PowerShell 对象中的值
- c - 谁能帮我理解这段代码如何打印三角形?
- javascript - 如何确定 Javascript 中“真实”键盘的存在?
- android - 设置 APN 后,我的 Android 应用无法连接到 localhost
- typescript - 具有复制属性的类方法的元数据在更改源类的元数据时发生更改 - Typescript
- python-3.x - 使用 pandas.json_normalize 时保护嵌套对象不被展平