简体   繁体   English

访问 vba 将 excel 中的公式复制到最后一行

[英]Access vba to copy formulas in excel to last row

I am running into a issue were the formula is getting copied down.如果公式被复制下来,我遇到了一个问题。 however it is returning 0 even if the formula is True.但是,即使公式为 True,它也会返回 0。 when using it via Access vba.通过访问 vba 使用它时。 If i open excel macro workbook and run the same formula it returns the true value in the cells to the last row.如果我打开 excel 宏工作簿并运行相同的公式,它将单元格中的真实值返回到最后一行。 could someone please look at my code and tell me what i am doing wrong.有人可以看看我的代码并告诉我我做错了什么。 im Using MS Access 2016 and i have all the necessary reference tools selected.我正在使用 MS Access 2016,并且我选择了所有必要的参考工具。 here is my code for Access vba.这是我访问 vba 的代码。

Dim xlApp3 As Object
Dim xlWBk3 Excel.Workbook
Dim xlSh3, xlSh4, xlSh5, xlSh6 As Excel.Worksheet
Dim strPath3 As String
Dim xlCopy1 As Variant
    strPath3 = "X:\Access_DataBases\Linked_Files\Funding_Integration_Workbook.xlsx"
    Set xlApp3 = CreateObject("Excel.Application") 
    Set xlWB3 = xlApp3.Workbooks.Open(strPath3)
    Set xlSh3 = xlWB3.Sheets("Copied-FMEDDW-Data")
    Set xlSh4 = xlWB3.Sheets("Data Link to FMEDDW")
    Set xlSh5 = xlWB3.Sheets("Balance Pivot (Fund Level)")
    Set xlSh6 = xlWB3.Sheets("Funding Int Review Pivot")
    xlApp3.Visible = False
    xlApp3.ScreenUpdating = False
    xlApp3.DisplayAlerts = False
xlSh4.Activate
LastrowF = xlSh3.Range("A" & xlSh3.Rows.Count).End(xlUp).Row
xlSh4.Range("AG2").Select
xlCopy1 = xlSh4.Range("AG2").Formula
xlSh4.Range("AG2").Formula = xlCopy1: xlSh4.Range("AG2:AG" & LastrowF).FillDown

When ran it copies the formula down to the last row however it returns all 0 (zeros).运行时,它将公式复制到最后一行,但它返回全 0(零)。

This formula is already sitting is excel cell AG2.这个公式已经是 excel 单元格 AG2。 Formula that is being copied:正在复制的公式:

=IF(J2="0100QP1D20",0,IF(F2="","",IF(AND(F2<>"",F2="4610",D2<>"GASD"),N2,0)))

Now if i use the excel macro workbook this is the vba line i am using:现在,如果我使用 excel 宏工作簿,这就是我正在使用的 vba 行:

Sheets("Data Link to FMEDDW").Select
Range("AG2").Select
    Lastrow = Worksheets("Copied-FMEDDW-Data").Range("A" & Rows.Count).End(xlUp).Row
    Selection.Copy
Range("AG2:AG" & Lastrow).PasteSpecial xlPasteFormulas

This works perfect when ran from excel macro workbook.从 excel 宏工作簿运行时,这非常有效。 it returns the value (Dollar amounts).它返回值(美元金额)。

A little back story with this.一个小小的背景故事。 The excel is being updated from a report of around 5k records or more and grows daily. excel 正在从大约 5k 条或更多记录的报告中更新,并且每天都在增长。 I have added column AG to return the value of the said formula.我已添加列 AG 以返回所述公式的值。 any help is greatly appreciated.任何帮助是极大的赞赏。 Thanks in andvance.谢谢你。

The error may be in the F2="4610".错误可能在 F2="4610" 中。 This is a string and 4610 is a number.这是一个字符串,4610 是一个数字。 Try something like Value(F2)=4610 as number or TEXT(F2,0) = "4610"尝试像 Value(F2)=4610 as number 或 TEXT(F2,0) = "4610"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM