如何在Vba Excel中使用命令按钮单击自动保存为单元格值?
希望通过单击Excel中带有VBA代码的命令按钮添加第二个函数-第一个函数将数据从工作表1(订单)填充到工作表2中的数据库日志。正在查找要执行的第二个函数,该函数将自动保存订单中的单元格值。谢谢大家!如何在Vba Excel中使用命令按钮单击自动保存为单元格值?,excel,vba,Excel,Vba,希望通过单击Excel中带有VBA代码的命令按钮添加第二个函数-第一个函数将数据从工作表1(订单)填充到工作表2中的数据库日志。正在查找要执行的第二个函数,该函数将自动保存订单中的单元格值。谢谢大家! Private Sub CommandButton1_Click() Dim OrderDate As String, PONumber As String, Vendor As String, ShipTo As String, SKU As String Dim R A
Private Sub CommandButton1_Click()
Dim OrderDate As String, PONumber As String, Vendor As String, ShipTo As String, SKU As String
Dim R As Long, LastSKURow As Long, NextDBRow As Long, OFrm As Worksheet, DB As Worksheet
Set OFrm = Worksheets("Order Form 1")
Set DB = Worksheets("Database")
OrderDate = OFrm.Range("B3")
PONumber = OFrm.Range("D3")
Vendor = OFrm.Range("B7")
ShipTo = OFrm.Range("D7")
LastSKURow = OFrm.Cells(OFrm.Rows.Count, "F").End(xlUp).Row
For R = 3 To LastSKURow
SKU = OFrm.Range("F" & R).Value
NextDBRow = DB.Cells(DB.Rows.Count, "A").End(xlUp).Row + 1
DB.Range("A" & NextDBRow).Value = OrderDate
DB.Range("B" & NextDBRow).Value = PONumber
DB.Range("C" & NextDBRow).Value = Vendor
DB.Range("D" & NextDBRow).Value = ShipTo
DB.Range("E" & NextDBRow).Value = SKU
Next R
Application.ScreenUpdating = False
Dim Path As String
Dim filename As String
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,
Path = "C:\PDF\"
filename = OFrm.Range("D3")
OFrm.SaveAs filename:=Path & filename & ".pdf", FileFormat:=xlPDF
OpenAfterPublish:=False
Application.ScreenUpdating = True
End Sub
“自动保存”是指它保存excel工作簿吗?如果是这样的话,你可以添加这样的内容
Application.ActiveWorkbook.Save
如果这不是您要找的,请告诉我……对不起,花了几天时间,我一直很忙 根据您之前的评论,我编写的这个子程序应该可以解决您的问题。我选择将文件保存为pdf格式,假设您已经为表单设置了打印区域。我之所以选择pdf,是因为您声明它是一份订单,并且假设以后不会编辑该表单。我之所以选择它,是因为它是一个表单,并且可能有一些计算字段附加到另一张工作表上,如果您分离工作表,这些字段将无法工作。但是,如果您决定只需要一份工作表的副本,我也提供了它的代码。您所需要做的就是用注释填充区域,并将此函数附加到命令按钮:)。请让我知道,如果你想了解更多的信息如何做到这一点 对于pdf函数,您可以在此处查看信息: 有关另存为信息,您可以在此处查看信息:
迈克,谢谢你的反馈!通过自动保存,我的意思是我希望通过单击工作表上显示的命令按钮将当前订单工作表(OFrm)保存到预先指定的C:驱动器位置,而不一定要保存整个工作簿。一、 此外,我正在查找要另存为工作表中显示的单元格值的文件名。让我知道你能做什么!代码列在我上面的前一篇文章中。非常感谢。
Public Sub savefile()
Dim wb As Workbook
Dim ws As Worksheet
Dim rngfilename As Range
Dim strSavePath As String
'assumptions:
'you are saving the file to a fixed location and the name of the file within the range is continually changing
'you have set the print areas
Set wb = Application.ActiveWorkbook
Set ws = wb.ActiveSheet
Set rngfilename = ws.Range("A1") 'the range that contains your filename
strSavePath = "C:/" 'the location that you would like to save your file
'ensure that there is a value within the filename field declared above
If rngfilename.Value = "" Or Len(rngfilename) = 0 Then
MsgBox "You have not entered a filename"
Exit Sub
End If
'if you do not have print areas set ignore print areas to true in the function bellow
ws.ExportAsFixedFormat xlTypePDF, strSavePath & rngfilename.Value, xlQualityStandard
'for the save as function
'ws.SaveAs strSavePathe & rngfilename.Value
End Sub