Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过打印语句插入Excel VBA额外换行符_Excel_Vba - Fatal编程技术网

通过打印语句插入Excel VBA额外换行符

通过打印语句插入Excel VBA额外换行符,excel,vba,Excel,Vba,通过Excel VBA宏,我试图为Excel中的选定范围打印多达10个空格分隔的参数 例如,我的选择范围A1:A24中有24个值(例如,Val1、Val2、Val3、Val4等) 使用下面的VBA代码,我想得到“outfile.bat”中的输出,如下所示 “C:\ProgramFiles(x86)\Google\Chrome\Application\Chrome.exe”Val1 Val2。。。。瓦尔10 “C:\ProgramFiles(x86)\Google\Chrome\Applicati

通过Excel VBA宏,我试图为Excel中的选定范围打印多达10个空格分隔的参数

例如,我的选择范围A1:A24中有24个值(例如,Val1、Val2、Val3、Val4等) 使用下面的VBA代码,我想得到“outfile.bat”中的输出,如下所示

“C:\ProgramFiles(x86)\Google\Chrome\Application\Chrome.exe”Val1 Val2。。。。瓦尔10

“C:\ProgramFiles(x86)\Google\Chrome\Application\Chrome.exe”Val11 Val2。。。。瓦尔20

“C:\ProgramFiles(x86)\Google\Chrome\Application\Chrome.exe”Val21 Val22 Val23 Val24

i、 e.每行最多打印10个参数值(用空格分隔)。以上任何内容都应移到下一行(同样,最多10个空格分隔的参数)

不知何故,下面的代码是 (1) 不将输出保持在同一行和 (2) 在第10个值处插入新行,但不在第20、30和其他值处插入新行

它产生以下结果:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Val1
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Val2
C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Val3
等等

这是我的密码:

Private Sub GetChromeFile_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer, a As Integer
myFile = "C:\Users\User1\" & "outfile.bat"
Set rng = Selection

Open myFile For Output As #7
a = 0
For i = 1 To rng.Rows.Count
    Print #7, Chr(34) & "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" & Chr(34)

        a = a + 1
        cellValue = rng.Cells(i).Value
         If (a = 10) Then
            Print #7, " " & cellValue & vbNewLine
        Else
            Print #7, " " & cellValue
        End If
Next i


Close #7

Range("F5").Value = " Done!"
End Sub
请让我知道这可能会出错。
谢谢

print语句将一行打印到文件中,因此在每行末尾添加
vbNewLine
是多余的。您还为每个参数值(
cellValue
)调用
Print
,这就是为什么这些参数会出现在自己的行中

您很可能将整个文件内容构造为单个字符串,然后使用单个
Print
语句来编写整个文件。如果您正在处理大量数据,可能需要对其进行分段,但在大多数情况下,这应该是可行的:

Option Explicit
Sub writebat()
Const pathTxt$ = """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" "
Dim lineTxt As String
Dim cellValue As String
Dim fname As String
Dim ff As Long
Dim a As Long
Dim i As Long
Dim rng As Range

Set rng = Selection ' Range("A1:A37")

fname = "C:\Users\User1\" & "outfile.bat"    ' "C:\debug\output.txt"

ff = FreeFile()

Open fname For Output As #ff
    lineTxt = pathTxt
    a = 1
    For i = 1 To rng.Rows.Count
        '## Add the cell value to the string
        lineTxt = lineTxt & rng.Cells(i).Value & " "
        If a Mod 10 = 0 Then
            '## Start a new line with the executable path
            lineTxt = lineTxt & vbNewLine & pathTxt
        End If
        a = a + 1
    Next
    Print #ff, lineTxt
Close #ff
End Sub
这将产生以下输出:


快速测试,请尝试打印7“,”C:\ProgramFiles\…\chrome.exe“?(换句话说,使用双引号来打印引号,而不是
CHR()
)感谢@BruceWayne的输入。这包括在戴维哲门斯的回答中。谢谢你的帮助!非常感谢DavidZemens。工作起来很有魅力+1,并将此标记为答案!