在VBA中将单元格内容添加到字符串变量中。此外,还可以使用偏移量在其下方添加单元格

在VBA中将单元格内容添加到字符串变量中。此外,还可以使用偏移量在其下方添加单元格,vba,excel,offset,Vba,Excel,Offset,我有一个简单的函数,它将从一个特定的单元格N4中提取。如果下面有单元格,它将循环并收集单元格的所有内容,并用逗号分隔 我得到了价值!现在excel中出现错误,我知道问题出在哪里,但不知道如何解决,因为我对excel vba代码不太精通。我认为问题在于ActiveSheet.RangeN4.Value和偏移部分 我不知道如何偏移,然后在文本中选择值,然后将其添加到我的字符串值。 关于如何选择单元格内容并添加到字符串变量,以及在偏移量时执行相同操作,您有什么想法吗 这是我的密码: Function

我有一个简单的函数,它将从一个特定的单元格N4中提取。如果下面有单元格,它将循环并收集单元格的所有内容,并用逗号分隔

我得到了价值!现在excel中出现错误,我知道问题出在哪里,但不知道如何解决,因为我对excel vba代码不太精通。我认为问题在于ActiveSheet.RangeN4.Value和偏移部分

我不知道如何偏移,然后在文本中选择值,然后将其添加到我的字符串值。 关于如何选择单元格内容并添加到字符串变量,以及在偏移量时执行相同操作,您有什么想法吗

这是我的密码:

Function pullshit() As String

Dim output As String
Dim counter As Integer

counter = 1

output = ActiveSheet.Range("N4").Value

If Application.offset(N4, counter, 0).Value = "" Then

    pullshit = output

Else
    While counter <> 0
        output = output + ", " + Application.offset(N4, counter, 0).Value
        counter = counter + 1

        If Application.offset(N4, counter, 0) = "" Then
            counter = 0
        End If
    Wend

    pullshit = output

End If

End Function

使用偏移量是错误的。试试这个

Function pullshit() As String

Dim output As String
Dim counter As Integer

counter = 1

output = ActiveSheet.Range("N4").Value

If Range("N4").Offset(counter, 0).Value = "" Then

    pullshit = output

Else
    While counter <> 0
        output = output + ", " + Range("N4").Offset(counter, 0).Value
        counter = counter + 1

        If Range("N4").Offset(counter, 0) = "" Then
            counter = 0
        End If
    Wend

    pullshit = output

End If

End Function
在屏幕截图中使用该函数

您的偏移呼叫无效。我应该如何呼叫我的偏移@Chrisneilsen出于某种原因在进行此更新后,我得到了一个名称?excel中出错。我刚尝试了一个新文件,结果很好。多谢各位。