简体   繁体   English

导出 header 和每两行由宏从 Excel 到 csv 的问题

[英]Issue with export header and each two rows by macro from Excel to csv

I want to export data from master Excel to CSV files.我想将数据从主 Excel 导出到 CSV 文件。

在此处输入图像描述

As you can see, first row is header, and every two rows will be copied into new CSV file.如您所见,第一行是 header,每两行将复制到新的 CSV 文件中。 The CSV file is named by City (from row without diacritics) and by predefined words. CSV 文件由 City(从没有变音符号的行)和预定义的单词命名。 Here is my macro:这是我的宏:

    Sub Macro1()

Dim newWB As Workbook
Dim myWS As Worksheet
Dim lastRow As Long

Set myWS = ThisWorkbook.Worksheets("ExportSamospravy")

With myWS

    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    For x = 2 To lastRow
    
        Set newWB = Workbooks.Add

        newWB.Sheets("Hárok1").Rows(1) = myWS.Rows(1).Value
        newWB.Sheets("Hárok1").Rows(2) = myWS.Rows(x).Value
        newWB.Sheets("Hárok1").Rows(3) = myWS.Rows(x).Value

        
        newWB.SaveAs Filename:="C:\Users\andre\OneDrive\prispevky\smart-cities\data\1-2021\Export\EconomySamospravy\" & Range("I2").Value & "EconomySamospravy", FileFormat:=xlCSV

        Columns(9).EntireColumn.Delete
        Columns(9).EntireColumn.Delete

        newWB.Close True
        
    Next x
    
End With

End Sub

My issue is that only row 2019 is multiplied into second row.我的问题是只有第 2019 行乘以第二行。 Also it has issue with saving.还有保存的问题。

I found solution我找到了解决方案

    For x = 2 To lastRow Step 2
n = x + 1
    Set newWB = Workbooks.Add

Now it exports perfectly!现在它完美导出!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM