我需要从一个工作簿复制棕褐色单元格并粘贴到另一个工作簿中。并且只需要在Excel中获取特定的单元格值。我实现了这一点,但只能粘贴到同一工作簿中的另一个工作表中。你能帮助我在粘贴数据到另一个工作簿上的一个特定的工作表,也应该粘贴的值在第二行,(即从第二行开始),因为第一行有标题在它。
源表标题:
项目阶段状态st Dt结束Dt预资源备注备注
Dest表标题:
现有代码:
Option Explicit
Sub CopyRowsGroup()
Dim wks As Worksheet
Dim wNew As Worksheet
'Dim y As Workbook
Dim lRow As Long
Dim lNewRow As Long
Dim x As Long
Dim ptr As Long
Set wks = ActiveSheet
lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
Set wNew = Worksheets.Add
'Set y = Workbooks.Open("C:\Users\1519728\Desktop\Capacity Planning Tracker-ver1.0.xlsx")
'Workbooks.Open("C:\Users\1519728\Desktop\Capacity Planning Tracker-ver1.0.xlsx").Activate
'Set wNew = y.Sheets("Data dump")
lNewRow = 1
For x = 1 To lRow
If wks.Cells(x, 1).Interior.Color = RGB(221, 217, 195) Then
wks.Cells(x, 1).EntireRow.Copy
wNew.Cells(lNewRow, 1).PasteSpecial Paste:=xlPasteValues
lNewRow = lNewRow + 1
End If
Next
wNew.Rows([1]).EntireRow.Delete
wNew.Columns([3]).EntireColumn.Delete
wNew.Columns([3]).EntireColumn.Delete
wNew.Columns([5]).EntireColumn.Delete
wNew.Columns([6]).EntireColumn.Delete
wNew.Columns([6]).EntireColumn.Delete
wNew.Columns([6]).EntireColumn.Delete
For ptr = 2 To lNewRow - 2
If Cells(ptr, "A") = vbNullString Then
Cells(ptr, "A") = Cells(ptr, "A").Offset(-1, 0)
End If
Next
End Sub
你真的很接近让它做你想做的事情。致命的缺陷是在试图激活目标文件时第二次打开目标文件,这会擦除您在上面指定的行的y变量。没有必要使您的目标文件处于活动状态,但如果出于任何原因,您真的希望它处于活动状态,我包含了一行可以让它工作的代码。
除此之外,我做了一些小的修改,并留下了关于为什么要做这些修改的评论。
Sub CopyRowsGroup()
Dim wks As Worksheet
Dim wNew As Worksheet
Dim y As Workbook
Dim lRow As Long
Dim lNewRow As Long
Dim x As Long
Dim ptr As Long
Set wks = ActiveSheet
lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
'Set wNew = Worksheets.Add 'commented out since we're using the destination file as the paste location
Set y = Workbooks.Open("C:\Users\1519728\Desktop\Capacity Planning Tracker-ver1.0.xlsx")
'The line below is what was causing your problems. You opened the workbook again and erased your y variable
'Workbooks.Open("C:\Users\1519728\Desktop\Capacity Planning Tracker-ver1.0.xlsx").Activate 'you don't need to activate a workbook after opening it
'If you really want to make the workbook active, simply use the line below
'y.Activate
Set wNew = y.Sheets("Data dump")
'Copy the rest of your data over
lNewRow = 2 'Changed to 2 to accomodate the header in row 1
For x = 1 To lRow
If wks.Cells(x, 1).Interior.Color = RGB(221, 217, 195) Then
wks.Cells(x, 1).EntireRow.Copy
wNew.Cells(lNewRow, 1).PasteSpecial Paste:=xlPasteValues
lNewRow = lNewRow + 1
End If
Next
'wNew.Rows([1]).EntireRow.Delete 'This was deleting the header column which I am assuming was already in the sheet based on your request for the data to begin being copied to row 2
wNew.Columns([3]).EntireColumn.Delete
'wNew.Columns([3]).EntireColumn.Delete 'this was deleting the end dt column, which you listed as one of the columns you wanted to keep
wNew.Columns([5]).EntireColumn.Delete
wNew.Columns([6]).EntireColumn.Delete
wNew.Columns([6]).EntireColumn.Delete
'wNew.Columns([6]).EntireColumn.Delete 'not deleting anything so we don't need it
For ptr = 2 To lNewRow - 2
If Cells(ptr, "A") = vbNullString Then
Cells(ptr, "A") = Cells(ptr, "A").Offset(-1, 0)
End If
Next
End Sub
我写了下面的代码,并不断地看到。我试图克服这个问题,但似乎没有任何效果。我正试图从一个工作簿复制整个工作表,并将其粘贴到另一个工作簿中:
我需要一个宏,它可以根据我选择的单元格从我所在的工作簿中复制整行。我已经可以这样做了,使用以下方法: 但是,我需要它将其(格式化和全部)粘贴到关闭的工作簿的第一个空行(基于B列)。R: \dasboards\wo。xlsm公司
标题或多或少概括了它。我试图使一个简单的宏打开,然后复制/粘贴数据从txt文件到一个新打开的工作簿。我有什么工作,但它粘贴到个人工作簿,在打开一个新的“book1”之前。我可以让它粘贴到Book1,但我只想让它粘贴到打开的第一个工作簿中,以防以后我想在同一个文件中添加更多数据。我对此相当陌生,所以任何帮助都将不胜感激!!
试图从当前工作簿“Create Report.xlsm”中复制工作表名称“Headings Explantions”,该工作簿打开到我要求打开的工作簿中,我得到了下标超出范围的错误
所以在COL D中,我必须只粘贴COL A和COL C相等的值,如果这些值不相等,则跳过或粘贴COL D中的任何东西 我写过类似这样的代码,但不幸的是它粘贴了一切!!
我想将工作簿的一页(包括样式)复制到新的工作簿。 我尝试对所有单元格进行迭代 抛出java.lang.IllegalStateException:超出了单元格样式的最大数目。在.xls工作簿中最多可以定义4000个样式 抛出java.lang.IllegalArgumentException:此样式不属于提供的工作簿。是否尝试将样式从一个工作簿分配到另一个工作簿的单元格? 正确的样式复制方法是什么