0

関数生成()

Dim varLookup As Collection
Dim sheetOutput As Worksheet
Dim sheetVariables As Worksheet
Dim intRow As Integer
Dim intCol As Integer

intRow = 1
intCol = 1

Set sheetVariables = Worksheets("variables")

Dim rngCountries As Range
Set rngCountries = sheetVariables.Range("A2:A250")

Dim rngCities As Range
Set rngCities = sheetVariables.Range("D1:D4195")

Dim rngAirports As Range
Set rngAirports = sheetVariables.Range("C1:C4195")

Set varLookup = New Collection
varLookup.Add "A1:A10", "[country]"

Dim countryList(1) As String
countryList(0) = "US"
countryList(1) = "FR"

Dim strOuput As String
strOutput = "From UK to "

Dim strCombo As String
strCombo = "From UK to [Country]"

'For every country in list
Set sheetOutput = Worksheets("keyphrases")
For Each c In rngCountries.Cells
    strOutput = "From UK to " & c.Value
    sheetOutput.Cells(intRow, intCol).Value = strOutput
    intRow = intRow + 1
Next
For Each c In rngCountries.Cells
    strOutput = "From " & c.Value & " to UK"
    sheetOutput.Cells(intRow, intCol).Value = strOutput
    intRow = intRow + 1
Next

'For every city in list
For Each c In rngCities.Cells
    strOutput = "From UK to " & c.Value
    sheetOutput.Cells(intRow, intCol).Value = strOutput
    intRow = intRow + 1
Next
For Each c In rngCities.Cells
    strOutput = "From " & c.Value & " to UK"
    sheetOutput.Cells(intRow, intCol).Value = strOutput
    intRow = intRow + 1
Next

'For every airport in list
For Each c In rngAirports.Cells
    strOutput = "From UK to " & c.Value
    sheetOutput.Cells(intRow, intCol).Value = strOutput
    intRow = intRow + 1
Next
For Each c In rngAirports.Cells
    strOutput = "From " & c.Value & " to UK"
    sheetOutput.Cells(intRow, intCol).Value = strOutput
    intRow = intRow + 1
Next
' From every City to Country [UK]
For Each c In rngCities.Cells
    strOutput = "From " & c.Value & " to UK"
    sheetOutput.Cells(intRow, intCol).Value = strOutput
    intRow = intRow + 1
Next

For x = LBound(countryList) To UBound(countryList) 'define start and end of array
    strOutput = "From UK to " & countryList(x)
Next x ' Loop!

Set sheetOutput = Nothing

終了機能

私は 2km の長いコードを手に入れましたが、まだ他に 15 の組み合わせを行う必要があります。どうすれば簡単にできますか? 他の 15 個の組み合わせをコピーして貼り付けたくありません。

どうもありがとう

4

1 に答える 1

3

範囲を渡すサブルーチンを次のように記述します。

Sub PrintRange (r As Range)
    'For every city in list
    For Each c In r.Cells
        strOutput = "From UK to " & c.Value
        sheetOutput.Cells(intRow, intCol).Value = strOutput
        intRow = intRow + 1
    Next
    For Each c In r.Cells
        strOutput = "From " & c.Value & " to UK"
        sheetOutput.Cells(intRow, intCol).Value = strOutput
        intRow = intRow + 1
    Next
End Sub

次に、次のように呼び出します。

PrintRange rngCities

範囲のハードコーディングを避けるために、名前付き範囲を使用できます。

于 2013-08-18T14:31:58.033 に答える