我在excel中有一个宏,它可以工作,但并不像我想要的那样完美,不能找到解决方案,需要你的想法。
下面是它所做的工作:从设置复制粘贴值到计算中的第一个非空单元格sheet.It to ok
以下是我的代码:
Sub support()
Sheets("Settings").Select
Range("S411:S421").Select
Selection..Copy
Sheets("Calculation").Select
Range("C4").Select
Range("C4").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
但是,我只想将非空单元格和值复制到这10行之间的计算页中,这些单元格和值不是0。(所以我应该跳过复制0和空单元格)有什么简单的技巧可以指导我吗?
发布于 2017-01-12 02:16:07
你可以用AutoFilter()
Sub support()
With Sheets("Settings").Range("S410:S421") '<--| reference wanted range and the cell above it as the "header"
If IsEmpty(.Cells(1, 1)) Then .Cells(1, 1).Value = "dummyheader" '<--| add a "dummy" header value if it's empty
.AutoFilter Field:=1, Criteria1:="<>", Operator:=xlAnd, Criteria2:="<>0" '<--| filter referenced range with "not empty" and "not zero" values
If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then
.Resize(.Rows.Count - 1, 1).Offset(1).SpecialCells(xlCellTypeVisible).Copy
Sheets("Calculation").Range("C4").End(xlDown).Offset(1, 0).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End If
If .Cells(1, 1).Value = "dummyheader" Then .Cells(1, 1).ClearContents '<--| remove "dummy" header, if there
.Parent.AutoFilterMode = False
End With
End Sub
发布于 2017-01-12 01:34:59
您想每次都从Range("S411:S421")
复制到Range("C4")..->
吗?或者这些范围可以改变?
试着:
Public Sub CommandButton1_Click()
j = 4
For i = 411 To 421
If ThisWorkbook.Sheets("Settings").Cells(i, 19) <> 0 And ThisWorkbook.Sheets("Settings").Cells(i, 19) <> "" Then
ThisWorkbook.Sheets("Settings").Cells(i, 19).Copy
ThisWorkbook.Sheets("Calculation").Cells(j, 3).PasteSpecial (xlPasteValues)
j = j + 1
End If
Next i
End Sub
https://stackoverflow.com/questions/41609026
复制相似问题