我目前正在使用VBA在Excel中将一个Sudoku程序作为项目工作,我需要将一个单元格范围存储在一个变量中。如何在一个变量中存储一系列单元格
沿东西这行:
'''
dim grid as string
grid = range("b3:j11")
'''
但我不知道该怎么调暗格的。 我试过整数,字符串和范围,但网格似乎永远不会有价值。 我还假设我没有正确指定范围到变量
///////////////////////////// ////
下面的回复im粘贴代码在这里。 duplicatecheck函数通过一系列单元格来检查它们是否超过某个数字,然后相应地突出显示单元格
正如我想查看不同范围的单元格以进行不同的检查,我需要可变所以该函数可以
然而当可变网格被称为在它没有价值的duplicatecheck功能和theirfor没有细胞被检查
Dim row As Integer
Dim col As Integer
Dim grid As Variant
Dim checkmark As Integer
Sub Rowcheck()
checkmark = 1
row = 0
col = 0
grid = range("b3:j3").Value
For row = 0 To 8
duplicatecheck
Next row
结束子
重复使用的范围Function duplicatecheck()
Dim safe As Boolean
Dim check As Integer
Dim x As Integer
' check each number in a range for duplicates starting with 1
For check = 1 To 9
If Application.Worksheet.Function.CountIf(Selection.Offset(row, col), range(grid)).check = 1 Then
' if number is only found once
safe = True
ElseIf Application.Worksheet.Function.CountIf(Selection.Offset(row, col), range(grid)).check < 1 Then
' if the number is found less than once in the range
safe = False
ElseIf Application.Worksheet.Function.CountIf(Selection.Offset(row, col), range(grid)).check > 1 Then
' if the number is found more than once
Selection.Offset(row, x).range(grid).Interior.colour = vbBlue ' highlight the range red
If checkmark = 1 Then
For x = 0 To 8
Selection.Offset(row, x).range(geid).Value = check
Selection.Offset(row, x).range.ont.colour = vbRed
Next x
ElseIf checkmark = 2 Then
For x = 0 To 8
Selection.Offset(x, col).range(grid).Value = check
Selection.Offset(x, col).range.ont.colour = vbRed
Next x
safe = False
error = True
If safe = False Then
complete = False
End If
结束如果 结束如果 接着检查 端功能
我试过这个代码和Rons但都没有工作。 将网格设置为范围或变量后,在网格或设置网格=范围(“b3:j11”)后,变量网格仍然没有值时稍后调用 – user2920648
检查我对Ron的回答的评论。变体可以存储范围或值。如果您尝试存储值,请使用范围的值,而不是范围本身。 – Manhattan