VBA是复制三个单元格的范围,并将其保存到另一个文件,我怎么得到它来增加范围

我的VBA代码是来自扫描条形码的扫描器的馈送input,然后将三条信息input到单元A1 B1和C1。 第一次在它周围工作正常,但是当我尝试在下面的行上做它不起作用。 我知道这与我select的范围有关,但我不知道如何增加范围。 这是我的代码到目前为止:

Private Sub Worksheet_Change(ByVal Target As Range) Dim String_1 As String Dim String_2 As String Dim String_3 As String String_1 = "400" String_2 = "401" String_3 = "402" If Len(Range("A1").Value) > 0 And Len(Range("B1").Value) > 0 And Len(Range("C1").Value) > 0 Then Dim sComp As String sComp = Left(Range("C1"), 3) If sComp = String_1 Or sComp = String_2 Or sComp = String_3 Then Range("A1:C1").Copy Dim wbCopy As Workbook Set wbCopy = Workbooks.Add With wbCopy .Sheets(1).Range("A1").PasteSpecial xlPasteValues Application.DisplayAlerts = False .SaveAs Filename:="u:\CSV\Diepunch" & sComp & ".csv", FileFormat:=xlCSV, CreateBackup:=False Application.DisplayAlerts = True .Close False End With End If End If End Sub 

我需要这个工作,所以每次有一个不同的数字由string触发正确的数据保存到正确的文件。 任何人有任何见解如何解决这个问题或技巧? 帮助表示赞赏。

您需要find下一个空行来粘贴数据。

私人小组Worksheet_Change(BYVAL目标作为范围)

     Dim String_1 As String
    昏暗的String_2作为string
     Dim String_3 As String
     Dim newRow As Long
     String_1 =“400”
     String_2 =“401”
     String_3 =“402”

    如果Len(Range(“A1”)。Value)> 0并且Len(Range(“B1”)。Value)> 0并且Len(Range(“C1”)。Value)> 0 Then

        昏暗sComp作为string
         sComp =左(范围(“C1”),3)

        如果sComp = String_1或者sComp = String_2或者sComp = String_3那么

            范围( “A1:C1”)复制。
            昏暗的wbCopy作为工作簿
            设置wbCopy = Workbooks.Add

            用wbCopy
                 newRow = .Range(“A1”)。End(xlUp).Row + 1
                 .Sheets(1).Cells(newRow,1).PasteSpecial xlPasteValues
                 Application.DisplayAlerts = False
                 .SaveAs文件名:=“u:\ CSV \ Diepunch”&sComp&“.csv”,FileFormat:= xlCSV,CreateBackup:= False
                 Application.DisplayAlerts = True
                 。closures错误

            结束

        万一

    万一

结束小组

您必须同时检查是否存在要更新的CSV文件,并查找最后一行以在其后添加新数据

一个快速和肮脏的修复:

 Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim String_1 As String Dim String_2 As String Dim String_3 As String Dim myFileName As String Dim wbCopy As Workbook Dim lastRow As Long String_1 = "400" String_2 = "401" String_3 = "402" If Len(Range("A1").Value) > 0 And Len(Range("B1").Value) > 0 And Len(Range("C1").Value) > 0 Then Dim sComp As String sComp = Left(Range("C1"), 3) If sComp = String_1 Or sComp = String_2 Or sComp = String_3 Then ' myFileName = "u:\CSV\Diepunch" & sComp & ".csv" Range("A1:C1").Copy Application.ScreenUpdating = False If Dir(myFileName) = "" Then Set wbCopy = Workbooks.Add Else Set wbCopy = Workbooks.Open(myFileName) End If With wbCopy With .Sheets(1) lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row If .Cells(lastRow, 1).Value <> "" Then lastRow = lastRow + 1 .Cells(lastRow, 1).PasteSpecial xlPasteValues End With Application.DisplayAlerts = False .SaveAs Filename:=myFileName, FileFormat:=xlCSV, CreateBackup:=False Application.DisplayAlerts = True .Close False End With Application.ScreenUpdating = True End If End If End Sub