我正在使用此方法自动增加excel电子表格中的列A;
=IF(B3="","",A2+1)
我这样做是为了在将数据添加到B列时,A列中的ID号增加1。
同时,我有一个Word文档,在其中添加了命令按钮。这个想法是,用户将按下命令按钮,它将使用Word Doc中的数据填充B到E列,这将导致A递增,然后我要获取A的值并将其放回ActiveX文本框在doc一词上。除了将A列中递增的ID#传输回Word文档外,我一切正常。这是我到目前为止所拥有的。第一个函数为我找到最后一行,以便将数据从word doc填充到excel电子表格。为此目的,它可以正常工作,但以相反的方式使用它是行不通的。第二个功能是我用来确保必填字段中包含实际数据的东西,因此我不尝试将空白字段写入电子表格。
Function GetLastRow(ByVal col As String) As Long
With Worksheets("Sheet1")
GetLastRow = .Range(col & .Rows.Count).End(xlUp).Row + 1
End With
End Function
Function RequiredField(strQuestion, strMessage)
If strQuestion.Text = "" Then
Do
sInFld = InputBox(strMessage)
Loop While sInFld = ""
strQuestion.Text = sInFld
End If
'Required Field Prompt Function w/ input box
End Function
Private Sub RequestClaimNumber_Click()
Dim wkbk As Workbook
Dim doc As Object
Dim app As Object
Dim sdocname As String
Dim LastRow As Long
Set app = GetObject(, "Word.Application")
sdocname = "J:\Correspondence\CAST Database\Word Docs\General Loss Form.docm"
Set doc = ThisDocument
Set wkbk = Workbooks.Open("J:\Accident Reports\98 Claims Register.xlsx")
With wkbk.Sheets("Sheet1")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
Debug.Print LastRow
RequiredField PolicyName, "Please insert a name for the Policy Holder in the 'Policy Holder Name' field."
RequiredField InsuredName, "Please insert a name for the Contractor in the 'Insured Name' field."
RequiredField DOL, "Please enter a Date of Loss for this incident."
With wkbk.Sheets("Sheet1")
rw = GetLastRow("B")
.Range("B" & rw) = ThisDocument.ReportDate
rw = GetLastRow("C")
.Range("C" & rw) = ThisDocument.DOL
rw = GetLastRow("D")
.Range("D" & rw) = ThisDocument.PolicyName
rw = GetLastRow("E")
.Range("E" & rw) = ThisDocument.InsuredName
With doc
doc.FormFields("ClaimNumber").Result = .Range(LastRow).Value
End With
End With
wkbk.Save
wkbk.Close
Set wkbk = Nothing
End Sub
问题似乎出在这里:
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
当我调试该行时,它将返回数字30687。我假设这应该是它在电子表格上返回的单元格。我也得到
“下标超出范围”
错误,并突出显示该行;
doc.FormFields("ClaimNumber").Result = .Range(LastRow).Value
我的假设是,它不会在A列中找到实际最后使用的单元格。它将到达A的最底端,并且什么也找不到。为此,我尝试了几种尝试找到最后一个单元格的方法,但是它们都给我相同的下标超出范围的错误,并且在我调试它们时都返回30687的值。
如果我不知道有其他方式可以这样做,我愿意提出建议。否则,我只需要弄清楚我现有代码的问题所在,以便可以正常工作。
答案 0 :(得分:1)
您没有提供一列,而是仅提供一行,因此您需要说出.Range("X" & LastRow).Value
或.cells(lastrow,24)
答案 1 :(得分:0)
我知道了。这是有效的最终代码。
Private Sub RequestClaimNumber_Click()
Dim wkbk As Workbook
Dim lastColumn As Long
RequiredField PolicyName, "Please insert a name for the Policy Holder in the 'Policy Holder Name' field."
RequiredField InsuredName, "Please insert a name for the Contractor in the 'Insured Name' field."
RequiredField DOL, "Please enter a Date of Loss for this incident."
Set wkbk = Workbooks.Open("J:\Accident Reports\98 Claims Register.xlsx")
With wkbk.Sheets("Sheet1")
lastColumn = .Range("B" & .Rows.Count).End(xlUp).Row
End With
With wkbk.Sheets("Sheet1")
rw = GetLastRow("B")
.Range("B" & rw) = ThisDocument.ReportDate
rw = GetLastRow("C")
.Range("C" & rw) = ThisDocument.DOL
rw = GetLastRow("D")
.Range("D" & rw) = ThisDocument.PolicyName
rw = GetLastRow("E")
.Range("E" & rw) = ThisDocument.InsuredName
rw = GetLastRow("F")
.Range("F" & rw) = ThisDocument.UserID
ClaimNumber.Text = .Range("A" & lastColumn + 1).Text
End With
wkbk.Save
wkbk.Close
Set wkbk = Nothing
End Sub