Vba 将非空白单元格值从一张图纸复制并粘贴到另一张图纸
我正在尝试使用下面的代码将一张纸上的非空白单元格复制到另一张纸上Vba 将非空白单元格值从一张图纸复制并粘贴到另一张图纸,vba,excel,Vba,Excel,我正在尝试使用下面的代码将一张纸上的非空白单元格复制到另一张纸上 Public Sub Copypastenonblanks() Dim mySheet As Worksheet, myOtherSheet As Worksheet, myBook As Workbook Set myBook = Excel.ActiveWorkbook Set mySheet = myBook.Sheets("Sheet1") Set myOtherSheet = myBook.Sheets("Sheet2
Public Sub Copypastenonblanks()
Dim mySheet As Worksheet, myOtherSheet As Worksheet, myBook As Workbook
Set myBook = Excel.ActiveWorkbook
Set mySheet = myBook.Sheets("Sheet1")
Set myOtherSheet = myBook.Sheets("Sheet2")
mySheet.Range("BK1:BK230").SpecialCells(xlCellTypeConstants).Copy myOtherSheet.Range
("Q2")
End Sub
在工作表列中,BK包含空白单元格,我正在尝试复制非空白单元格和粘贴到其他工作表,它只粘贴标题,而不是所有数据 这应该可以满足您的需要
Public Sub Copypastenonblanks()
Dim mySheet As Worksheet
Dim myOtherSheet As Worksheet
Dim myBook As Workbook
Dim irow As Integer
Dim c As Variant
Set myBook = Excel.ThisWorkbook
Set mySheet = myBook.Sheets("Sheet1")
Set myOtherSheet = myBook.Sheets("Sheet2")
irow = 0
Do Until irow = -1
For Each c In mySheet.Range("BK1:BK230")
irow = irow + 1
If c.Value <> "" Then
myOtherSheet.Cells(irow, 17).Value = c.Value
Else
If irow = 230 Then
irow = -1
End If
irow = irow - 1
GoTo EndofNext
End If
If irow = 230 Then
irow = -1
End If
EndofNext:
Next c
Loop
End Sub
公共子副本粘贴非空白()
将我的工作表设置为工作表
将工作表设置为工作表
将myBook设置为工作簿
Dim irow作为整数
dimc作为变体
设置myBook=Excel.ThisWorkbook
设置mySheet=myBook.Sheets(“Sheet1”)
设置myOtherSheet=myBook.Sheets(“Sheet2”)
irow=0
直到irow=-1为止
对于mySheet.Range中的每个c(“BK1:BK230”)
irow=irow+1
如果c.值为“”,则
myOtherSheet.Cells(irow,17).Value=c.值
其他的
如果irow=230,则
irow=-1
如果结束
irow=irow-1
转到EndofNext
如果结束
如果irow=230,则
irow=-1
如果结束
EndofNext:
下一个c
环
端接头
您的函数在Excel 2013中确实适用于我。但是,当内置函数不能产生预期的结果时,您可以始终转身构建自己的定制函数(大多数情况下,这一点都不难)
我是这样做的:
Sub CopyPasteNonBlanks()
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
Set ws1 = wb.Sheets(1)
Set ws2 = wb.Sheets(2)
col1 = 63 'Column BK of 1st sheet
col2 = 17 'Column Q of 2nd sheet
lastRow1 = ws1.Cells(ws1.Rows.Count, col1).End(xlUp).Row 'Find last row of 1st sheet
currentRow2 = 2 'Start below headers of 2nd sheet
For iRow1 = 1 To lastRow1
If ws1.Cells(iRow1, col1) <> "" Then
ws2.Cells(currentRow2, col2) = ws1.Cells(iRow1, col1)
currentRow2 = currentRow2 + 1
End If
Next iRow1
Application.ScreenUpdating = True
End Sub
子复制粘贴非空白()
Application.ScreenUpdating=False
设置wb=ActiveWorkbook
设置ws1=wb.Sheets(1)
设置ws2=wb.Sheets(2)
col1=63'第1页BK列
col2=17'第二页Q列
lastRow1=ws1.Cells(ws1.Rows.Count,col1).End(xlUp).Row'查找第一张工作表的最后一行
currentRow2=2'从第二页页眉下方开始
对于iRow1=1到最后一行1
如果ws1.Cells(iRow1,col1)“,则
ws2.Cells(currentRow2,col2)=ws1.Cells(iRow1,col1)
currentRow2=currentRow2+1
如果结束
下一个iRow1
Application.ScreenUpdating=True
端接头
如果您的目标是复制所有非空白单元格,而不仅仅是常数,请尝试替换您的
mySheet.Range("BK1:BK230").SpecialCells(xlCellTypeConstants).Copy _
myOtherSheet.Range("Q2")
用类似
With mySheet.Range("BK1:BK230")
Union(.SpecialCells(xlCellTypeConstants), .SpecialCells(xlCellTypeFormulas)). _
Copy myOtherSheet.Range("Q2")
End With
不知道你的意思,使用Excel 2010对我来说效果很好。