我在订单计算模板中创建了MACRO以删除一些不必要的行,并将工作表另存为“name”。 MACRO工作得很好,但是哪里有一个烦人的问题,我每次都根据订单号更改它的工作簿名称。因此,我想创建/改进我的MACRO以将工作表保存为具有单元名称的工作簿(此单元格范围为“G1”)。
有人可以提出如何做到这一点的想法吗?
Sub Pirmoji()
'
' Pirmoji Macro
Sheets("Svorio Patvirtinimo dok").Select
ActiveSheet.Shapes.Range(Array("Column1")).Select
Sheets("Svorio Patvirtinimo dok").Copy
Rows("1:6").Select
Selection.Delete Shift:=xlUp
ActiveWindow.SmallScroll Down:=66
Dim LastRow As Long, myCell As Range, myRange As Range
Dim myCell1 As Range
LastRow = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
Set myCell1 = Range("A" & LastRow)
Cells.Find(What:="• Praau atkreipti d?mes?:", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Set myCell = ActiveCell
Set myRange = Range(myCell, myCell1)
myRange.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-78
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveSheet.Copy
MsgBox "This new workbook will be saved as MyWb.xls(x)"
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\MyWb", xlWorkbookNormal
MsgBox "It is saved as " & ActiveWorkbook.FullName & vbLf & "Press OK to close it"
ActiveWorkbook.Close False
End Sub
答案 0 :(得分:2)
以下代码仅会修复帖子的相关部分,如何将"Svorio Patvirtinimo dok"
工作表另存为新工作簿,并根据" G1"中的值保存文件名。
您可以在代码的上半部分做很多改进,有很多不必要的Select
,Selection
和ActiveCell
。
阅读HERE为什么您应该避免使用Select
,Activate
和其他类似类型。
修改后的代码(仅限相关部分):
Dim Sht As Worksheet
Dim NewWBName As String
' set the worksheet object
Set Sht = ThisWorkbook.Sheets("Svorio Patvirtinimo dok")
MsgBox "This new workbook will be saved as MyWb.xls(x)"
' set the bnew name in same path and file name according to the value in "G1"
NewWBName = ThisWorkbook.Path & "\" & Sht.Range("G1").Value2 & ".xlsx"
'save sheet as workbook with the name in cell "G1"
Sht.SaveAs NewWBName, 51 ' save format 51 - .xlsx
MsgBox "It is saved as " & NewWBName & vbLf & "Press OK to close it"
ActiveWorkbook.Close False