Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
(VBA EXCEL)仅用四个空格替换0_Vba_Excel_Replace_Null_Isnullorempty - Fatal编程技术网

(VBA EXCEL)仅用四个空格替换0

(VBA EXCEL)仅用四个空格替换0,vba,excel,replace,null,isnullorempty,Vba,Excel,Replace,Null,Isnullorempty,这是一个奇怪的具体问题。假设我有一个单元格,它的值为0。我正在把它写入一个文本文件 当我写它而不是0时,我希望有四个空格(它本质上是null的可视占位符) 此行的问题: cellValue = Replace(cellValue, 0, " ") 这倾向于用四个空格替换所有的零。有人知道如何解决这个问题吗?我将感谢任何和所有的帮助 样本输入: 0 | 100 | 48 样本输出: ____10048 (Underscores are spaces). 到目前为止,我所拥有的: Dim

这是一个奇怪的具体问题。假设我有一个单元格,它的值为0。我正在把它写入一个文本文件

当我写它而不是0时,我希望有四个空格(它本质上是null的可视占位符)

此行的问题:

cellValue = Replace(cellValue, 0, "    ")
这倾向于用四个空格替换所有的零。有人知道如何解决这个问题吗?我将感谢任何和所有的帮助

样本输入:

0 | 100 | 48
样本输出:

____10048 (Underscores are spaces).
到目前为止,我所拥有的:

Dim myFile As String, rng As Range, cellValue As Variant, I As Integer, j As Integer

myFile = Application.DefaultFilePath & "\test_data_output.txt"
Set rng = Selection
Open myFile For Output As #1                                    

For I = 1 To rng.Rows.Count
    For j = 1 To rng.Columns.Count
cellValue = Replace(cellValue, 0, "    ")
cellValue = cellValue + CStr(rng.Cells(I, j).Value)
If j = rng.Columns.Count Then
    Print #1, cellValue
End If

    Next j
    cellValue = ""
Next I

Close #1
---------------------------------更新--------------------------------------

所以我们决定做一个excel表格,如果为空,如果为假,则显示4个空格。现在应该可以了。谢谢你的帮助。它本质上是一组空白的细胞,被带到一张新的纸上。因此,出于某种原因,它总是显示0以表示它是空白的。我们决定如果有别的办法最好

Excel代码:

=IF(ISBLANK('OurSheetName'!I7),"    ",'OurSheetName'!I8)

