打开两个excel,excel1包含所需的宏文件,excel2是需要执行宏的文件
import win32com.client
def useVBA(VBA):
xlApp = win32com.client.DispatchEx("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = 0
# 宏所在的xls
xlApp.Workbooks.Open(r"D:\code\cq macro\combine2.xlsm", False)
#需要执行宏的xls
xlBook = xlApp.Workbooks.Open(file_path,False)
xlBook.Application.Run(VBA) #宏
xlBook.Close(True)
xlApp.quit()
file_path = r"D:\code\cq macro\combine.xlsm"
# 宏的文件名!模块名.函数名
useVBA(r"combine2.xlsm!module1.mac")
Sub del_mn()
'
' del_mn
'
'
Columns("M:N").Select
Selection.Delete Shift:=xlToLeft
End Sub
Sub cancel_hide_dj()
'取消d-j隐藏,删除空列
Columns("D:J").Select
Selection.EntireColumn.Hidden = False
Range("E:E,F:F,H:H,J:J").Select
Selection.Delete Shift:=xlToLeft
End Sub
Sub 12()
'
' 取消1 2 行隐藏
'
'
Cells.Select
Selection.EntireRow.Hidden = False
End Sub
Sub del_ecolumn_space()
'
' del_ecolumn_space
'
'
Columns("E:E").Select
Selection.AutoFilter
ActiveSheet.Range("$E$1:$E$393").AutoFilter Field:=1, Criteria1:="="
Cells.Select
Range("E1").Activate
Selection.Delete Shift:=xlUp
End Sub
主程序
Sub MRPFomart()
Application.ScreenUpdating = False
'del m n column
Call del_mn
' main function
If Cells(3, 2) = "L/T:" Then
Call MRPFomart_AHUE
Else
If Cells(6, 2) = "PO ETA" Then
Call MRPFomart1
Else
Call ForecastFormat
End If
End If
'unhide d-j column and delete null column
Call cancel_hide_dj
'unhide 1 2 row
Call unhide12
'del e column null cell row
Call del_ecolumn_space
Application.ScreenUpdating = True
End Sub
Sub ForecastFormat()
'
' Macro1 エ栋
' Administrator 2006/6/9 魁籹エ栋
'
Dim i, rowcount, colcount As Integer
Dim j As Integer
Dim k As Integer
Dim i_count As Integer
Dim str1, str2, str3 As String
Dim bl As Boolean
bl = True
i = 1
If Cells(255, 255) = "1" Then
MsgBox "硂Forecastゅン竒Αて筁叫ぃ璶Αて", vbInformation, "System"
Exit Sub
End If
i_count = 1
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
While rowcount < 3
Rows(Str(i)).Select
With Selection.Font
.Name = "Times New Roman"
.Size = 10
End With
str1 = "D" + Trim(Str(i))
If Range(str1).FormulaR1C1 = "P/N:" Or _
Range(str1).FormulaR1C1 = "" Or _
Range(str1).FormulaR1C1 = "Action day" Then
With Selection.Font
.Bold = True
.ColorIndex = 16
End With
End If
If Range(str1).FormulaR1C1 = "P/N:" Or _
Range(str1).FormulaR1C1 = "Demand" Then
Rows(Str(i)).Font.ColorIndex = 5
End If
If Range(str1).FormulaR1C1 = "P/N:" Then
Cells(i + 3, 1) = i_count
i_count = i_count + 1
Cells(i + 3, 2) = Cells(i, 5)
Cells(i + 3, 3) = Cells(i, 7)
Cells(i + 1, 2) = Cells(i, 9)
str3 = "A" + Trim(Str(i + 2)) + ":" + "A" + Trim(Str(i + 3))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
Selection.Font.ColorIndex = 5
str3 = "B" + Trim(Str(i + 2)) + ":" + "B" + Trim(Str(i + 3))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
str3 = "C" + Trim(Str(i + 2)) + ":" + "C" + Trim(Str(i + 3))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
Rows(i).EntireRow.Hidden = True
End If
colcount = 0
j = 1
If Range(str1).FormulaR1C1 = "Demand Qty" Then
While colcount < 10
If Cells(i, j) <> "0" Then
Cells(i, j).Font.ColorIndex = 3
End If
If Strings.InStr(1, Cells(i + 1, j), "-", vbTextCompare) > 0 Then
Cells(i + 1, j).Font.ColorIndex = 3
End If
If Cells(i, j).Value = "" Then
colcount = colcount + 1
End If
If j > 8 Then
If Cells(i, j) = "" Then
If Columns(j).EntireColumn.Hidden = False Then
Columns(j).EntireColumn.Hidden = True
End If
End If
End If
j = j + 1
Wend
End If
If Range(str1).FormulaR1C1 = "" Then
rowcount = rowcount + 1
Else
rowcount = 0
End If
j = 1
colcount = 1
While colcount < 9
If Cells(i, j) = "" Then
colcount = colcount + 1
Else
colcount = 10
End If
If colcount = 8 Then
Rows(i).EntireRow.Hidden = True
End If
j = j + 1
Wend
i = i + 1
Wend
k = 6
Dim cellstr As String
While bl = True
If Cells(3, k) = "" And k > 15 Then
bl = False
stre = Cells(3, k)
' cellstr = Cells(3, k - 1).Address
End If
k = k + 1
Wend
cellstr = Cells(i - 2, k - 2).Address
str1 = "F1:" + cellstr
Range(str1).Select
With Selection
.HorizontalAlignment = xlCenter
.NumberFormatLocal = "0"
End With
Columns("B:AS").EntireColumn.AutoFit
str1 = "A1:" + cellstr
Range(str1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
Columns(5).EntireColumn.Hidden = True
Columns(6).EntireColumn.Hidden = True
Columns(7).EntireColumn.Hidden = True
'Columns(8).EntireColumn.Hidden = True
Columns(9).EntireColumn.Hidden = True
Columns(2).Font.Bold = True
Columns(3).Font.Bold = True
Columns(2).Font.ColorIndex = 5
Columns(3).Font.ColorIndex = 5
Cells(255, 255) = 1
End Sub
Sub MRPFomart1()
'
' Macro1 エ栋
' Administrator 2006/6/9 魁籹エ栋
'
'
Dim i, rowcount, colcount As Integer
Dim j As Integer
Dim k As Integer
Dim i_count As Integer
Dim str1, str2, str3 As String
Dim bl As Boolean
bl = True
i = 1
If Cells(255, 255) = "1" Then
MsgBox "硂MRPゅン竒Αて筁叫ぃ璶Αて", vbInformation, "System"
Exit Sub
End If
i_count = 1
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
While rowcount < 3
Rows(Str(i)).Select
With Selection.Font
.Name = "Times New Roman"
.Size = 10
End With
str1 = "D" + Trim(Str(i))
If Range(str1).FormulaR1C1 = "P/N:" Or _
Range(str1).FormulaR1C1 = "" Or _
Range(str1).FormulaR1C1 = "Action day" Then
With Selection.Font
.Bold = True
.ColorIndex = 16
End With
End If
If Range(str1).FormulaR1C1 = "P/N:" Or _
Range(str1).FormulaR1C1 = "Demand" Then
Rows(Str(i)).Font.ColorIndex = 5
End If
If Range(str1).FormulaR1C1 = "P/N:" Then
Cells(i + 5, 1) = i_count
i_count = i_count + 1
Cells(i + 5, 2) = Cells(i, 5)
Cells(i + 5, 3) = Cells(i, 6)
Cells(i + 2, 2) = Cells(i, 8)
Cells(i + 2, 3) = Cells(i, 10)
Cells(i, 10) = ""
str3 = "A" + Trim(Str(i + 3)) + ":" + "A" + Trim(Str(i + 6))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
Selection.Font.ColorIndex = 5
str3 = "B" + Trim(Str(i + 3)) + ":" + "B" + Trim(Str(i + 6))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
str3 = "C" + Trim(Str(i + 3)) + ":" + "C" + Trim(Str(i + 6))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
Rows(i).EntireRow.Hidden = True
End If
colcount = 0
j = 1
If Range(str1).FormulaR1C1 = "Suggest" Then
While colcount < 10
If Cells(i, j) <> "0" Then
Cells(i, j).Font.ColorIndex = 3
End If
If Strings.InStr(1, Cells(i + 1, j), "-", vbTextCompare) > 0 Then
Cells(i + 1, j).Font.ColorIndex = 3
End If
If Cells(i, j).Value = "" Then
colcount = colcount + 1
End If
j = j + 1
Wend
End If
If Range(str1).FormulaR1C1 = "" Then
rowcount = rowcount + 1
Else
rowcount = 0
End If
j = 1
colcount = 1
While colcount < 9
If Cells(i, j) = "" Then
colcount = colcount + 1
Else
colcount = 10
End If
If colcount = 8 Then
Rows(i).EntireRow.Hidden = True
End If
j = j + 1
Wend
i = i + 1
Wend
k = 6
Dim cellstr As String
While bl = True
If Cells(3, k) = "" And k > 15 Then
bl = False
stre = Cells(3, k)
' cellstr = Cells(3, k - 1).Address
End If
k = k + 1
Wend
cellstr = Cells(i - 2, k - 2).Address
str1 = "F1:" + cellstr
Range(str1).Select
With Selection
.HorizontalAlignment = xlCenter
.NumberFormatLocal = "0"
End With
Columns("B:AS").EntireColumn.AutoFit
str1 = "A1:" + cellstr
Range(str1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
Columns(5).EntireColumn.Hidden = True
Columns(6).EntireColumn.Hidden = True
Columns(8).EntireColumn.Hidden = True
Columns(2).Font.Bold = True
Columns(3).Font.Bold = True
Columns(2).Font.ColorIndex = 5
Columns(3).Font.ColorIndex = 5
Cells(255, 255) = 1
End Sub
Sub MRPFomart_AHUE()
'
' Macro1 エ栋
' Administrator 2006/6/9 魁籹エ栋
'
'
Dim i, rowcount, colcount As Integer
Dim j As Integer
Dim k As Integer
Dim i_count As Integer
Dim str1, str2, str3 As String
Dim bl As Boolean
bl = True
i = 1
If Cells(255, 255) = "1" Then
MsgBox "硂MRPゅン竒Αて筁叫ぃ璶Αて", vbInformation, "System"
Exit Sub
End If
i_count = 1
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Selection.Insert Shift:=xlToRight
While rowcount < 3
Rows(Str(i)).Select
With Selection.Font
.Name = "Times New Roman"
.Size = 10
End With
str1 = "D" + Trim(Str(i))
If Range(str1).FormulaR1C1 = "P/N:" Or _
Range(str1).FormulaR1C1 = "" Or _
Range(str1).FormulaR1C1 = "Action day" Then
With Selection.Font
.Bold = True
.ColorIndex = 16
End With
End If
If Range(str1).FormulaR1C1 = "P/N:" Or _
Range(str1).FormulaR1C1 = "Demand" Then
Rows(Str(i)).Font.ColorIndex = 5
End If
If Range(str1).FormulaR1C1 = "L/T:" Then
Cells(i + 1, 2) = "L/T:" & Cells(i, 5)
Cells(i + 1, 3) = "MOQ:" & Cells(i, 6)
Rows(i).EntireRow.Hidden = True
End If
If Range(str1).FormulaR1C1 = "P/N:" Then
Cells(i + 6, 1) = i_count
i_count = i_count + 1
Cells(i + 6, 2) = Cells(i, 5)
Cells(i + 6, 3) = Cells(i, 6)
Cells(i + 3, 2) = Cells(i, 8)
Cells(i + 3, 3) = Cells(i, 10)
Cells(i, 10) = ""
str3 = "A" + Trim(Str(i + 4)) + ":" + "A" + Trim(Str(i + 7))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
Selection.Font.ColorIndex = 5
str3 = "B" + Trim(Str(i + 4)) + ":" + "B" + Trim(Str(i + 7))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
str3 = "C" + Trim(Str(i + 4)) + ":" + "C" + Trim(Str(i + 7))
Range(str3).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
Rows(i).EntireRow.Hidden = True
End If
colcount = 0
j = 1
If Range(str1).FormulaR1C1 = "Suggest" Then
While colcount < 10
If Cells(i, j) <> "0" Then
Cells(i, j).Font.ColorIndex = 3
End If
If Strings.InStr(1, Cells(i + 1, j), "-", vbTextCompare) > 0 Then
Cells(i + 1, j).Font.ColorIndex = 3
End If
If Cells(i, j).Value = "" Then
colcount = colcount + 1
End If
If j > 8 Then
If Cells(i, j) = "" Then
If Columns(j).EntireColumn.Hidden = False Then
Columns(j).EntireColumn.Hidden = True
End If
End If
End If
j = j + 1
Wend
End If
If Range(str1).FormulaR1C1 = "" Then
rowcount = rowcount + 1
Else
rowcount = 0
End If
j = 1
colcount = 1
While colcount < 9
If Cells(i, j) = "" Then
colcount = colcount + 1
Else
colcount = 10
End If
If colcount = 8 Then
Rows(i).EntireRow.Hidden = True
End If
j = j + 1
Wend
i = i + 1
Wend
k = 6
Dim cellstr As String
While bl = True
If Cells(4, k) = "" And k > 20 Then
bl = False
stre = Cells(4, k)
' cellstr = Cells(3, k - 1).Address
End If
k = k + 1
Wend
cellstr = Cells(i - 2, k - 2).Address
str1 = "F1:" + cellstr
Range(str1).Select
With Selection
.HorizontalAlignment = xlCenter
.NumberFormatLocal = "0"
End With
Columns("B:AS").EntireColumn.AutoFit
str1 = "A1:" + cellstr
Range(str1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 16
End With
Columns(5).EntireColumn.Hidden = True
Columns(6).EntireColumn.Hidden = True
Columns(8).EntireColumn.Hidden = True
Columns(2).Font.Bold = True
Columns(3).Font.Bold = True
Columns(2).Font.ColorIndex = 5
Columns(3).Font.ColorIndex = 5
Cells(255, 255) = 1
End Sub
' Columns("A:A").Select
' Range("A2").Activate
'' Selection.Insert Shift:=xlToRight
' Range("A3:A7").Select
' With Selection
' .HorizontalAlignment = xlGeneral
' .VerticalAlignment = xlBottom
' .WrapText = False
' .Orientation = 0
' .AddIndent = False
' .ShrinkToFit = False
' .MergeCells = True
' End With
' Selection.Delete Shift:=xlUp
' Columns("A:A").Select
' Selection.Delete Shift:=xlToLeft
' Rows("1:1").Select
' Selection.Font.ColorIndex = 5
Sub del_mn()
'
' del_mn
'
'
Columns("M:N").Select
Selection.Delete Shift:=xlToLeft
End Sub
Sub cancel_hide_dj()
'unhide_dj_and_delete
Columns("D:J").Select
Selection.EntireColumn.Hidden = False
Range("E:E,F:F,H:H,J:J").Select
Selection.Delete Shift:=xlToLeft
End Sub
Sub unhide12()
'
' 取消1 2 行隐藏
'
'
Cells.Select
Selection.EntireRow.Hidden = False
End Sub
Sub del_ecolumn_space()
'
' del_ecolumn_space
'
'
Columns("E:E").Select
Selection.AutoFilter
ActiveSheet.Range("$E$1:$E$393").AutoFilter Field:=1, Criteria1:="="
Cells.Select
Range("E1").Activate
Selection.Delete Shift:=xlUp
End Sub