我似乎会遇到类似的问题,只要我搞乱了很多工作簿,所以我想这是一个普遍的问题,当我处理多个工作簿,并不局限于只是复制单元格······
Sub TrySaveAs()
Dim wkb0 As Workbook
Set wkb0 = ThisWorkbook
Dim wkb1 As Workbook
Dim wkb2 As Workbook
Dim i As Integer
i = 2
Do Until IsEmpty(wkb0.Worksheets("Sheet2").Cells(i, 1))
Dim full_path As String
full_path = C:\something\something.xlsx
Set wkb1 = Workbooks.Open(Filename:=somename)
wkb1.SaveAs Filename:=full_path
Set wkb2 = Workbooks.Open(Filename:=someothername)
Dim last_row As Integer
last_row = wkb2.Worksheets("RAW").Range("A1").End(xlDown).Row
wkb2.Worksheets("RAW").Range(Cells(1, 1), Cells(last_row, 5)).Copy <--------problematic
wkb1.Worksheets("Sheet1").Range(Cells(1, 1), Cells(last_row, 5)).Paste <--------problematic
wkb2.Close SaveChanges:=False
wkb1.Close SaveChanges:=True
i = i + 1
Loop
End Sub
你可以做
wkb2.worksheets(“raw”).range(单元格(1,1).address&“:”&单元格(last_row,5).address).copy
和
Sub hello()
Dim rr1 As Range
Dim rr2, rr3 As Range
Debug.Print Range(Worksheets(1).Cells(1, 1), Worksheets(1).Cells(3, 3)).Address(External:=False)
Set rr1 = Range(Worksheets(1).Cells(1, 1), Worksheets(1).Cells(3, 3))
Debug.Print rr1.Parent.Name & "'!" & rr1.Address(External:=False)
MsgBox rr1.Parent.Name & "'!" & rr1.Address(External:=False)
'notice it says "Sheet1'!$A$1:$C$3
Worksheets(2).Activate
Set rr2 = Cells(1, 1) 'if we dont qualify Cells, with a range object, it defaults to cells of ActiveSheet
Set rr3 = Cells(3, 3)
Debug.Print Range(rr2, rr3).Parent.Name & "'!" & Range(rr2, rr3).Address(External:=False)
MsgBox Range(rr2, rr3).Parent.Name & "'!" & Range(rr2, rr3).Address(External:=False)
'notice it says "Sheet2'!$A$1:$C$3, because that is the current ActiveSheet
Worksheets(3).Activate
Set rr2 = Cells(1, 1) 'if we dont qualify Cells with a range object, it defaults to cells of ActiveSheet
Set rr3 = Cells(3, 3)
Debug.Print Range(rr2, rr3).Parent.Name & "'!" & Range(rr2, rr3).Address(External:=False)
MsgBox Range(rr2, rr3).Parent.Name & "'!" & Range(rr2, rr3).Address(External:=False)
'notice it says "Sheet3'!$A$1:$C$3, because that is the current ActiveSheet
On Error GoTo theError
Debug.Print Worksheets(1).Range(rr1, rr2).Address
MsgBox Worksheets(1).Range(rr1, rr2).Address
theError: Debug.Print "Error because ranges rr1 and rr2 are cells of Worksheet(3), not Worksheet(1)"
Debug.Print Range(Cells(1, 1), Cells(3, 3)).Parent.Name & "'!" & _
Range(Cells(1, 1), Cells(3, 3)).Address(External:=False)
MsgBox Range(Cells(1, 1), Cells(3, 3)).Parent.Name & "'!" & _
Range(Cells(1, 1), Cells(3, 3)).Address(External:=False)
'notice it says "Sheet3'!$A$1:$C$3, because that is the current ActiveSheet
'Moral of story you cannot do Worksheets(1).range(cells(a,b),cells(c,d)) because
'cannot guarantee Cells refer to Worksheet(1)
'If you really want to refer to unqualified Cells, one way is to mimic the Excel
'range syntax by grabbing the cell address
Debug.Print Worksheets(1).Range(Cells(1, 1).Address & ":" & Cells(3, 3).Address).Parent.Name & "'!" & _
Worksheets(1).Range(Cells(1, 1).Address & ":" & Cells(3, 3).Address).Address(External:=False)
MsgBox Worksheets(1).Range(Cells(1, 1).Address & ":" & Cells(3, 3).Address).Parent.Name & "'!" & _
Worksheets(1).Range(Cells(1, 1).Address & ":" & Cells(3, 3).Address).Address(External:=False)
End Sub
我使用这段代码将工作簿中的每一张工作表复制到一个新的工作簿中,它工作得很好,但它颠倒了工作表的顺序,是否有任何办法阻止它这样做? 我正在复制所有的工作表,这样我就可以将它保存为不同的文件扩展名,这是我发现唯一有效的方法。
这里是VBA新手。 谢谢你的教育!
我试图将活动工作表和2个指定工作表复制到一个新工作簿,然后让宏继续在新工作簿上运行,以便在保存它之前更改一些内容 null
> sheet1:用户单击运行VBA的按钮。 第2:VBA选择第D列上的第一个非空白单元格 Range(“d”&rows.count).end(xlUp).offsed(1).select 多谢!
当在列中单击文本“发送调查”时,我正在尝试从工作表“”上的活动行复制单元格b中的值,并将其粘贴到单元格中。A2在我的另一个工作簿“”上的“”表。 由于某种原因,我没有收到任何错误,但是没有任何内容被粘贴到我的其他工作簿的单元格A2中。 有人能告诉我哪里出了问题吗。提前感谢
我写了一个google脚本来创建和粘贴从一个单元格到另一个单元格的值(相同的电子表格)。该守则包括以下两个步骤: > (不工作):将相邻单元格的新值粘贴到第二张工作表中下一个空行(第1列)的单元格中。 下面的代码是我迄今为止尝试过的代码,但第二页上没有显示该值。有人知道我在下面的尝试中的问题在哪里吗? 谢谢你