您的代码有几个问题:-

  • 您将
    cellValue
    定义为
    变体
    ,没有明显的原因。最好使用最能表示数据的数据类型来声明变量。您可以简单地将其声明为
    字符串
    ,除非您有很好的理由将其保留为使用
    变体
    的昂贵工具
  • 我看不到您在哪里为
    cellValue
    赋值。这是完整的代码,还是您为过账而编辑的代码?如果代码因发布而被黑客攻击,并且没有适当的注释来解释被删除的内容,则很难对代码进行评估
  • 您只需在写入之前测试该值:-

    ...
    
    'Inside the "For j" loop
    
    cellValue = rng.Value
    
    'Test the value - but test it as a string value
    If cellValue = "0" Then
      cellValue = "    "     'Replace with 4 spaces
    End If
    
    'Carry on with code...
    

    您的代码有几个问题:-

  • 您将
    cellValue
    定义为
    变体
    ,没有明显的原因。最好使用最能表示数据的数据类型来声明变量。您可以简单地将其声明为
    字符串
    ,除非您有很好的理由将其保留为使用
    变体
    的昂贵工具
  • 我看不到您在哪里为
    cellValue
    赋值。这是完整的代码,还是您为过账而编辑的代码?如果代码因发布而被黑客攻击,并且没有适当的注释来解释被删除的内容,则很难对代码进行评估
  • 您只需在写入之前测试该值:-

    ...
    
    'Inside the "For j" loop
    
    cellValue = rng.Value
    
    'Test the value - but test it as a string value
    If cellValue = "0" Then
      cellValue = "    "     'Replace with 4 spaces
    End If
    
    'Carry on with code...
    

    您的代码有几个问题:-

  • 您将
    cellValue
    定义为
    变体
    ,没有明显的原因。最好使用最能表示数据的数据类型来声明变量。您可以简单地将其声明为
    字符串
    ,除非您有很好的理由将其保留为使用
    变体
    的昂贵工具
  • 我看不到您在哪里为
    cellValue
    赋值。这是完整的代码,还是您为过账而编辑的代码?如果代码因发布而被黑客攻击,并且没有适当的注释来解释被删除的内容,则很难对代码进行评估
  • 您只需在写入之前测试该值:-

    ...
    
    'Inside the "For j" loop
    
    cellValue = rng.Value
    
    'Test the value - but test it as a string value
    If cellValue = "0" Then
      cellValue = "    "     'Replace with 4 spaces
    End If
    
    'Carry on with code...
    

    您的代码有几个问题:-

  • 您将
    cellValue
    定义为
    变体
    ,没有明显的原因。最好使用最能表示数据的数据类型来声明变量。您可以简单地将其声明为
    字符串
    ,除非您有很好的理由将其保留为使用
    变体
    的昂贵工具
  • 我看不到您在哪里为
    cellValue
    赋值。这是完整的代码,还是您为过账而编辑的代码?如果代码因发布而被黑客攻击,并且没有适当的注释来解释被删除的内容,则很难对代码进行评估
  • 您只需在写入之前测试该值:-

    ...
    
    'Inside the "For j" loop
    
    cellValue = rng.Value
    
    'Test the value - but test it as a string value
    If cellValue = "0" Then
      cellValue = "    "     'Replace with 4 spaces
    End If
    
    'Carry on with code...
    

    在进行替换之前,只需检查单元格值是否为0

    For I = 1 To rng.Rows.Count
      For j = 1 To rng.Columns.Count
        If cellValue = 0 Then
           cellValue = Replace(cellValue, 0, "    ")
        End If
        If j = rng.Columns.Count Then
           Print #1, cellValue
        End If
      Next j
      cellValue = ""
    Next I
    

    在进行替换之前,只需检查单元格值是否为0

    For I = 1 To rng.Rows.Count
      For j = 1 To rng.Columns.Count
        If cellValue = 0 Then
           cellValue = Replace(cellValue, 0, "    ")
        End If
        If j = rng.Columns.Count Then
           Print #1, cellValue
        End If
      Next j
      cellValue = ""
    Next I
    

    在进行替换之前,只需检查单元格值是否为0

    For I = 1 To rng.Rows.Count
      For j = 1 To rng.Columns.Count
        If cellValue = 0 Then
           cellValue = Replace(cellValue, 0, "    ")
        End If
        If j = rng.Columns.Count Then
           Print #1, cellValue
        End If
      Next j
      cellValue = ""
    Next I
    

    在进行替换之前,只需检查单元格值是否为0

    For I = 1 To rng.Rows.Count
      For j = 1 To rng.Columns.Count
        If cellValue = 0 Then
           cellValue = Replace(cellValue, 0, "    ")
        End If
        If j = rng.Columns.Count Then
           Print #1, cellValue
        End If
      Next j
      cellValue = ""
    Next I
    

    匹配整个单元格内容?这是一个命令吗?0 | 100 | 48-这正是您单元格中的数据吗?是的,“|”将用这三个数字显示三个不同的单元格。您可以使用匹配整个单元格内容,它将如下所示:
    YourRange.Replacement What:=“0”,Replacement:=“0”,看:=xlWhole
    这真的很好,因为它符合我的要求。它确实会查找并替换0,但会替换所有零。所以0变成了四个空格,但是100变成了1,这不是我想要的。匹配整个单元格内容吗?这是一个命令吗?0 | 100 | 48-这正是您单元格中的数据吗?是的,“|”将用这三个数字显示三个不同的单元格。您可以使用匹配整个单元格内容,它将如下所示:
    YourRange.Replacement What:=“0”,Replacement:=“0”,看:=xlWhole
    这真的很好,因为它符合我的要求。它确实会查找并替换0,但会替换所有零。所以0变成了四个空格,但是100变成了1,这不是我想要的。匹配整个单元格内容吗?这是一个命令吗?0 | 100 | 48-这正是您单元格中的数据吗?是的,“|”将用这三个数字显示三个不同的单元格。您可以使用匹配整个单元格内容,它将如下所示:
    YourRange.Replacement What:=“0”,Replacement:=“0”,看:=xlWhole
    这真的很好,因为它符合我的要求。它确实会查找并替换0,但会替换所有零。所以0变成了四个空格,但是100变成了1,这不是我想要的。匹配整个单元格内容吗?这是一个命令吗?0 | 100 | 48-这正是你单元格中的数据吗?是的,“|”用这三个数字显示三个不同的单元格。你可以使用匹配整个单元格内容,它看起来像这样:
    YourRange.Replacement What:=“0”,Replacement:=”,LookAt:=xlWhole
    ,这真的很好,因为它有点