从单元格读取字符串出错

时间:2015-11-07 14:18:45

标签: excel vba excel-vba

Sub nationalityscript()
Dim i As Integer
Dim temp As Integer
Dim Country As String
Dim strEnd As Integer
Dim strComplete As String
i = 0
Do While i < 300
    Dim k As Integer
    Dim a As String

    strComplete = Cells(h, i).Value
    strEnd = Len(strComplete)
    k = 0
    Do While k < strEnd
        a = Cells(h, i).Value.Substring(k, k)
        If a = "," Then
            temp = a
        End If
        k = k + 1
    Loop
    Country = Cells(h, i).Value.Substring(temp, strEnd)
i = i + 1
Loop
End Sub

嗨,这是我在vba编程的第一天,所以请放轻松。

我在excel中编写,专注于 h 列。代码将在列h中找到的任何字符串,在最后一个“,”之后识别字符串的最后一部分,然后用它得到的子字符串替换该单元格中的字符串。

我认为这可能与我的问题无关,因为我的问题是将单元格中的字符串分配给新变量。

我的问题在于此处突出显示的行。 enter image description here

我得到的错误是:

  

运行时错误1004,对象定义或应用程序定义的错误。

2 个答案:

答案 0 :(得分:3)

您的程序正在寻找一些名为“h”的变量或标记。由于没有,编译器抱怨。 Cells获取行和列的整数,而不是列字母。因此,如果用8替换h(第8列),它应该可以解决这个问题。

<强>更新

抱歉,我认为您还需要Cells(i, 8),因为语法为Cells(RowNumber, ColumnNumber)

答案 1 :(得分:1)

首先,第j列中第i行的值为 细胞(I,J)。

其次,您需要将h声明为列或常量...

第三,我不确定以这种方式循环变量声明

sudo mv /home/cam/DCS-*.jpg /home/cam/cam/

我认为它不会起作用。