Comment passer sur N colonnes lorsque N est un numéro auquel l'user est entré? VBA

Je me demandais s'il était possible de requestr à l'user un nombre (je sais comment faire) et ensuite, en commençant par disons colonne G, en boucle sur chaque colonne en fonction du nombre d'user entré?

Exemple:

Entrée user = 4

GHIJ ... 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 

Signification, entrez un nombre, puis selon le nombre de loops sur cette lettre + le nombre de lettres. Alors, après avoir terminé avec tous les éléments de G en bouclant les éléments de H, etc., N … N peut être l'input. J'ai vu quelque chose de semblable ici, mais je ne sais pas à quel point un nombre s'y rattache (nouveau pour VBA). Est-il possible d'append un chiffre à G et d'être soudainement en F?

Essayez le code ci-dessous

 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 

Voici une alternative à la mise en boucle de toutes les valeurs de votre gamme.

 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 

Mais j'aime mettre un wrapper autour de mon code comme suit, ce qui augmentera les performances de votre code. Et évitez les loops sans fin si vous avez des feuilles de travail

 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 

Eh bien, j'apprécie vraiment vos réponses, mais j'ai fini par faire quelque chose comme ça. Ces types m'ont aidé à arriver ici.

  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