允许用户选择csv的VBA不起作用

时间:2018-08-13 18:25:54

标签: excel vba excel-vba

我想修改以下代码,以便发生两件事:

1)用户可以在文件夹中选择他们想要的csv 2)仅保留第一个csv的标头,并保留其余csvs的正文

在下面的代码中我将如何处理?运行此代码时,我目前始终收到错误消息。

async function getProducts(myUrl) {
  try {
    let res = await axios.get(myUrl, {
      httpsAgent: agent
    });
    res.data.products.forEach(product => {
      var product = {
        'values': {
          '2': product.name,
          '3': product.display_price,
          '4': product.slug
        }
      };

      await postProduct(product);

    });

  } catch (e) {
    console.log(e);
  }
}

async function postProduct(product) {
  try {
    let res = await axios.post(huburl, product);
    console.log(res.data);

  } catch (e) {
    console.log(e);
  }
}

for (let i = 1; i <= 686; i++) {
  var pagedUrl = url + '&page=' + i;
  console.log(pagedUrl);
  await getProducts(pagedUrl);
}

1 个答案:

答案 0 :(得分:2)

这对您来说是个入门。

它可以毫无错误地抓取文件,然后您就可以执行想要的操作。

Sub ImportCSVsWithReference()

    Dim xSht  As Worksheet
    Dim xWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String

    Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
    xFileDialog.AllowMultiSelect = True
    xFileDialog.Title = "Select a folder [CSV Consolidation]"

    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If

    If xStrPath = "" Then Exit Sub

    Set xSht = ThisWorkbook.ActiveSheet
    If MsgBox("Clear the existing sheet before importing?", vbYesNo) = vbYes Then xSht.UsedRange.Clear
    Application.ScreenUpdating = False

    Dim vrtSelectedItem As Variant

     Set xWb = Workbooks.Open(xStrPath)
     MsgBox "Opened " & xStrPath & " for headers"

     'Do your work with headers here with xWb as workbook with code
     xWb.Close False


    For Each vrtSelectedItem In xFileDialog.SelectedItems

        Set xWb = Workbooks.Open(vrtSelectedItem)
        MsgBox "Opened " & vrtSelectedItem & " for content"
        'Do your work with content here with xWb as workbook with code
        xWb.Close False
    Next

    Application.ScreenUpdating = True



End Sub