如何在一列中合并公式和数值? (VBA)

时间:2018-06-06 15:57:04

标签: excel vba

1800行(员工数据)

我在列AT中有数字值,并且一个IF语句填充了列AU中的单元格(其他单元格中的公式被视为空白但仍包含公式)。

我需要他们像这样合并:

000000

公式(未转换,仍为语法)

000000

000000

000000

000000

这可以实现吗?这是我的代码,通过我底部的For / Next语句吐出的数字值就像这样

00000false 或

false00000

基于哪个与之合并。

代码:

Sub Update()


'File Paths
Dim Preplan As String
Dim PS_Export As String
Preplan = "M:\Template2.xlsm"
PS_Export = "M:\PS_Export.xlsx"

'Open WB's
Dim PP_WB As Workbook
Dim PS_WB As Workbook
Set PP_WB = Workbooks.Open(Filename:=Preplan, Password:="")
Set PS_WB = Workbooks.Open(Filename:=PS_Export)

Dim PP_WS As Worksheet
Set PP_WS = PP_WB.Sheets("2017 Pre-Planning Emp Detail")

Dim PS_WS As Worksheet
Set PS_WS = PS_WB.Sheets("ps")

Dim AVCell As Long



lastrow = PP_WS.Range("A" & Rows.Count).End(xlUp).Row
lastrow2 = PS_WS.Range("A" & Rows.Count).End(xlUp).Row

Application.ScreenUpdating = False


PP_WB.Activate

With PP_WS


With .Range("AE2")
.Formula = "=VLOOKUP(A2,[PS_Export.xlsx]ps!$A:$K,11,FALSE)"
.AutoFill Destination:=Range("AE2:AE" & lastrow)
End With

With .Range("AF2")
.Formula = "=VLOOKUP(A2,[PS_Export.xlsx]ps!$A:$H,8,FALSE)"
.AutoFill Destination:=Range("AF2:AF" & lastrow)
End With

With .Range("AG2")
.Formula = "=VLOOKUP(A2,[PS_Export.xlsx]ps!$A:$AY,50,FALSE)"
.AutoFill Destination:=Range("AG2:AG" & lastrow)
End With

With .Range("AH2")
.Formula = "=VLOOKUP(A2,[PS_Export.xlsx]ps!$A:$O,15,FALSE)"
.AutoFill Destination:=Range("AH2:AH" & lastrow)
End With

With .Range("AI2")
.Formula = "=VLOOKUP(A2,[PS_Export.xlsx]ps!$A:$P,16,FALSE)"
.AutoFill Destination:=Range("AI2:AI" & lastrow)
End With

End With

PS_WB.Activate

With PS_WS
.Columns("AH:AH").Insert Shift:=xlToRight



With .Range("AH2")
.Formula = "=AD2+AG2"
.AutoFill Destination:=Range("AH2:AH" & lastrow2)
.Range("AH1") = "Variable Comp"
End With


End With

PS_WB.Close
ThisWorkbook.Saved = True

PP_WB.Activate


With PP_WS

With .Range("AR2")
.Formula = "=IF(F2=""X"",VLOOKUP(A2,[PS_Export.xlsx]ps!$A:$AH,34,FALSE),(AS2+AU2+AX2))"
.AutoFill Destination:=Range("AR2:AR" & lastrow)
End With

With .Range("AS2")
.Formula = "=IF(F2="""",VLOOKUP(A2,[PS_Export.xlsx]ps!$A:$AD,30,FALSE),(AR2-AX2))"
.AutoFill Destination:=Range("AS2:AS" & lastrow)
End With

Range("AT:AV").Insert Shift:=xlToRight



.Range("AT2").Formula = "=IF(F2="""",VLOOKUP(A2,[PS_Export.xlsx]ps!$A:$AD,30,FALSE))"
.Range("AT2").AutoFill Destination:=.Range("AT2:AT" & lastrow)
.Range("AT:AT").Copy
.Range("AT:AT").PasteSpecial xlPasteValues
.Range("AU2").Formula = "=IF(F2=""X"",AR2-AX2)"
.Range("AU2").AutoFill Destination:=.Range("AU2:AU" & lastrow)
.Range("AV1") = "2017 Planned Annual IC"



For AVCell = 2 To lastrow
.Range("AV" & AVCell).Value = .Range("AT" & AVCell).Value & .Range("AU" & AVCell).Value

Next AVCell





End With













End Sub

0 个答案:

没有答案