我是VBA的新手,所以只有在第一个缺少值的情况下,我才需要将行从一个工作表复制到另一个工作表。单元格格式总是保持不变。现在我有这个代码:
Sub test()
Dim tohere As Worksheet
Dim fromhere As Worksheet
Dim rngTohere As Range
Dim rngfromHere As Range
Dim rngCelTohere As Range
Dim count As Integer
Dim strArray As Variant
Dim i As Integer
'Set Workbooks
Set tohere = ThisWorkbook.Worksheets("Test")
Set fromhere = ThisWorkbook.Worksheets("Test 2")
'Set Columns
Set rngTohere = tohere.Columns("C") 'this is our column of interest
Set rngfromHere = fromhere.Columns("C")
i = 1 'this is counter to foryou to know which row you need to copy
count = rngfromHere.Cells.Count - WorksheetFunction.CountBlank(rngfromHere)
strArray = rngfromHere(Cells(1, 1), Cells(count, 1)).Value
'Loop through each cell in Column C
For Each rngCelTohere In rngTohere.Cells
If IsInArray(rngCelTohere.Value, strArray) = False Then
'here need to put copy entire row into sheet
'use i row to copy into tohere Worksheet
End If
i = i + 1 '
Next rngCelTohere
End Sub
Function IsInArray(stringToBeFound As Integer, arr As Variant) As Boolean 'this functions returns true or false. for our case we need it to be false
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function
任何人都可以帮助我,并说这是个好主意,还可以帮我复制整张表格中的整行。提前谢谢!
答案 0 :(得分:0)
由于这个原因,您的代码无法编译:
0 3
1 4
2 3
3 3
4 4
Name: animals, dtype: int64
这些是整数,它们不是Set i = 1 'this is counter to foryou to know which row you need to copy
set count = rngfromHere.Cells.Count - WorksheetFunction.CountBlank(rngfromHere)
。准备好代码后,转到set
并检查编译错误。 VB编辑器将显示它们的位置。
What does the keyword Set actually do in VBA?
关于复制第i行,请参见此处:
Copy row values after another row values, copy whole row to another sheet afterwards
Copy and Paste row by index number in Excel Macro
在您的代码中,您的索引可以是Debug>Compile
。