简体   繁体   English

如何使用 VBA 将单元格值(不是范围)从 Excel 工作表插入到现有的 csv 文件中

[英]How to I insert cell values (not a range) from an Excel sheet to an existing csv file using VBA

I have successfully written code to insert cell values from a worksheet into an SQL table using loop.我已经成功编写了代码,使用循环将工作表中的单元格值插入到 SQL 表中。 There are unused columns and unused rows and incorrect headers in the original sheet which have been managed by the VBA code Now the database is being shut down and we want to use a csv file to upload the data to another database.原始工作表中有未使用的列和未使用的行以及不正确的标题,它们已由 VBA 代码管理 现在数据库正在关闭,我们想使用 csv 文件将数据上传到另一个数据库。 What code is used to open the csv file and insert the data cell by cell?什么代码用于打开csv文件并逐个单元格插入数据?

  1. Iterate through all active worksheets.遍历所有活动工作表。
  2. For each worksheet, iterate through all used rows.对于每个工作表,遍历所有使用的行。
  3. For each row, iterate through all columns.对于每一行,遍历所有列。
  4. Build line string by wrapping each cell values in double quotes and delimiting with commas.通过将每个单元格值用双引号括起来并用逗号分隔来构建行字符串。
  5. Write line string to csv file.将行字符串写入 csv 文件。
Const sFilePath = "C:\test\myfile.csv"
Const strDelim = ","
Sub CreateCSV_Output()
    Dim ws As Worksheet
    Dim rng1 As Range
    Dim X
    Dim lRow As Long
    Dim lCol As Long
    Dim strTmp As String
    Dim lFnum As Long

    lFnum = FreeFile
    Open sFilePath For Output As lFnum

    For Each ws In ActiveWorkbook.Worksheets
        'test that sheet has been used
        Set rng1 = ws.UsedRange
        If Not rng1 Is Nothing Then
            'only multi-cell ranges can be written to a 2D array
            If rng1.Cells.Count > 1 Then
                X = ws.UsedRange.Value2
                'The code TRANSPOSES COLUMNS AND ROWS by writing strings column by column
                For lCol = 1 To UBound(X, 2)
                    'write initial value outside the loop
                     strTmp = IIf(InStr(X(1, lCol), strDelim) > 0, """" & X(1, lCol) & """", X(1, lCol))
                    For lRow = 2 To UBound(X, 1)
                        'concatenate long string & (short string with short string)
                        strTmp = strTmp & (strDelim & IIf(InStr(X(lRow, lCol), strDelim) > 0, """" & X(lRow, lCol) & """", X(lRow, lCol)))
                    Next lRow
                    'write each line to CSV
                    Print #lFnum, strTmp
                Next lCol
            Else
                Print #lFnum, IIf(InStr(ws.UsedRange.Value, strDelim) > 0, """" & ws.UsedRange.Value & """", ws.UsedRange.Value)
            End If
        End If
    Next ws

    Close lFnum
    MsgBox "Done!", vbOKOnly

End Sub

Ref 参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想使用VBA将excel中的命名范围插入到word文件中的特定位置的现有word文件中(它有20个表) - I want to insert a named range from excel to an existing word file at a specific position in the word file (it has 20 tables) using VBA 如何使用Excel VBA将一张纸中的值分配给隐藏的纸中? (并跳过范围内的列?) - How to assign values from one sheet into hidden sheet using Excel VBA? (and skip a column within the range?) 如何使用VBA将多个字符串值添加到Excel工作表中的单元格 - How to add multiple string values to a cell in a excel sheet using vba 使用vba将信息从.csv文件中的单元格提取到Excel中的变量中 - Pulling information from a cell in a .csv file into a variable in Excel using vba Excel VBA:如果列中的值匹配,则将值从工作表1插入到工作表2 - Excel VBA: Insert values from sheet 1 to sheet 2 if value in column matches 如何在 VBA 中的现有单元格值之前插入单元格值 - How to insert cell values before existing cell values in VBA 如何使用VBA将ETABS给定的一维数组中的值插入excel中的范围 - How to insert values from a one dimensional array given by ETABS into a range in excel using VBA 如何使用 Excel 宏 VBA 创建一个新文件,其中数字作为初始文件中工作表的值? - How using the Excel macro VBA to create a new file where numbers as values from the sheet in the initial file? Excel VBA-根据图纸上的单元格值从阵列中粘贴值 - Excel VBA - Paste values from array based on cell values on the sheet 使用VBA并使用“偏移”将一系列单元格值复制并粘贴到另一张工作表中 - Copy and paste a range of cell values into another sheet with VBA and using Offset
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM