hi我是VBA的新手,我有一个奇怪的错误,通过一个几乎每次都相同的循环进行四进制运算。
Sub tabele_to_database()
Dim ws As Worksheet
Dim Countries As New Collection
Dim things As New Collection
Dim Row As Boolean
Dim Coloum As Boolean
Dim i As Integer
Dim a As Integer
Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
Row = True
Coloum = True
i = 2
While Row
Countries.Add Sheets("payment terms").Cells(1, i).Value
i = i + 1
If IsEmpty(Sheets("payment terms").Cells(1, i)) Then
Row = False
End If
Wend
i = 2
While Coloum
things.Add CStr(Sheets("payment terms").Cells(i, 1).Value)
i = i + 1
If IsEmpty(Sheets("payment terms").Cells(i, 1)) Then
Coloum = False
End If
Wend
a = 1
For Each thing In things
For Each Country In Countries
ws.Cells(1, a) = (thing + " " + Country)
a = a + 1
Next Country
Next thing
End Sub
错误在中
ws.Cells(1, a) = (thing + " " + Country)
大约四分之一的路程。
变量是a=257,这是它打印的柱数事情是第7排的事情,国家是第36排的国家,所以应该一切都很好
我知道这件事,但我正在实习,对大多数事情都一无所知。
这是为了工作,但我正在实习,所以我不知道,我"学习"VBA就是为了这个。
提前感谢
试试这个:
ws.Cells(1, a) = (thing & " " & Country)
将集合变量与字符串连接时,需要使用&
而不是+
编辑:正如Vityata所指出的,这是一个很好的提示,但它并不能解决手头的问题。
这样更改代码:
For Each thing In things
For Each Country In Countries
Debug.Print things + " " + Country
ws.Cells(1, a) = thing + " " + Country
a = a + 1
Next Country
Next thing
因此,您将看到错误的确切原因。按Ctrl+G查看日志。
+
不是一个好的串联符号,但是,如果thing
和Country
都是数字,它可能会"工作"。VBA中正确的连接是使用&
。
另一个可能的错误是Integer
的使用——为了安全起见,使用vba,使用long。
在旧的Excel版本上,列的限制在250左右