使用 VBA 将单元格值复制到 Excel 中的模板 sheet
Copying Cell Value to template sheet in Excel with VBA
目前我正在使用一个主要的 Excel 工作簿,其中将成员信息输入到设置范围中,例如"Member"指的是添加了会员编号的单元格。
然后输入的信息需要 transferred/copied 到发票模板,由
表示
"folderPath & "\Templates\invoice.xlsx"",
码到
就出问题了
"Cells.Range("B11").Value = Range("Member").Value"
注意:"Member" 在主要工作sheet 上,"B11" 在模板 sheet 上。
以前使用用户表单输入数据并从那里传输到模板。下面的代码适用于该场景。
但是由于transferring/adapting它在Excel工作sheet中稍微起作用,所以它失败了。
大约有 10+ 个单元格需要复制,因此首选有效的方法。
我对此进行了研究,但尚未找到 'perfect' 答案。
Dim wb As Workbook
Set wb = Workbooks.Open(folderPath & "\Templates\invoice.xlsx")
'copy data to the template
Cells.Range("B11").Value = Range("Member").Value
使用下面的代码复制单元格
Dim xlwb As Workbook
Set xlws = xlwb.Worksheets("Sheet1")
xlws.Cells(1, 3) = (xlws.Cells(1, 1))
您需要注明要从哪个工作簿复制以及要粘贴到哪里
Dim wb As Workbook, wbk As Workbook, wbk1 As Workbook
Set wb = Workbooks.Open(folderPath & "\Templates\invoice.xlsx")
Set wbk = Workbooks("invoice.xlsx")
'repalce your soruce workbook name with correct extension xlsm
Set wbk1 = Workbooks("sorce.xlsx")
'please change the sheet index with name as you wish
wbk.Sheets(1).Cells.Range("B11").Value = wbk1.Sheets(1).Cells.Range("Member").Value
希望这就是你想要的:)
目前我正在使用一个主要的 Excel 工作簿,其中将成员信息输入到设置范围中,例如"Member"指的是添加了会员编号的单元格。
然后输入的信息需要 transferred/copied 到发票模板,由
表示"folderPath & "\Templates\invoice.xlsx"",
码到
就出问题了"Cells.Range("B11").Value = Range("Member").Value"
注意:"Member" 在主要工作sheet 上,"B11" 在模板 sheet 上。
以前使用用户表单输入数据并从那里传输到模板。下面的代码适用于该场景。 但是由于transferring/adapting它在Excel工作sheet中稍微起作用,所以它失败了。
大约有 10+ 个单元格需要复制,因此首选有效的方法。 我对此进行了研究,但尚未找到 'perfect' 答案。
Dim wb As Workbook
Set wb = Workbooks.Open(folderPath & "\Templates\invoice.xlsx")
'copy data to the template
Cells.Range("B11").Value = Range("Member").Value
使用下面的代码复制单元格
Dim xlwb As Workbook
Set xlws = xlwb.Worksheets("Sheet1")
xlws.Cells(1, 3) = (xlws.Cells(1, 1))
您需要注明要从哪个工作簿复制以及要粘贴到哪里
Dim wb As Workbook, wbk As Workbook, wbk1 As Workbook
Set wb = Workbooks.Open(folderPath & "\Templates\invoice.xlsx")
Set wbk = Workbooks("invoice.xlsx")
'repalce your soruce workbook name with correct extension xlsm
Set wbk1 = Workbooks("sorce.xlsx")
'please change the sheet index with name as you wish
wbk.Sheets(1).Cells.Range("B11").Value = wbk1.Sheets(1).Cells.Range("Member").Value
希望这就是你想要的:)