Excel 更改ActiveCell以使用各种工作簿中的数据更新工作表
我有多个工作簿,其中包含以发票格式列出的发票信息 我需要提取各种数据,并编译一个工作表,可以导入到另一个软件 以下是我编写的代码:Excel 更改ActiveCell以使用各种工作簿中的数据更新工作表,excel,vba,macos,Excel,Vba,Macos,我有多个工作簿,其中包含以发票格式列出的发票信息 我需要提取各种数据,并编译一个工作表,可以导入到另一个软件 以下是我编写的代码: Sub GetFile() Dim fNameAndPath As Variant Dim wbdata As Workbook Dim wbsource As Workbook Dim ShToCopy As Worksheet Dim rangedata As Range 'set data workbook Set wbdata = ThisWorkboo
Sub GetFile()
Dim fNameAndPath As Variant
Dim wbdata As Workbook
Dim wbsource As Workbook
Dim ShToCopy As Worksheet
Dim rangedata As Range
'set data workbook
Set wbdata = ThisWorkbook
Set rangedata = ActiveCell
'open other workbook and set as source workbook
fNameAndPath = Application.GetOpenFilename
If fNameAndPath = False Then Exit Sub
Set wbsource = Workbooks.Open(fNameAndPath)
Set ShToCopy = wbsource.Worksheets("PCO #")
Call Extract_Invoice_Data_1(wbdata, wbsource, ShToCopy, rangedata)
Call Extract_Invoice_Data_2(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_3(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_4(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_5(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_6(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_7(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_8(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_9(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_10(wbdata, wbsource, ShToCopy)
Call Extract_Invoice_Data_11(wbdata, wbsource, ShToCopy)
wbsource.Close SaveChanges:=False
Set wbsource = Nothing
End Sub
Sub Extract_Invoice_Data_1(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet, rangedata As Range)
rangedata.Value = ShToCopy.Range("G5").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_2(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("G4").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_3(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("C3").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_4(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("C4").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_5(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("C5").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_6(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("C6").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_7(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("G32").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_8(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("G25").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_9(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("G28").Value
ActiveCell.Offset(0, 1).Activate
ActiveCell = "=RC[-1]*0.15"
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_10(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("G21").Value
ActiveCell.Offset(0, 1).Activate
End Sub
Sub Extract_Invoice_Data_11(wbdata As Workbook, wbsource As Workbook,
ShToCopy As Worksheet)
Set rangedata = ActiveCell
rangedata.Value = ShToCopy.Range("G22").Value
ActiveCell.Offset(0, 1).Activate
ActiveCell = "=RC[-1]*0.15"
ActiveCell.Offset(0, 1).Activate
ActiveCell = "=SUM(RC[-4]:RC[-1])"
ActiveCell.Offset(0, 1).Activate
End Sub
问题是我无法更改ActiveCell。它将在工作表的第一个单元格中输入数据,然后不提取任何其他数据
注意:我正试图在Mac上执行此操作。未经测试,但您可以看到,在不使用
ActiveCell
/激活等的情况下,此操作是如何工作的:
Sub ChooseInputFileAndExtractData()
Dim fNameAndPath As Variant
Dim wbsource As Workbook
Dim destRow As Range
Set destRow = ActiveCell.EntireRow 'get the selected Row
'open other workbook and set as source workbook
fNameAndPath = Application.GetOpenFilename
If fNameAndPath <> False Then
Set wbsource = Workbooks.Open(fNameAndPath)
ExtractInvoiceData destRow, wbsource.Worksheets("PCO #")
wbsource.Close SaveChanges:=False
Set wbsource = Nothing
End If
End Sub
Sub ExtractInvoiceData(destRow As Range, SourceSheet As Worksheet)
With destRow
.Cells(1).Value = SourceSheet.Range("G5").Value
.Cells(2).Value = SourceSheet.Range("G4").Value
.Cells(3).Value = SourceSheet.Range("C3").Value
.Cells(4).Value = SourceSheet.Range("C4").Value
.Cells(5).Value = SourceSheet.Range("C5").Value
.Cells(6).Value = SourceSheet.Range("C6").Value
.Cells(7).Value = SourceSheet.Range("G32").Value
.Cells(8).Value = SourceSheet.Range("G25").Value
.Cells(9).Value = SourceSheet.Range("G28").Value
.Cells(10).FormulaR1C1 = "=RC[-1]*0.15" '
'etc etc you get the idea
End With
End Sub
子选择InputFileandExtractData()
Dim fNameAndPath作为变量
将wbsource设置为工作簿
Dim destRow As范围
设置destRow=ActiveCell.EntireRow'获取所选行
'打开其他工作簿并设置为源工作簿
fNameAndPath=Application.GetOpenFilename
如果fNameAndPath为False,则
设置wbsource=Workbooks.Open(fNameAndPath)
ExtractInvoiceData destRow,wbsource.工作表(“PCO#”)
wbsource.Close SaveChanges:=False
设置wbsource=Nothing
如果结束
端接头
子ExtractInvoiceData(按范围显示,按工作表显示源表)
与destRow
.Cells(1).Value=SourceSheet.Range(“G5”).Value
.Cells(2).Value=SourceSheet.Range(“G4”).Value
.Cells(3).Value=SourceSheet.Range(“C3”).Value
.Cells(4).Value=SourceSheet.Range(“C4”).Value
.Cells(5).Value=SourceSheet.Range(“C5”).Value
.Cells(6).Value=SourceSheet.Range(“C6”).Value
.Cells(7).Value=SourceSheet.Range(“G32”).Value
.Cells(8).Value=SourceSheet.Range(“G25”).Value
.Cells(9).Value=SourceSheet.Range(“G28”).Value
.单元格(10).公式1c1=“=RC[-1]*0.15”'
“等等,你明白了吗
以
端接头
您是否已在调试器中逐步完成此操作?我建议你读一读,然后重新开始。我不知道如何在调试器中逐步完成它。这实际上是我第三次把东西放在一起,所以我已经开始三次了。在我第四次尝试的时候,你能给我一个如何做得更好的建议吗?我的主要问题是,当从另一个工作簿复制粘贴时,我不知道如何在单元格中移动。非常感谢。