【发布时间】:2014-05-06 13:43:56
【问题描述】:
我目前正在使用 VBA 将 Sudoku 程序作为 excel 中的一个项目工作,我需要将一系列单元格存储在一个变量中。
类似这样的事情:
'''
dim grid as string
grid = range("b3:j11")
'''
但我不确定将网格调暗为什么。 我尝试过整数、字符串和范围,但网格似乎从来没有值。 我还假设我没有正确地将范围分配给变量
//////////////////////////////
按照我在此处粘贴代码的回复。 duplicatecheck 函数查看一系列单元格以检查它们是否超过一定数量的一个,然后相应地突出显示单元格
由于我想查看不同范围的单元格以进行不同的检查,因此我需要将范围设为变量,以便可以重复使用该函数
但是,当在 duplicatecheck 函数中调用变量 grid 时,它没有任何值,并且它们的单元格没有被检查
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”)后,变量网格在稍后调用时仍然没有值
-
查看我在 Ron 的回答中的评论。变体可以存储范围或值。如果您尝试存储值,请使用范围的值,而不是范围本身。