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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25610971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:52:49  来源:igfitidea点击:

Adding content of a cell into a string variable in VBA. Also, adding cells below it using offset

vbaexcel-vbaoffsetexcel

提问by Han Likim

I have a simple function that is going to pull from a specific Cell (N4). If there are cells below it then it will loop and collect all the content of the cell and separate with comma.

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

I'm getting #VALUE!error in excel right now and I kind of know where my issue lies but can not figure how to fix it, since I am not that proficient with excel-vbacode. I think the issue is with ActiveSheet.Range("N4").Valueand the offset part.

我现在#VALUE!在 excel 中遇到错误,我有点知道我的问题出在哪里,但无法弄清楚如何解决它,因为我对excel-vba代码并不那么精通。我认为问题在于ActiveSheet.Range("N4").Value和偏移部分。

I am not sure how to offset then select the value in the text then add it to my string Value. Any thought on how I can select the content of cell and add to string variable and doing the same when you offset?

我不确定如何偏移然后选择文本中的值,然后将其添加到我的字符串值中。关于如何选择单元格的内容并添加到字符串变量并在偏移时执行相同操作的任何想法?

Here is my code:

这是我的代码:

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

采纳答案by Rusan Kax

Offset use is wrong. Try this.

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

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

Use of the function in a screenshot enter image description here

在屏幕截图中使用该功能 在此处输入图片说明