0

我想知道是否可以向用户询问一个数字(我知道该怎么做),然后从假设 G 列开始,根据用户输入的数字循环遍历每一列?

例子:

用户输入 = 4

   G       H       I       J       ...
Item_G1 Item_H1 Item_I1 Item_J1 Item_...n1
Item_G2 Item_H2 Item_I2 Item_J2 Item_...n2
Item_G3 Item_H3 Item_I3 Item_J3 Item_...n3
Item_G4 Item_H4 Item_I4 Item_J4 Item_...n4

意思是,输入一个数字,然后根据数字循环在那个字母+字母的数量上。所以,在完成所有 G 的项目遍历 H 的项目之后,等等...... N 个数字可能是输入。我在这里看到了类似的东西,但我不知道数字如何适合那里(VBA 的新手)。是否可以将一个数字添加到 G 并突然进入 F?

4

3 回答 3

1

这是循环遍历范围内所有值的替代方法。

Dim lngRows As Long
Dim lngColumns As Long
Dim rngStartingCell As Range
Dim rngItems As Range

lngColumns = InputBox("Input Number Of Items")
lngRows = InputBox("How Many Items For Each Item")

Set rngStartingCell = [G1] 'Change to your starting cell
Set rngItems = rngStartingCell.Resize(lngRows, lngColumns)

With rngItems
    .Formula = "=""Item_"" & SUBSTITUTE(ADDRESS(ROW(),COLUMN()),""$"","""")"
    'The Following Line is Optional But it well remove the formula from the cells
    'Leaving only the Desired results
    .Value = .Value
End With

但我喜欢在我的代码周围放置一个包装器,如下所示,这将提高代码的性能。如果您有工作表事件,有助于避免无限循环

Sub Sample()
'Save Settings, Then turn them all off
'///////////////////////////////////////////////////////////////////////////////////////////////
With Application
Dim StartingScreenUpdateing As Boolean
Dim StartingEnabledEvent As Boolean
Dim StartingCalculations As XlCalculation

StartingScreenUpdateing = .ScreenUpdating
StartingEnabledEvent = .EnableEvents
StartingCalculations = .Calculation

    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With
'///////////////////////////////////////////////////////////////////////////////////////////////

Dim lngRows As Long
Dim lngColumns As Long
Dim rngStartingCell As Range
Dim rngItems As Range

lngColumns = InputBox("Input Number Of Items")
lngRows = InputBox("How Many Items For Each Item")

Set rngStartingCell = [G1] 'Change to your starting cell
Set rngItems = rngStartingCell.Resize(lngRows, lngColumns)

With rngItems
    .Formula = "=""Item_"" & SUBSTITUTE(ADDRESS(ROW(),COLUMN()),""$"","""")"
    'The Following Line is Optional But it well remove the formula from the cells
    'Leaving only the Desired results
    .Value = .Value
End With

'Restore starting settings.
'///////////////////////////////////////////////////////////////////////////////////////////////
With Application
    .ScreenUpdating = StartingScreenUpdateing
    .EnableEvents = StartingEnabledEvent
    .Calculation = StartingCalculations
End With
'///////////////////////////////////////////////////////////////////////////////////////////////

End Sub
于 2013-11-04T21:20:10.743 回答
1

试试下面的代码

Sub main()


    user_input_rows = InputBox("Input rows to loop")
    user_input_cols = InputBox("Input columns to loop")

    If user_input_rows <> "" And IsNumeric(user_input_rows) And user_input_cols <> "" And IsNumeric(user_input_cols) Then

        For i = 1 To user_input_rows
            For j = 7 To user_input_cols
                Cells(i, j).Value = "Item_" & Split((Cells(i, j).Address), "$")(1) & Split((Cells(i, j).Address), "$")(2)
            Next
        Next

    End If


End Sub
于 2013-11-04T20:25:49.800 回答
0

好吧,我真的很感谢你们的回答,但我最终做了这样的事情。这些人帮助我到达了这里。

           firstSite = 7 'Primer Sitio
    cantidad_sitios = InputBox("Por Favor ingrese cantidad de sitios en Lista de Materiales: ", "Cantidad de Sitios")
    For i = firstSite To (cantidad_sitios - 1) + firstSite
       ' Getting Site Name & Code
        SiteName = MaterialListSheet.Range(Split(Cells(1, i).Address, "$")(1) & "4").Value
        SiteCode = MaterialListSheet.Range(Split(Cells(1, i).Address, "$")(1) & "3").Value

        'loop here
   Next i
于 2013-11-05T14:22:26.107 回答