我有以下代码:
Private Sub UpdateButton_Click()
Dim uniqueID As Integer
uniqueID = 0
Dim notInOrder As Boolean
notInOrder = False
Dim inOrder As Boolean
inOrder = False
Dim employeeNumber As String
Dim week As Integer
Dim tdate As Date
tdate = Now
Dim qtyWelds As Integer
qtyWelds = 26
Dim totalOfWeldsInOrder As Integer
totalOfWeldsInOrder = 0
Dim qtyGridsInspected As Integer
qtyGridsInspected = 1
Dim s1 As Integer
Dim s2 As Integer
Dim s3 As Integer
Dim s4 As Integer
Dim s5 As Integer
Dim s6 As Integer
Dim s7 As Integer
Dim s8 As Integer
Dim s9 As Integer
Dim s10 As Integer
Dim s11 As Integer
Dim s12 As Integer
Dim s13 As Integer
Dim s14 As Integer
Dim s15 As Integer
Dim s16 As Integer
Dim s17 As Integer
Dim s18 As Integer
Dim s19 As Integer
Dim s20 As Integer
Dim s21 As Integer
Dim s22 As Integer
Dim s23 As Integer
Dim s24 As Integer
Dim s25 As Integer
Dim s26 As Integer
If ThisWorkbook.Worksheets(2).Shapes("cBox1").OLEFormat.Object.Value = 0 Then
totalOfWeldsInOrder = totalOfWeldsInOrder + 1
s1 = 0
notInOrder = True
End If
.....
If ThisWorkbook.Worksheets(2).Shapes("cBox26").OLEFormat.Object.Value = 0 Then
totalOfWeldsInOrder = totalOfWeldsInOrder + 1
s26 = 0
notInOrder = True
End If
Dim sInOrder As Integer
If inOrder = True Then
sInOrder = 1
Else
sInOder = 0
End If
Dim snotInOrder As Integer
If notInOrder = True Then
snotInOrder = 1
Else
snotInOrder = 0
End If
Dim rowArray(1 To 35) As String
uniqueID = GetLastRow() - 1
rowArray(1) = uniqueID
.....
rowArray(35) = s26
Dim lastRow As Long
lastRow = GetLastRow()
Debug.Print "Last Row: ", lastRow
InsertRow lastRow, rowArray
End Sub
Public Function GetLastRow() As Long
Dim wbshtSelect As Worksheet
Dim LR_wbSelectNew As Long
Set wbshtSelect = Sheets("Data")
' find last row with data in Column AI9 at current regioun starting at cell AI9
With wbshtSelect.Range("AI9").CurrentRegion
LR_wbSelectNew = .Rows(.Rows.Count).Row
End With
Dim rng As Range
Set rng = wbshtSelect.Range("AI9:AI" & LR_wbSelectNew)
' find last row inside the range, ignore values inside formulas
LR_wbSelectNew = rng.Find(What:="*", _
After:=Range("AI9"), _
LookAt:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
GetLastRow = LR_wbSelectNew
End Function
Function InsertRow(lastRow As Long, List() As String)
Dim newRow As Range
Dim rowArray As Variant
Dim wsData As Worksheet
Set wsData = Sheets("Data")
rowArray = List()
lastRow = lastRow + 1
For Each c In wsData.Range("A" & lastRow & ":AI" & lastRow)
For Each s In rowArray
c.Value = s
Next s
Next c
Debug.Print "New Row: ", lastRow
End Function
我有一个按钮,当我单击按钮时,宏运行上面的代码。代码将获取最后填充的行,并从下一行的数组(代码中)插入数据。数组正在正确填充,但是当它将其写入单元格时,所有值都返回为0。
我尝试使用CStr(variable)
转换值但没有成功(仍保存为0)。
将值写入行的函数:
Function InsertRow(lastRow As Long, List() As String)
Dim newRow As Range
Dim rowArray As Variant
Dim wsData As Worksheet
Set wsData = Sheets("Data")
rowArray = List()
lastRow = lastRow + 1
For Each c In wsData.Range("A" & lastRow & ":AI" & lastRow)
For Each s In rowArray
c.Value = s
Next s
Next c
Debug.Print "New Row: ", lastRow
End Function
知道我做错了吗?
注1:我使用的是Office 2007(Excel)
注意2:我删除了一些重复的代码。 (在代码中表示为....)
答案 0 :(得分:0)
修正了它。我想在一个范围内写一个数组。以下是有效的:
Function InsertRow(lastRow As Long, List() As String)
Dim newRow As Range
Dim rowArray As Variant
Dim i As Long
Dim wsData As Worksheet
Set wsData = Sheets("Data")
rowArray = List()
lastRow = lastRow + 1
wsData.Range("A" & lastRow & ":AI" & lastRow) = rowArray
Debug.Print "New Row: ", lastRow
End Function