我目前正在使用VBA作为excel项目中的一个Sudoku程序,我需要在变量中存储一系列单元格 .
有点像这样:
'''
dim grid as string
grid = range("b3:j11")
'''
但我不知道该如何调暗网格 . 我已经尝试了整数,字符串和范围,但网格似乎永远不会有值 . 我还假设我没有正确地将范围分配给变量
/////////////////////////////////
在回复后我将代码粘贴在这里 . duplicatecheck函数查看一系列单元格以检查它们是否超过某个数字,然后相应地突出显示单元格
由于我想通过不同范围的单元格查看不同的检查,我需要将范围变为变量,以便可以重用该函数
但是当在duplicatecheck函数中调用变量网格时,它没有任何值,并且它们不会检查任何单元格
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
结束如果结束如果下一步检查结束功能
1 回答
将其存储在变体中
然后,vGrid将成为基于2D的数组,其中Dim 1 =行,Dim2 =列
例如,B3的内容将在vGrid(1,1)中
Edit: 现在看来你正在使用Grid作为Range对象的字符串参数,并且你的CountIF有问题 .
如我在评论中提到的那样设置 Option Explicit 和要求变量声明的选项 .
就你的CountIF语句而言,如果你检查的范围是在Grid中声明的范围,并且你的标准是在Selection.Offset(row,col)中,那么该行应该类似于:
请注意,我已将使用函数更正为Worksheet对象的属性;将范围和标准参数按正确顺序排列;并删除了您在该函数末尾添加的.check .