我需要vba代码,才能执行以下操作:
选择一个随机命名的工作簿,其中保存有名为“ ImportData”的特定工作表,然后将其值复制到列A1:T121中。如果“ K”列中有数据,并且“ E”列不是空白。
将值粘贴到MainWorkbook工作表1中,位于A列的第一行。
这可能吗?
谢谢。
答案 0 :(得分:0)
修改代码,然后尝试:
Option Explicit
Sub test()
Dim wb1 As Workbook, wb2 As Workbook
Dim Lastrow As Long, i As Long
'Make sure that both workbooks are open
Set wb1 = Workbooks("Book1.xlsm")
Set wb2 = Workbooks("Book2.xlsm")
'Refer to Book1 & Sheet1
With wb1.Worksheets("Sheet1")
'Get the last row of Book1, Sheet1 & column A
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Loops Book1, Sheet1 & column A from line 1 to lastrow
For i = 1 To Lastrow
'Check for blanks in colums E & K
If .Range("E" & i).Value <> "" And .Range("K" & i).Value <> "" Then
'If there are no blanks copy from wb1, Sheet1 & Range "A1:D1" to wb2, Sheet1 & Range "A1:D1"
.Worksheets("Sheet1").Range("A1:D1").Copy wb2.Worksheets("Sheet1").Range("A1:D1")
End If
Next i
End With
End Sub