我刚开始使用VBA让我的生活更轻松,编程根本不是我的背景。当我运行代码时,我可能写得太多了。 所以我有两个问题,请查看下面的代码。
Sub Find() “ '找宏 “
” “L.NAM.O
Worksheets("LAC").Select
Cells.Select
Selection.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("NewForecast").Select
Range("K2").Select
ActiveSheet.Paste
'L.NAM.M
Worksheets("EMEA").Select
Cells.Select
Selection.Find(What:="forecast_quarter", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("NewForecast").Select
Range("K" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
我希望能够在两张表中找到forecast_quarter(我总共有3张)并粘贴在一张工作表(新预测)中,一张在另一张下面。问题是,我认为这太过分了,可能比重新运行所有过程更简单。
我的想法是,“在我想要的工作表中搜索forecast_quarter季度并粘贴在另一个下面。”因为我有这样做的所有标准,这可能是我的大量。任何更简单,更好的方式来运行它? 谢谢!
答案 0 :(得分:0)
这样的事情(未经测试)应该有效。
Sub CopyAll()
CopyDataByHeader "LAC", "forecast_quarter"
CopyDataByHeader "EMEA", "forecast_quarter"
End Sub
'Look for a specific header on a sheet, and if found copy
' the data below it to "NewForecast" sheet
Sub CopyDataByHeader(shtName As String, hdrText As String)
Dim f As Range
With ActiveWorkbook.Sheets(shtName)
'search for the header
Set f = .Cells.Find(What:=hdrText, After:=.Cells(1), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not f Is Nothing Then
'found the header: copy the data below it
.Range(f.Offset(1, 0), .Cells(.Rows.Count, f.Column).End(xlUp)).Copy _
ActiveWorkbook.Sheets("NewForecast").Cells( _
Rows.Count, "K").End(xlUp).Offset(1, 0)
Else
'header not found...
MsgBox "Header text '" & hdrText & "' not found on sheet '" & shtName & "' !"
End If
End With
End Sub