excel vba |从具有特定名称的模板创建新列表并将其保存在.csv中

时间:2017-10-31 02:18:35

标签: excel vba

Hiyall!

我是excel macros和vba的新手,但愿意学习。最近我提出了一个想法,在主题中描述特定的宏。让我解释一下:

INPUT:

包含模板样式和列填充(数字和公式)的1个列表 2列表查找功能 3输出列表

PROCESS: -start循环 -for i到list2上的end_column 从list2创建名称= Ai的新列表 从list1复制列 在使用公式复制单元格后,将每个x替换为list2中的= Bi -save list csv

然而我发现只保存为.csv,虽然它在路径中存在错误,例如"不可能的路径"

Sub SplitSheets2()
    Dim s As Worksheet
    Dim wb as Workbook
    Set wb = ActiveWorkbook
    For Each s In wb.Worksheets
        s.Copy
        ActiveWorkbook.SaveAs wb.Path & "\" & s.Name & ".xlsx", FileFormat:=24
End Sub

我在哪里可以找到其他作品?以及如何使路径工作?

============= 14h编辑 我提出了以下代码,但它在评论中有错误和更多问题

Dim c As Range
For Each c In Sheets("reference").Range("A2:A4")
    Sheets.Add After:=ActiveSheet
    Sheets("List2").Name = "123" '123 to change onto =ref!R3A2b but have "out of range error"
    Sheets("temp").Select
    Range("A1:D3").Select
    Selection.Copy
    Sheets("123").Select 'how do I select =ref!R3C2 list againg w/o looking up its name on ref list?
    ActiveSheet.Paste
    Range("C2").Select
    Application.CutCopyMode = False 'dont know yet what does that mean, yet I was only changing formula
    ActiveCell.FormulaR1C1 = "=reference!R3C2+1"
    Selection.AutoFill Destination:=Range("C2:C3"), Type:=xlFillDefault 'idk also how Type:= appeared
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "=reference!R3C3*2"
    Selection.AutoFill Destination:=Range("D2:D3")
    Range("D2:D3").Select
End Sub

2 个答案:

答案 0 :(得分:0)

在执行所有需要重复的步骤(复制,粘贴,输入公式,保存等)时记录宏,然后修改为宏生成的VBA,添加循环将很简单。

一些提示&示例:

然后循环可以添加几行:

Dim c as Range
For Each c in Sheets("Sheet1").Range("A1:A10")

    ...(code to repeat here)
    ...(refer to list item as:  c.Value  )

Next c

编辑:

此代码循环遍历活动工作簿中的所有工作表,并将每个工作表“导出”为单独的`.CSV'文件,每个文件都以它来自的工作表命名。可以从JumpShare here下载有效的.xlsm示例。 (*在线查看器不适用于VBA。)

Sub MakeWorkbooksFromSheets()
'save each sheet to a new workbook (named after the sheet)

    Dim sht As Worksheet, this_WB As Workbook, new_WB As Workbook
    Dim savePath As String, saveFile As String
    Dim currentWB As String, copyCount As Integer
    Set this_WB = ActiveWorkbook 'create current-workbook object

    If this_WB.Path <> "" Then
        savePath = this_WB.Path  'output path will be same as current file's path...
    Else
        savePath = Application.DefaultFilePath '...unless current file isn't saved yet
    End If

    For Each sht In this_WB.Worksheets
        saveFile = sht.Name & ".csv"

        If Dir(savePath & "\" & saveFile) <> "" Then
            'skip this sheet (or you could change this to delete existing file instead)
            MsgBox "Skipping Sheet - File Already Exists: " & vbCrLf & savePath & "\" & saveFile
        Else
            sht.Copy 'create new workbook, activate it, and copy sht to it
            Set new_WB = ActiveWorkbook 'create output worksheet object

            new_WB.SaveAs Filename:=savePath & "\" & saveFile, FileFormat:=xlCSVUTF8 ' save new file as CSV, or instead you could...

            copyCount = copyCount + 1

            new_WB.Close savechanges:=True 'close new workbook (remove this line to keep it open)
            Set new_WB = Nothing 'free memory of new_workbook object
        End If

    Next
    Set this_WB = Nothing 'discard current-workbook object
    MsgBox copyCount & " sheets copied to new CSV's in folder:" & vbCrLf & savePath
End Sub

相关阅读:

答案 1 :(得分:0)

我最终得到了以下代码,它的确有效!谢谢你们!但我对循环有疑问;

Sub MarcoTemplate()
Dim c As Range
Dim n As String
For Each c In Sheets("ref").Range("A2:A3")
    n = c
    Vl = Application.WorksheetFunction.VLookup(n, Sheets("ref").Range("A2:D3"), 2, False)
    Worksheets.Add.Name = c
    ActiveSheet.Move After:=Sheets(ActiveWorkbook.Sheets.Count)
    Worksheets("temp").Range("A1:D3").Copy ActiveSheet.Range("A1")
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=ref!R2C2" + "+1"
    Selection.AutoFill Destination:=Range("C2:C3")
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "=ref!R2C2" + "*4"
    Selection.AutoFill Destination:=Range("D2:D3")
    Range("G2").Select
    ActiveCell.FormulaR1C1 = Vl
Next c
End Sub

如何在公式细胞之间进行转换?例如:

ActiveCell.FormulaR1C1 = "=ref!R2C2" + "+1"

在这里,我想将ref!R2C2更改为ref!R2C(c行号)或其他类似c + = 1为每个新c添加1行或1列

@ashleedawg @ YowE3K