简体   繁体   中英

(VBA 2010) SaveAs Filename

I need help to figure out how to get vba to create and save a file in a folder on my desktop after three cells have been filled in by a scanner. Imagine a grocery store scanner, it scans a product. Three pieces of information are pulled from the bar-code and then placed in three separate cells say A1 B1 C1, how do I get VBA to see this information copy it and paste it into the saved file? this is what i have so far:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Range("A1").Value = "" And Not Range("B1").Value = "" And Not Range("C1").Value = "" Then

        Dim workbookName As String
        workbookName = "DiePunch.csv"
        ActiveCell.Range("A1:C1").Select
        Selection.Copy
        Workbooks.Add
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = Fa
        ActiveWorkbook.SaveAs Filename:="u:\CSV\Diepunch.csv", FileFormat:=xlCSV, CreateBackup:=False
        ActiveWorkbook.Save
        ActiveWindow.Close (SaveChange = False)
    End If

End Sub

I could be more helpful if you could post your code.

Use the cells(row,column) to get the values in the cells. Creating a file in VBA, I recall is done with the Freefile and Open, Print#, Close syntax.

(This is almost usable code, but not 100%)

Dim intHandle as Integer
intHandle = FreeFile()
Open "Filename.txt" for output as intHandle
Print #intHandle, Excel.Cells(Row,Column).Value
Close intHandle

This will do the job

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Range("A1").Value = "" And Not Range("B1").Value = "" And Not Range("C1").Value = "" Then

    Dim workbookName As String
    workbookName = "whatever.xls"

    ThisWorkbook.SaveAs "C:\" & workbookName

End If

End Sub

You need to decide how you want to name your file. If you just choose the workbookName to be anything, then the files are saved with an increment in the file name, like

workbookName(1).xls
workbookName(2).xls

etc..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM