-2

I am trying to create some VBA code to do the following:

  1. A specific folder contains multiple excel files and each tab has the name of a (let's say) city.

  2. An excel list defines to which country a city belongs. (column A = London, Paris, Boston, New-York and column B = U-K, France, U.S, U.S)

  3. I would like a code looping through the folder and creating an additional file per country regrouping all the cities (there is only one tab per city but there are multiple files)

So far, nothing I tried seems to work.

4

1 回答 1

0

这个问题非常广泛,但这个基本逻辑可能会有所帮助:

使用这篇文章来确定如何循环遍历目录: 指定要循环遍历 Excel/VBA 的附加目录

此代码模板应该足以让您入门。使用它,改进您的问题,我们也许可以为您提供进一步的帮助。

Option Explicit

Sub CopySomeSheets()

    ' Loop through directory as in Question of this post
    Dim wb As Workbook
    Dim ws As Worksheet

    Dim cityNameList() As Variant
    Dim cityName As Variant


    ' Pseudocode, next line is a dummy to make example code run
    Set wb = ActiveWorkbook
    ' For Each wb In directory, as from post above
        For Each ws In wb.Worksheets
            For Each cityName In cityNameList

                ' Could do this as a loop for each country or think a bit more and produce a smarter iteration with range lookup/2d array or somethnig to better match the shape of your data
                cityNameList = Range("A1:A10") ' List of cities are in this range
                If InStr(ws.Name, cityName) Then
                    ' Do stuff, i.e. save workbook with required name
                End If

            Next cityName
        Next ws
    ' Next wb




End Sub
于 2013-02-24T15:03:40.243 回答