运行时错误:1004:在MAC上保存为工作表类的方法失败

时间:2017-04-21 12:17:35

标签: excel vba macos excel-vba excel-vba-mac

这是MAC特定的问题,在WindowsOS上,它运行得非常好。

在这个宏中,我从某些单元格中获取值(来自Sheet 1)&然后将此连接文本复制到此电子表格的第3页。

在使用Excel 2016的MAC上执行代码时,我收到错误

  

运行时错误1004:保存Worsheet类的方法失败。

错误代码如下: Worksheets("Sheet3").SaveAs Filename:=path1 & "\" & Worksheets("Sheet1").Cells(28, 2).Value & ".csv", FileFormat:=xlCSV, CreateBackup:=False

完整代码如下:

Sub Generate()
ThisWorkbook.Worksheets("Sheet1").Unprotect ("laces")

Dim sOutputPath As String
Dim val2 As Variant
Dim lastrow As Long

path1 = ActiveWorkbook.Path

Set oExcel = CreateObject("Excel.Application")
Set Osheet = ActiveWorkbook.Worksheets("Sheet1")

sVal1 = Osheet.Cells(37, 2).Value & Osheet.Cells(37, 3).Value & Osheet.Cells(37, 4).Value & ";" 'Reference Mapping
sVal1 = sVal1 & Osheet.Cells(2, 9).Value & ";" 'Date Mapping
sVal1 = sVal1 & Osheet.Cells(29, 2).Value & Osheet.Cells(29, 3).Value & Osheet.Cells(29, 4).Value & ";"  'Name Mapping
sVal1 = sVal1 & Osheet.Cells(28, 2).Value & Osheet.Cells(28, 3).Value & Osheet.Cells(28, 4).Value & ";"   'Company Mapping
sVal1 = sVal1 & Osheet.Cells(30, 2).Value & Osheet.Cells(30, 3).Value & Osheet.Cells(30, 4).Value & ";"   'Street1 Mapping
sVal1 = sVal1 & "a" & ";"    'Street2 Mapping
sVal1 = sVal1 & Osheet.Cells(31, 2).Value & Osheet.Cells(31, 3).Value & Osheet.Cells(31, 4).Value & ";"   'Zip Code Mapping
sVal1 = sVal1 & Osheet.Cells(32, 2).Value & Osheet.Cells(32, 3).Value & Osheet.Cells(32, 4).Value & ";"   'City Mapping
sVal1 = sVal1 & "" & ";"    'Region Mapping
sVal1 = sVal1 & Osheet.Cells(33, 2).Value & Osheet.Cells(33, 3).Value & Osheet.Cells(33, 4).Value & ";"   'Country Mapping
sVal1 = sVal1 & Osheet.Cells(34, 2).Value & Osheet.Cells(34, 3).Value & Osheet.Cells(34, 4).Value & ";"   'Email Mapping
sVal1 = sVal1 & Osheet.Cells(35, 2).Value & Osheet.Cells(35, 3).Value & Osheet.Cells(35, 4).Value & ";"   'Phone Mapping

hData1 = "Reference;Date;Name;Company;Street1;Street2;Zip Code;City;Region;Country;Email;Phone;Product;SKU;Quantity"

Worksheets("Sheet3").Range("A1").Value = hData1
hData1 = ""

'Product & Quality Mapping
For i = 6 To 13

 S = Osheet.Cells(i, 6).Value
 M = Osheet.Cells(i, 7).Value
 L = Osheet.Cells(i, 8).Value
 XL = Osheet.Cells(i, 9).Value

  If (S = "" And M = "" And L = "" And XL = "") Then

  Else

' SKU Mapping

SKU = Osheet.Cells(i, 1).Value & " - " & Osheet.Cells(i, 2).Value

If S <> "" Then
expKey = Osheet.Cells(i, 2).Value & "S"
sQuantity = Osheet.Cells(i, 6)


val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 6).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 6).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""

lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row

Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1


rData1 = ""
End If

If M <> "" Then
expKey = Osheet.Cells(i, 2).Value & "M"
sQuantity = Osheet.Cells(i, 7)
val4 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 7).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 7).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val4 & ";" & sQuantity
val4 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If


If L <> "" Then
expKey = Osheet.Cells(i, 2).Value & "L"
sQuantity = Osheet.Cells(i, 8)
val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 8).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 8).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1

rData1 = ""
End If

If XL <> "" Then
expKey = Osheet.Cells(i, 2).Value & "XL"
sQuantity = Osheet.Cells(i, 9)
val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 9).Value, 2))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 9).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1

rData1 = ""
End If

End If

Next

Worksheets("Sheet3").SaveAs Filename:=path1 & "\" & Worksheets("Sheet1").Cells(28, 2).Value & ".csv", _
        FileFormat:=xlCSV, CreateBackup:=False


MsgBox "You can find the generated  CSV file in " & path1

Sheet3.Cells.Clear

ActiveWorkbook.Close savechanges:=False
Set oExcel = Nothing


ThisWorkbook.Worksheets("Sheet1").Protect ("laces")
End Sub


Function findSKU(farb As String, size As String) As String

Dim ri As Long
    For ri = 2 To 33
        If Sheet2.Cells(ri, 1).Value = farb And Sheet2.Cells(ri, 2).Value = size Then
        findSKU = Sheet2.Cells(ri, 3).Value
        Exit Function
        End If

        Next ri

End Function

任何帮助都将受到高度赞赏。

非常感谢。

0 个答案:

没有答案