如何使用.Range.Offset(2,0)将值输入Excel单元格.value=此值取自动态数组(1)



我编写的宏从word文档中提取ID的过期日期,并将它们放入一个动态数组中。

该宏是在Excel VBA编辑器中编写的,它是从Excel启动的。它正在搜索当前打开的word文档,并将该文档中的数据提取到Excel电子表格中。

接下来,它从数组中读取到期日期,并将写入Excel单元格。通常是2-4人,因此是2-4个有效期。这就是我目前所得到的:

If startPos < 3 Or endPos < 3 Then
MsgBox ("Client's names were not found!")
'Id's expiration date extraction
Else
For Each Para In rng.Paragraphs
'Extract id's expiration date
'singleParaRng = Para.Range        'Here macro changes the contract text, without need.

With singleParaRng.Find
.Text = "<do dnia [0-9]{2} [A-ząćęłńóśżź]{3;12} [0-9]{4} roku>"
.MatchWildcards = True
.MatchCase = False
.Forward = True
.Execute
If .Found = True Then
aryExpiration(i) = singleParaRng.Text
lenght = Len(aryExpiration(i)) - 8        'Result = 21
aryExpiration(i) = Right(aryExpiration(i), Len(aryExpiration(i)) - 8)
aryExpiration(i) = Left(aryExpiration(i), Len(aryExpiration(i)) - 5)
aryExpiration(i) = Trim$(aryExpiration(i))
Debug.Print aryExpiration(i)
End If
End With

ActiveSheet.Range("M" & x).Value = aryExpiration(i)
'This line above works fine but it's important 4 me to use the line below because it's more automated using the cell that will never change = $M$11
ActiveSheet.scndRng.Offset(2, 0).Value = aryExpiration(i)          'Dim scndRng As Range
'this line above is not working as expected;
'It returns an error: Object doesn't support this property or method 
x = x + 1
i = i + 1

Next Para
End If
'End of ID's expiration dates input

请帮助我使用.Range.Offset(2, 0).Value使这条线路正常工作

ActiveSheet.scndRng.Offset(2, 0).Value = aryExpiration(i)          'Dimed scndRng As Range earlier in the beginning of the procedure;

有人能告诉我这条线怎么了吗??

我在这里检查过,我正在编写的代码存在。

什么是不支持此属性或方法的对象??

作为对@BigBen评论的回应:在程序开始时,我将scndRng调暗为范围:

Dim startPos As Long
Dim endPos As Long
Dim parNmbr As Long
Dim lenght As Integer
Dim x As Long
Dim i As Long
Dim flag As Boolean
Dim scndRng As Range
Dim thrdRng As Range
Dim aryExpiration() As Variant
Dim aryNum() As Variant

我把它设置在以下行中:

'Assigning object variables and values
Set wordApp = GetObject(, "Word.Application")       'At its simplest, CreateObject creates an instance of an object,
Set excelApp = GetObject(, "Excel.Application")     'whereas GetObject gets an existing instance of an object.
Set wordDoc = wordApp.ActiveDocument
Set mySheet = Application.ActiveWorkbook.ActiveSheet
Set rng = wordApp.ActiveDocument.Content
Set idExpDate = wordApp.ActiveDocument.Content
Set singleParaRng = wordApp.ActiveDocument.Content
Set scndRng = ActiveSheet.Range("A10:O40").Find("data ważności", , xlValues)
Set thrdRng = ActiveSheet.Range("A10:O40").Find("UDZIA", , xlValues)

从麻烦的行中删除以下内容:Activesheet.

变量scndRng已经包含该级别对象,因此再次添加它会加倍并引发错误。

相关内容