如何将值分配给VBA中不同范围的单元格

我试图随机生成一个1到100之间的整数,无论​​是在单元格或直接在vba代码。 然后,我想使用该值作为VLookup的查找值,该值将从另一个表单中抽取另一个随机生成的1到10之间的整数。 然后,我想使用1到10之间的第二个数字作为指标,填充第一个数字在1到100之间的列中的许多单元格。

因此,例如,如果我手动做:我将在单元格“C27”Sheet1 =MROUND(RANDBETWEEN(1,100),1) 。 假设它返回40.然后,我将在Sheet2上查找列A中的数字40,查看列D,其中有另一个=MROUND(RANDBETWEEN(1,10),1) 。 假设一个返回5(所以我需要填写一个列的5个单元格)。 然后,我将返回Sheet1并input40到单元格K31到K35(原始随机整数)。

我知道, RANDRANDBETWEEN随时更新工作表重新计算。 我使用触发的IF语句来防止更新,除非我更改触发器单元格中的值。 如果使用VBA生成一个随机数就更容易了,我就是这么做的。

我不认为这会有助于我发布我尝试过的许多迭代,因为我试图将解决scheme应用于此macros的每个单独的任务。 他们中没有一个看起来甚至让我接近。 但是现在我正在使用的还没有接近。 这段代码是为了让我尝试工作。 所以这些数字是静态的,而不是随机的。 但我需要随机的。 是的,这是我为我的D&D游戏掌握生成随机怪物:)

感谢任何能够让我走上正轨的人!

 Sub MonsterRoll() ' ' MonsterRoll Dim ws As Worksheet Dim roll As Integer Dim No1 As Integer Dim No2 As Integer Set ws = Sheets("Combat Helper") roll = 5 No1 = 31 No2 = 31 + 5 On Error Resume Next For i = No1 To No2 area.Cells(i, 11).Value = 5 Next End Sub 

这个表格把vlookups放到表格“Encounters”

该表包含源数据,列D是RANDBETWEEN

我仍然不确定几个单元格的参考,但认为我有一个大致的想法。 下面的代码可以作为你想要做的大部分事情的起点 – 有几个警告…

由于您正在监视Sheet1单元格K31:K50中的更改,然后对同一范围进行更改,这将再次触发更改事件。 所以,为了避免疯狂的结果,我添加了一个标志,以便它将忽略更改,直到您告诉它停止忽略。 这将是当你完成所有处理你原来的变化。

就个人而言,我宁愿通过代码生成我自己的随机数字,原因很简单,任何更改任何单元格都会触发所有“随机”数字重新生成。

去function“Set_All_Cell_Values”,并添加任何代码,你需要填补其他单元格。

 Option Explicit Dim blnIgnoreChanges As Boolean Private Sub Worksheet_Change(ByVal Target As Range) Dim ws1 As Worksheet Dim ws2 As Worksheet Dim i As Integer Dim iYourNbr As Integer Dim iMyNbr As Integer Dim iRow As Integer Dim iHowMany As Integer Dim Why As String ' The following code can be dangerous if your code is not working properly!!!! ' Since you want to 'monitor' changes to K31:K50, and then change those same cells via code, ' which will in turn trigger this 'Worksheet_Change' subroutine to fire again, ' you need to be able to ignore changes on demand. ' If this flag gets set and your code didn't complete (AND turn the flag off), then ' any monitoring of future changes will be ignored!! ' If the flag fails to get reset, then just execute the following code in the immediate window: ' blnIgnoreChanges = false If blnIgnoreChanges = True Then Exit Sub End If Set ws1 = ThisWorkbook.Worksheets("Combat Helper") Set ws2 = ThisWorkbook.Worksheets("Encounters") ' Sample data in Sheet2 ' ABCDEFGHIJ '40 Bird, Falcon 1 1 1 -10 5 2 1d4 t '41 Men: Wild Man 2 3 2 -9 2 3 1d5 u '42 Beast 3 5 3 -8 3 4 1d6 v '43 Elephant 4 7 4 -7 4 5 1d7 w ' Monitor only cells K31:K50 If Target.Row >= 31 And Target.Row <= 50 And Target.Column = 11 Then ' Value must be between 1 and 100 If Target.Value < 1 Or Target.Value > 100 Then MsgBox "Must enter between 1 and 100" Exit Sub Else ' If you want to Lookup match in Col A of Sheet2, and then get value from col D. iYourNbr = Application.VLookup(Target.Value, ws2.Range("A3:N102"), 4, False) ' I prefer to Generate my own random number between 1 and 10 iMyNbr = Int((10 - 1 + 1) * Rnd + 1) iRow = Find_Matching_Value(Target.Value) Debug.Print "Matching Row in Sheet2 is: " & iRow ' DANGER!! If you execute the following line of code, then you MUST set to FALSE ' when you have finished one change!!! blnIgnoreChanges = True iHowMany = Sheet2.Cells(iRow, 4).Value Sheet1.Cells(Target.Row, 13) = iHowMany Set_All_Cell_Values Target.Row, iRow, iHowMany End If ' We can ignore all other cell changes Else 'Debug.Print "Change made to: " & "R" & Target.Row & ":C" & Target.Column & " but not my row or column! Value is:" & Target.Value End If End Sub Function Set_All_Cell_Values(iS1Row As Integer, iS2Row As Integer, iHowMany As Integer) Dim i As Integer Debug.Print "Add code to set cells for Sheet1 R:" & iS1Row & " Sheet2 R:" & iS2Row For i = iS1Row + 1 To iS1Row + iHowMany - 1 Sheet1.Cells(i, 11) = Sheet1.Cells(iS1Row, 11) '################################################# ' ADD CODE TO FILL OTHER CELLS as needed!!! '################################################# Next i blnIgnoreChanges = False End Function Function Find_Matching_Value(iFind As Integer) As Integer Dim Rng As Range If Trim(iFind) <> "" Then With Sheets("Encounters").Range("A:A") Set Rng = .Find(What:=iFind, _ After:=.Cells(.Cells.Count), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not Rng Is Nothing Then Find_Matching_Value = Rng.Row Else MsgBox "Did not find match for value: " & iFind End If End With Else MsgBox "You passed an empty value to 'Find_Matching_Value'" End If End Function