Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 复制另一张图纸后给出错误值的公式_Vba - Fatal编程技术网

Vba 复制另一张图纸后给出错误值的公式

Vba 复制另一张图纸后给出错误值的公式,vba,Vba,我对工作簿“data_base”有一个小问题……将数据复制到另一个工作表后,“D”列中的公式应显示 像下面这样 "=IF([@[Time Out]]="","",([Time Out]-[Time In])*24)" 但其显示如下 "=IF(TTM_Form.xlsm!Table2[@[Time Out]]="","",(TTM_Form.xlsm!Table2[Time Out]-TTM_Form.xlsm!Table2[Time In])*24)" 我没有得到正确的结果…你能告诉我如何避

我对工作簿“data_base”有一个小问题……将数据复制到另一个工作表后,“D”列中的公式应显示

像下面这样

"=IF([@[Time Out]]="","",([Time Out]-[Time In])*24)"
但其显示如下

"=IF(TTM_Form.xlsm!Table2[@[Time Out]]="","",(TTM_Form.xlsm!Table2[Time Out]-TTM_Form.xlsm!Table2[Time In])*24)"
我没有得到正确的结果…你能告诉我如何避免吗。 下面的代码行

.SpecialCells(xlCellTypeVisible).Copy Destination:=Destination.Range("A1")

您可以尝试以下方法:

Destination.Range("A1").Formula = Origin.Range("F1").Formula

如果公式中有表名和列名,那么在复制/粘贴后,自然会有相同的表名和列名

假设您的表布局如下所示:

1-您可以手动将公式修改为
=IF(E2=“”,“(E2-F2)*24)
,然后使用您的代码进行复制/粘贴

2-或者您可以让代码为您完成:

Sub cpy()
Sheets("Sheet2").Range("D2").Formula = str(Sheets("Sheet1").Range("D2"))
End Sub

Function str(rng As Range) As String
Dim brakets As Object, regEx As Object
Dim colname As String, colletter As String
Dim i As Long, colno As Long, rowno As Long
Set regEx = CreateObject("vbscript.regexp")

str = rng.Formula
With regEx
    .Global = True
    .Pattern = "\[(.*?)\]"
    Set brakets = .Execute(str)
    For i = 0 To brakets.count - 1
        colname = Replace(Replace(Replace(brakets(i).submatches(0), "@", ""), "[", ""), "]", "")
        colno = Application.WorksheetFunction.Match(colname, Sheets(rng.Parent.Name).Rows(1), 0)
        colletter = Split(Cells(1, colno).Address(True, False), "$")(0)
        rowno = rng.Row
        str = Replace(str, brakets(i), colletter & rowno)
    Next i
End With
str = Replace(Replace(Replace(str, "@", ""), "[", ""), "]", "")
End Function

它不工作,Techscript我无法理解您的代码,它将如何解决问题Dim wbSource作为工作簿,wbDestination作为工作簿Dim startDate作为日期,Enddate作为日期,dtTodayDate作为字符串Dim Source作为工作表,目标为工作表。这就是变量的声明方式,我认为这应该在另一个答案下面,对吗?