将文件名保存为CSV和单元格值

时间:2021-08-10 22:20:30

I have a macro to export a range of cells to csv which is fine, I now need it to call the file as a cell value, preferably B2.

我有一个宏输出一系列的单元格到csv,这很好,我现在需要它把文件称为单元格值,最好是B2。

Thanks in advance.

提前谢谢。

Macro:

宏:

Sub WriteCSVFile()

    Dim My_filenumber As Integer Dim logSTR As String

    My_filenumber = FreeFile

    logSTR = logSTR & Cells(2, "B").Value & " , " 
    logSTR = logSTR & Cells(3, "B").Value & " , " 
    logSTR = logSTR & Cells(4, "B").Value & " , " 
    logSTR = logSTR & Cells(5, "B").Value & " , " 
    logSTR = logSTR & Cells(6, "B").Value & " , " 
    logSTR = logSTR & Cells(7, "B").Value & " , " 
    logSTR = logSTR & Cells(8, "B").Value & " , " 
    logSTR = logSTR & Cells(9, "B").Value & " , " 
    logSTR = logSTR & Cells(10, "B").Value & " , " 
    logSTR = logSTR & Cells(11, "B").Value & " , " 
    logSTR = logSTR & Cells(12, "B").Value & " , " 
    logSTR = logSTR & Cells(13, "B").Value & " , " 
    logSTR = logSTR & Cells(14, "B").Value & " , " 
    logSTR = logSTR & Cells(15, "B").Value & " , " 
    logSTR = logSTR & Cells(16, "B").Value & " , " 
    logSTR = logSTR & Cells(18, "B").Value & " , " 
    logSTR = logSTR & Cells(19, "B").Value & " , " 
    logSTR = logSTR & Cells(20, "B").Value & " , " 
    logSTR = logSTR & Cells(21, "B").Value & " , " 
    logSTR = logSTR & Cells(22, "B").Value & " , " 
    logSTR = logSTR & Cells(23, "B").Value & " , " 
    logSTR = logSTR & Cells(24, "B").Value & " , " 
    logSTR = logSTR & Cells(25, "B").Value & " , " 
    logSTR = logSTR & Cells(26, "B").Value & " , " 
    logSTR = logSTR & Cells(27, "B").Value & " , " 
    logSTR = logSTR & Cells(28, "B").Value & " , " 
    logSTR = logSTR & Cells(29, "B").Value & " , " 
    logSTR = logSTR & Cells(30, "B").Value & " , " 
    logSTR = logSTR & Cells(31, "B").Value & " , " 
    logSTR = logSTR & Cells(32, "B").Value & " , " 
    logSTR = logSTR & Cells(33, "B").Value & " , " 
    logSTR = logSTR & Cells(35, "B").Value & " , " 
    logSTR = logSTR & Cells(36, "B").Value & " , " 
    logSTR = logSTR & Cells(37, "B").Value & " , " 
    logSTR = logSTR & Cells(38, "B").Value & " , " 
    logSTR = logSTR & Cells(39, "B").Value & " , " 
    logSTR = logSTR & Cells(40, "B").Value & " , " 
    logSTR = logSTR & Cells(41, "B").Value & " , " 
    logSTR = logSTR & Cells(42, "B").Value & " , " 
    logSTR = logSTR & Cells(45, "B").Value & " , " 
    logSTR = logSTR & Cells(46, "B").Value & " , " 
    logSTR = logSTR & Cells(47, "B").Value & " , " 
    logSTR = logSTR & Cells(48, "B").Value & " , " 
    logSTR = logSTR & Cells(49, "B").Value & " , " 
    logSTR = logSTR & Cells(50, "B").Value & " , " 
    logSTR = logSTR & Cells(51, "B").Value & " , " 
    logSTR = logSTR & Cells(52, "B").Value & " , " 
    logSTR = logSTR & Cells(54, "B").Value & " , " 
    logSTR = logSTR & Cells(55, "B").Value & " , " 
    logSTR = logSTR & Cells(56, "B").Value & " , " 
    logSTR = logSTR & Cells(57, "B").Value & " , " 
    logSTR = logSTR & Cells(58, "B").Value & " , " 
    logSTR = logSTR & Cells(59, "B").Value & " , " 
    logSTR = logSTR & Cells(60, "B").Value & " , " 
    logSTR = logSTR & Cells(61, "B").Value & " , " 
    logSTR = logSTR & Cells(62, "B").Value & " , " 
    logSTR = logSTR & Cells(63, "B").Value & " , " 
    logSTR = logSTR & Cells(64, "B").Value & " , " 
    logSTR = logSTR & Cells(66, "B").Value & " , " 
    logSTR = logSTR & Cells(67, "B").Value & " , " 
    logSTR = logSTR & Cells(68, "B").Value & " , " 
    logSTR = logSTR & Cells(69, "B").Value & " , " 
    logSTR = logSTR & Cells(70, "B").Value & " , " 
    logSTR = logSTR & Cells(71, "B").Value & " , " 
    logSTR = logSTR & Cells(72, "B").Value & " , " 
    logSTR = logSTR & Cells(73, "B").Value & " , "

    Open "T:\Typing\Client Information\Test1.csv" For Append As
    #My_filenumber
        Print #My_filenumber, logSTR Close #My_filenumber

End Sub

2 个解决方案

#1


1  

If B2 is XXX this produces XXX.CSV (with a loop!)

如果B2是XXX,则产生XXX。CSV(循环)

Sub WriteCSVFile()
    Dim My_filenumber As Integer
    Dim i As Long
    Dim logSTR As String, fileName as String

    My_filenumber = FreeFile

    For i = 2 To 73
        logSTR = logSTR & Cells(i, "B").Value & " , "
    Next

    fileName = "T:\Typing\Client Information\" & Range("B2").Value & ".csv"

    Open fileName For Append As #My_filenumber
        Print #My_filenumber, logSTR
    Close #My_filenumber
End Sub

#2


0  

As I understood, all you want now is save the file as "B2.csv"

正如我所理解的,您现在需要的是将文件保存为“B2.csv”

Maybe this question it answers to your question, take a look:

也许这个问题可以回答你的问题,看看:

Saving excel worksheet to CSV with file name from a cell using a macro

使用宏从单元格中保存excel工作表到CSV

#1


1  

If B2 is XXX this produces XXX.CSV (with a loop!)

如果B2是XXX,则产生XXX。CSV(循环)

Sub WriteCSVFile()
    Dim My_filenumber As Integer
    Dim i As Long
    Dim logSTR As String, fileName as String

    My_filenumber = FreeFile

    For i = 2 To 73
        logSTR = logSTR & Cells(i, "B").Value & " , "
    Next

    fileName = "T:\Typing\Client Information\" & Range("B2").Value & ".csv"

    Open fileName For Append As #My_filenumber
        Print #My_filenumber, logSTR
    Close #My_filenumber
End Sub

#2


0  

As I understood, all you want now is save the file as "B2.csv"

正如我所理解的,您现在需要的是将文件保存为“B2.csv”

Maybe this question it answers to your question, take a look:

也许这个问题可以回答你的问题,看看:

Saving excel worksheet to CSV with file name from a cell using a macro

使用宏从单元格中保存excel工作表到CSV