用wordmacros返回excel的最后一行
如何使用Wordmacros复制未定义的范围从excel和过去的Word文档。 我尝试了下面,但不能成功。 你能帮我么。
注意:如果我提到范围工作正常,而我想使用代码NumberOfRows = .Range("A65536").End(xlUp).Row
macrosmacros – 我得到运行时错误1004。
Sub InputExcel() Set appExcel = CreateObject("Excel.Application") Dim INP_File As Variant Dim lngRows As Long Dim LenMgs As Long Dim NumberOfRows As String INP_File = appExcel.GetOpenFilename("Excel files (*.xlsx;*.xls),*.xlsx;*.xls", 2) appExcel.Workbooks.Open INP_File If INP_File > 0 Then Selection.Find.ClearFormatting With Selection.Find .Text = "Sample" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.HomeKey Unit:=wdLine LenMgs = appExcel.Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count 'NumberOfRows = appExcel.Worksheets("Sheet1").Range("A65536").End(xlUp).Row Set Rng = appExcel.Worksheets("Sheet1").Range(appExcel.Worksheets("Sheet1").Cells(4, 1), appExcel.Worksheets("Sheet1").Cells(LenMgs, 5)) 'Rng.Copy 'appExcel.Worksheets("Sheet1").Range("A1:B5").Copy - This is working !! if I specify the range. Selection.Paste End If appExcel.ActiveWorkbook.Close appExcel.Quit Set appExcel = Nothing End Sub
您正在收到错误,因为xlUp
是一个Excel常量,并且它不能被MS Word识别,因为您正在使用Late Binding与Excel连接。
你必须声明这是你的代码的顶部
Const xlup = -4162
你也可能想阅读这个在Excel中查找最后一行?