你可以使用VBA来填充csv中的字段,同时保持预先存在的数据吗?

k0pti3hp  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(106)

第一次问任何东西在这里我一直在VBA的任务做的东西,即使我没有经验,它。我已经阅读了其他文章,这有助于我取得了一些进展,但我试图从一个电子表格中提取一些数据说A10:M10,所以从一行中选择几个单元格,然后我必须让他们在一个预先存在的.csv,有很多领域已经填充,不会改变使用,但我需要得到的数据从A10:M10在csv相当于Z2:Z12,所以一行到一列。这可能吗?到目前为止,我只设法让我的代码填充一个空白的.csv与一些标题,但我真的不认为这是可行的,我键入每一个值的每一个领域。任何帮助都非常感谢。

Sub WriteFile()

  Dim ColNum As Integer
  Dim Line As String
  Dim LineValues() As Variant
  Dim OutputFileNum As Integer
  Dim PathName As String
  Dim RowNum As Integer
  Dim SheetValues() As Variant

  PathName = Application.ActiveWorkbook.Path
  OutputFileNum = FreeFile

  Open PathName & "\file.csv" For Output Lock Write As #OutputFileNum

  Print #OutputFileNum, "Field1" & "," & "Field2" & "," & "Field3" & "," & "..." & "," & "Field26" 


  Close OutputFileNum

End Sub
u59ebvdq

u59ebvdq1#

复制行到列

Sub CopyRowToColumn()
   
    ' Source
    Dim swb As Workbook: Set swb = ThisWorkbook ' workbook containing this code
    Dim sws As Worksheet: Set sws = swb.Sheets("Sheet1")
    Dim srg As Range: Set srg = sws.Range("A10:M10") ' 13 cells hence 'Z2:Z14'

    Application.ScreenUpdating = False
    
    ' Destination
    Dim dwb As Workbook:
    Set dwb = Workbooks.Open(swb.Path & Application.PathSeparator & "File.csv")
    Dim dws As Worksheet: Set dws = wb.Sheets(1) ' the one and only
    Dim dfCell As Range: Set dfCell = dws.Range("Z2")
    Dim drg As Range: Set drg = dfCell.Resize(srg.Columns.Count)
    
    ' Copy values.
    drg.Value = Application.Transpose(srg.Value)
    
    ' Save and close.
    dwb.Close SaveChanges:=True
    
    Application.ScreenUpdating = True

    ' Inform.
    MsgBox "Row copied to column.", vbInformation
    
End Sub

相关问题