我有一个代码,我正在努力更新一张工作表,该工作表根据一年中的几个月从另一个包含12列数据的信息中提取信息。正在更新并从另一个工作表中提取数据的工作表只需要当前和前几个月的信息。我知道这可以通过查找并将列字母替换为当前月份和前几个月列来完成,但我尝试编写VBA以便在打开时或使用命令按钮自动执行此操作。
我使用if函数检查当前显示的数据是哪个月,但是if功能是不是将它捡起来。
Sub Button1_Click()
'
' Button1_Click Macro
'
' Keyboard Shortcut: Ctrl+o
'
Dim current As String
current = Range("B8")
If current = "=+'Trend Balance Sheet'!$W$9" Then
Range("B:B").Value = Range("C:C").Value
Range("F:F").Value = Range("G:G").Value
Worksheets("Balance Sheet").Columns("B").Replace _
What:="$W", Replacement:="$L", _
SearchOrder:=xlByColumns, MatchCase:=True
Worksheets("Balance Sheet").Columns("F").Replace _
What:="$W", Replacement:="$L", _
SearchOrder:=xlByColumns, MatchCase:=True
Dim OriginalT As String
Dim CorrectedT As String
OriginalText = Range("B5")
CorrectedT = Replace(OriginalT, "12/31/15", "01/31/16")
Range("B5") = CorrectedT
ElseIf current = "=+'Trend Balance Sheet'!$L$9" Then
Range("B:B").Copy Range("C:C")
Range("F:F").Copy Range("G:G")
Dim OText As String
Dim CText As String
OText = Range("B5")
CText = Replace(OText, "01/31/16", "02/28/16")
Range("B5") = CText
Worksheets("Balance Sheet").Columns("B").Replace _
What:="L", Replacement:="M", _
SearchOrder:=xlByColumns, MatchCase:=True
Worksheets("Balance Sheet").Columns("F").Replace _
What:="L", Replacement:="M", _
SearchOrder:=xlByColumns, MatchCase:=True
End If
Application.Goto Reference:="Button1_Click"
End Sub