我尝试将第一个工作簿中的单元格中的不同值(我们将其命名为template.xlsx)复制到另一个(item.xls)
我尝试了这个,但它不起作用(错误9)
Woorbooks("item.xls").Worksheets("Unit_Of_Measure").Cells(i, 2) = Workbooks("template.xlsx").Worksheets("Introduction").Cells(i, 8).Value
有什么建议吗?
答案 0 :(得分:0)
让我建议一个不同的方式,可能看起来很复杂,但它很简单,更干净。这个代码将完成你需要的工作,但两个工作簿应该在同一个文件夹或路径,并且重要的是让“i”var控制依赖在你需要什么。假设我们要通过位于template.xls的按钮点击来制作副本:
Private Sub button1_click()
Var_data = Introduction.Cells(i,8)
Application.ScreenUpdating = False
Workbooks.Open Filename:=ThisWorkbook.Path & "\item.xls"
ThisWorkbook.Activate
Unit_Of_Measure.Cells(i,2) = var_data
Workbooks("item.xls").Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub
首先,您在var上捕获数据,然后打开管理簿,将数据放在所需的单元格上并关闭该工作簿以保存更改(您决定是保留此行还是保持工作簿打开并手动保存)。此外,Application.screenupdating它是一条线,可以帮助您在工作簿之间切换时不会轻弹屏幕。
希望它有助于朋友!
答案 1 :(得分:0)
Dim classeur As Workbook
Dim derlig As Integer 'dernière ligne de la colonne B de template
derlig = Range("B" & Rows.Count).End(xlUp).Row
Set classeur = Application.Workbooks.Add
With classeur
Sheets.Add
Sheets.Add
Sheets.Add
Worksheets(1).Name = "Items"
Worksheets(2).Name = "Unit_Of_Measure"
Worksheets(3).Name = "Item_Tax_Authorities"
Worksheets(4).Name = "Item_Optional_Fields"
.SaveAs "C:\Users\Ikrame\Desktop\Stage SVL\mass upload\Item upload.xls"
End With
Cells(1, 1).Value = "ITEMNO"
Cells(1, 2).Value = "DESC"
Cells(1, 3).Value = "ITEMBRIKID"
Cells(1, 4).Value = "FMTITEMNO"
Cells(1, 5).Value = "CATEGORY"
Cells(1, 6).Value = "CNTLACCT"
Cells(1, 7).Value = "STOCKITEM"
Cells(1, 8).Value = "STOCKUNIT"
Cells(1, 9).Value = "UNITWGT"
Cells(1, 10).Value = "SELLABLE"
Cells(1, 11).Value = "WEIGHTUNIT"
Dim i As Long
For i = 5 To derlig
'Ne pas oublier la partie de l'ITEM NUMBER
Cells(i - 3, 3).Value = "PRODCT"
Cells(i - 3, 10).Value = "1,05"
Cells(i - 3, 11).Value = "Kg"
Cells(i - 3, 7).Value = "TRUE"
Workbooks("Item upload.xls").Worksheets("Items").Cells(i - 3, 2) = Workbooks("template.xlsx").Worksheets("Introduction").Cells(i, 3).Value
Workbooks("Item upload.xls").Worksheets("Items").Cells(i - 3, 4) = Workbooks("template.xlsx").Worksheets("Introduction").Cells(i, 2).Value