从一个工作簿复制选定的单元格并复制到另一个工作簿

我正在尝试创build一个数据库,它将从主工作簿中复制选定范围的数据并复制到单独的工作簿中。

导致该问题的代码如下。 第二个工作簿根据“W2”的值打开。 新的行应插入到新的Wb中,然后格式化,然后粘贴所选单元格的值。

'Select data to be copied ActiveCell.Resize(1, 4).Copy 'Open Lessons Learned Db Location = Range("W2").Value Set Lessons = Workbooks.Open(Location) Set LL = Sheets("Lessons Learned") Windows("Lessons Learned Database.XLSM").Activate Sheets("Lessons Learned").Activate 'Insert New Row Range("5:5").Activate ActiveCell.Offset(1).EntireRow.Insert 'Enter Odd Or Even VALUE Range("A7").Select OE = ActiveCell.Value If OE = 1 Then Range("A6").Select ActiveCell.FormulaR1C1 = 0 Else Range("A6").Select ActiveCell.FormulaR1C1 = 1 End If 'Hide Permanently Hidden Rows -LINE BELOW GIVES ERROR 1004 Rows("5:5").Select Selection.EntireRow.Hidden = True Columns("A").Select Selection.EntireColumn.Hidden = True 'FORMAT ROW Range("A6").Select SC = ActiveCell.Value If SC = 1 Then Range("B6:N6").Select With Selection.Interior .ColorIndex = 15 .Pattern = xlSolid .PatternColorIndex = xlAutomatic End With End If Range("B5").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False 

任何指向我要出错的指针将不胜感激。

build议不要使用.Select.Activate因为还有其他的方法来完成这个。
因为你select并激活这可能导致ERROR 1004

下面我已经“清理”了定义LessonsLLLocation代码,并包含了MainWB并定义了您的范围。

通过定义您的Range Excel将始终从该Range获取.Value ,那么不需要使用.Select.Activate

据testing,下面的代码工作:

 Sub CopyMainWBtoNewWB() Dim Lessons As Workbook Dim LL As Worksheet Dim MainWB As Workbook Dim Location As String Set MainWB = Workbooks("Name Here") 'Open Lessons Learned Db Location = MainWB.Sheets("Sheet Name").Range("W2").Value Set Lessons = Workbooks.Open(Location) Set LL = Lessons.Sheets("Lessons Learned") 'Insert New Row LL.Rows(5).Offset(1).EntireRow.Insert shift:=xlDown 'Enter Odd Or Even VALUE If LL.Range("A7").Value = 1 Then LL.Range("A6").Value = 0 Else LL.Range("A6").Value = 1 End If 'Hide Permanently Hidden Rows -LINE BELOW GIVES ERROR 1004 LL.Rows(5).Hidden = True LL.Columns(1).Hidden = True 'FORMAT ROW If LL.Range("A6").Value = 1 Then With LL.Range("B6:N6").Interior .ColorIndex = 15 .Pattern = xlSolid .PatternColorIndex = xlAutomatic End With End If MainWB.Sheets("Sheet1").Range("A1:A4").Copy LL.Range("B5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False End Sub 

您只需要更改MainWBWorkbook名称,然后使用Sheet名称来收集Location Value

我怀疑你的代码有一些其他的问题Code Review可以帮助,但回答你的问题:

Rows("5:5").Select正在传递错误的参数数据types。

Worksheet.Rows()期待一个数字,无论是整数或长数据types,但你给它一个string。

将其更改为Rows(5) ,它应该工作。

这一切都可以巩固到:

 ActiveCell.Resize(1, 4).Copy '// not sure what this is for Set Lessons = Workbooks.Open([w2]) Set LL = Lessons.Sheets("Lessons Learned") With LL .Rows(6).EntireRow.Insert .Range("A6").value = IIf(.Range("A7").value = 1, 0, 1) .Rows(5).Hidden = True .Columns("A").Hidden = True If .Range("A6").value = 1 Then With .Range("B6:N6").Interior .ColorIndex = 15 .Pattern = xlSolid .PatternColorIndex = xlAutomatic End With End If .Range("B5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False End With 

你会注意到,这个代码直接引用每个对象而不用激活或select它。 以这种方式编码意味着每个对象都是完全合格的,并且您确切知道您正在使用哪个实例。

这应该确保该行无误地正确隐藏,因为您引用了Rows集合,该集合是具有已定义属性和方法的Ranges集合。 Selection可以是工作表,工作簿,图表,范围或其他任何你可以指向和点击的东西 – 所以当尝试访问属于特定对象或类的属性或方法时,这可能会导致问题。