Excel:从CSV导入后的名称表 - 运行到问题

时间:2013-12-17 15:41:00

标签: excel vba excel-vba ms-office

我一直在为一些报告功能编写工作簿。

我刚刚回到VB并且有点生疏 - 在我面前还有一本1000多页的书,并且已经遍布这样的网站。似乎我可以接近 - 但最终结束了一点点。

我每周都会有新数据进入,我将用它来更新一些数据透视表等等。我有5个区域可以导入数据并且当前具有导入功能,但是我遇到了两个方面的问题:我似乎无法创建一个刚刚导入的数据表。我没有错误检查。我需要这样命名的表,所以我可以在整个工作表中使用日期。

任何指导对以下内容都有好处:

  • 如何简单地清除A2处的当前表并导入新数据,而不必重命名表和标题? (我导入的数据以“First Name”而不是First_Name形式出现“所以最好保持这种格式。

  • 如何在此处添加错误检查,以确定是否有人运行它并关闭窗口,它不会清除工作表并启动调试器?

这是我的工作(我尝试过很多东西,但回到这里)

Sub ImportMaster() 
' this is the master list of all NA accounts for ..... 
' NA_ACCOUNTS_LIST Worksheet 
Dim ws As Worksheet, strFile As String Set ws = ActiveWorkbook.Sheets("NA_Accounts_List") ws.UsedRange.Clear strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please selec text file...") With ws.QueryTables.Add(Connection:="TEXT;" & strFile, _ Destination:=ws.Range("A2"))
        .Name = "ProgramData"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 2
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False End With '  ws.Name = "testing"

    ' updates the date Range("C12").ClearContents Range("C12") = Format(Date, "mm-dd-yyyy")


    ' This will focus this worksheet after upate Sheets("NA_Accounts_List").Visible = xlSheetVisible Sheets("NA_Accounts_List").Select


    MsgBox "Imported data successfully!"

end sub

2 个答案:

答案 0 :(得分:0)

让我看看我是否理解,你想要的是从你当前工作的表中清除A2的单元格?并在同一个单元格中插入新数据?

答案 1 :(得分:0)

你很幸运,我想它应该解决。

        Microsoft.Office.Interop.Excel.Application ex = new Microsoft.Office.Interop.Excel.Application();
        ex.Visible = true;
        Microsoft.Office.Interop.Excel.Workbook wk = ex.Workbooks.Add(Type.Missing);
        Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wk.Worksheets[1]; // set the first worksheet
        DataTable dtb = mysql.mysql_data_adapter(); // the data that are coming from mysql
        DataRow row;

        for (int i = 0; i < dtb.Columns.Count; i++) // add header
        {
            Microsoft.Office.Interop.Excel.Range rg = (ws.Cells[1, i + 1] as Microsoft.Office.Interop.Excel.Range); // excel start in 1
            rg.Value2 = dtb.Columns[i].ColumnName.ToString();
        }

        for (int i = 2; i < dtb.Rows.Count + 2; i++) // add data without change header, and this for run the row
        {
            row = dtb.Rows[i - 2]; //  where to start at correct row, dont oforget, start in 2 because 1 are the header
            for (int i2 = 1; i2 < dtb.Columns.Count + 1; i2++) // where run the collumns
            {
                try
                {
                    Microsoft.Office.Interop.Excel.Range rg = (ws.Cells[i, i2] as Microsoft.Office.Interop.Excel.Range); 
                    rg.Value2 = row[i2 - 1].ToString();
                }
                catch
                {
                }
            }
        }