[英]Run-time error 424: 'Object Required' error in VBA
I am trying to stack data from several sheets in one mastershhet (the sheet where I am running this Macros). 我试图在一个mastershhet(我正在运行此宏的表单)中堆叠来自多个工作表的数据。 So its essentially the same code replicated a few times.
所以它基本上相同的代码复制了几次。 I also want to highlight duplicates in the first column and thus last bit is about that.
我还想强调第一列中的重复项,因此最后一点就是这个。 Can't figure out why do I keep getting 'Object Required' Error.
无法弄清楚为什么我一直得到'对象必需'错误。 Any help will be greatly appreciated.
任何帮助将不胜感激。
Sub Stackdata()
Dim emptyrow As Long, lastrow As Long, lastcolumn As Long
Workbooks.Open ”Declined.csv”
Worksheets(1).Select
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
ActiveWorkbook.Close
Worksheets(1).Select
emptyrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(emptyrow, 1).Select
ActiveSheet.Paste
Workbooks.Open ”Offersbutwithdrawn.csv”
Worksheets(1).Select
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
ActiveWorkbook.Close
Worksheets(1).Select
emptyrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Cells(emptyrow, 1).Select
ActiveSheet.Paste
Dim iWarnColor As Integer
Dim rng As Range
Dim rngCell As Variant
Set rng = Range("A1:A200") ' area to check '
iWarnColor = xlThemeColorAccent2
For Each rngCell In rng.Cells
vVal = rngCell.Text
If (WorksheetFunction.CountIf(rng, vVal) = 1) Then
rngCell.Interior.Pattern = xlNone
Else
rngCell.Interior.ColorIndex = iWarnColor
End If
Next rngCell
End Sub
I did shorten your code and deleted anything related to .Select
to avoid errors. 我确实缩短了您的代码并删除了与
.Select
相关的任何内容以避免错误。 Thought I can't see where the error comes from, Let me know if this code works for you. 以为我无法看到错误的来源,请告诉我此代码是否适合您。 If it doesn't come back after you debug the error line and tell us:
如果在调试错误行后它没有回来并告诉我们:
Option Explicit
Sub Stackdata()
Dim emptyrow As Long, lastrow As Long, lastcolumn As Long, i As Long
Dim wb As Workbook, wbSource As Workbook, arrWorkbooks, ws As Worksheet, wsSource As Worksheet
arrWorkbooks = Array("Declined.csv", "Offersbutwithdrawn.csv") 'here you can add as many workbooks as you need
'reference and declare workbooks and worksheets to avoid .Select
Set wb = ThisWorkbook
Set ws = wb.Sheets(1)
'A loop through all your workbooks on the variable arrWorkbooks
For i = 0 To UBound(workbooks)
Set wbSource = workbooks.Open(arrWorkbooks(i), ReadOnly:=True) 'thought you need the full path before the file name
Set wsSource = wbSource.Sheets(1)
emptyrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
With wsSource
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Range(.Cells(2, 1), .Cells(lastrow, lastcolumn)).Copy ws.Cells(emptyrow, 1)
End With
Next i
Dim iWarnColor As Integer
Dim rng As Range
Dim rngCell As Variant
With ws
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rng = .Range("A1:A" & lastrow) ' area to check ' now it gets to the last row always
iWarnColor = xlThemeColorAccent2
For Each rngCell In rng.Cells
vVal = rngCell.Text
If (WorksheetFunction.CountIf(rng, vVal) = 1) Then
rngCell.Interior.Pattern = xlNone
Else
rngCell.Interior.ColorIndex = iWarnColor
End If
Next rngCell
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.