网站模板代理,wordpress google翻译,福建省交通建设质量安全监督局网站,网页制作素材dw做数独游戏的时候#xff0c;画在纸上很容易弄花眼#xff0c;所以我考虑用Excel辅助做一个。 界面如下#xff1a; 按下初始化表格区域按钮#xff0c;会在所有单元格中填充“123456789”。如下图#xff1a; 当某个单元格删除得只剩一个数字时#xff0c;会将同一行、…做数独游戏的时候画在纸上很容易弄花眼所以我考虑用Excel辅助做一个。 界面如下 按下初始化表格区域按钮会在所有单元格中填充“123456789”。如下图 当某个单元格删除得只剩一个数字时会将同一行、同一列和同一区域的其它单元格中的相同数字删除。如下图 实现上述效果的VBA如下 1、初始化按钮的代码
Sub startup_Click()Dim row%, col%For row 1 To 9For col 1 To 9Cells(row, col) 123456789NextNext
End Sub以上代码仅仅简单遍历相关单元格并填充字符串。 实现自动删除关联单元格中的数字的功能的代码放在工作表的Worksheet_Change事件中这样只要修改相关游戏区域中的单元格就会自动执行检查并删除有关数字。代码如下
Private Sub Worksheet_Change(ByVal Target As Range)Dim row%, col%, changeRow%, changeCol%, rngRow%, rngCol%, txt$changeRow Target.rowchangeCol Target.Column记录刚修改单元格的内容txt Cells(changeRow, changeCol)如果刚修改的单元格只剩下一个数字则执行自动消除If Len(txt) 1 Then防止修改单元格内容时工作表改变事件被循环触发Application.EnableEvents False确定同一区域单元格第一行行号If changeRow 4 ThenrngRow 1ElseIf changeRow 6 ThenrngRow 7ElserngRow 4End If确定同一区域单元格第一列列号If changeCol 4 ThenrngCol 1ElseIf changeCol 6 ThenrngCol 7ElserngCol 4End If将同一行、列及区域单元格中相关的数字删除For row 1 To 9For col 1 To 9If row changeRow Or col changeCol Or (row rngRow And row rngRow 3 _And col rngCol And col rngCol 3) ThenCells(row, col) Replace(Cells(row, col), txt, )End IfNextNextCells(changeRow, changeCol) txt恢复事件处理以继续响应工作表改变事件Application.EnableEvents TrueEnd If
End Sub下面再附上一个用VBA做数独的程序不过没有优化
Sub VBA做数独()Dim targetRegion As StringDim origStr, tmpStr, tStr As Stringi, j, r, c, tmpr, tmpc, tr, 用于遍历表格stackR为堆栈指针Dim i, j, r, c, tmpr, tmpc, tr, tc, tmpLen, targetRow, targetCol, stackR As IntegerDim change As BooleanDim startTime, endTime As DatestartTime Now()origStr 1,2,3,4,5,6,7,8,9targetRegion A1:I9stackR 1Application.ScreenUpdating False 填写:change FalseFor r 1 To 9For c 1 To 9If Len(Cells(r, c)) 1 ThentmpStr Cells(r, c) 单元格内容为已去掉用过的数字后的字串ElseIf Len(Cells(r, c)) 1 And Cells(r, c) 0 ThenGoTo 跳到下一单元格 单元格数字已确定跳到下一单元格ElsetmpStr origStr 单元格为空单元格设定内容为原始字符串End If 将同一行中已用过的数字从原始字串中去除For tmpc 1 To 9If Len(Cells(r, tmpc)) 1 ThenIf InStr(tmpStr, Cells(r, tmpc)) 0 ThentmpStr Replace(tmpStr, Cells(r, tmpc), )change TrueEnd IfEnd IfNext将同一列中已用过的数字从原始字串中去除For tmpr 1 To 9If Len(Cells(tmpr, c)) 1 ThenIf InStr(tmpStr, Cells(tmpr, c)) 0 ThentmpStr Replace(tmpStr, Cells(tmpr, c), )change TrueEnd IfEnd IfNext将同一区域中已用过的数字从原始字串中去除If r 4 Thentr 1ElseIf r 6 Thentr 7Elsetr 4End If If c 4 Thentc 1ElseIf c 6 Thentc 7Elsetc 4End IfFor tmpr tr To tr 2For tmpc tc To tc 2If Len(Cells(tmpr, tmpc)) 1 ThenIf InStr(tmpStr, Cells(tmpr, tmpc)) 0 ThentmpStr Replace(tmpStr, Cells(tmpr, tmpc), )change TrueEnd IfEnd IfNextNexttStr Replace(tmpStr, ,, )某个单元格的数字全部删完那么这种填法错误If Len(tStr) 0 ThenIf stackR 10 Then出栈Range(A stackR :i stackR 8).SelectSelection.CutRange(A1).SelectPaste调整堆栈指针stackR stackR - 10GoTo 填写ElseMsgBox (﹏)~这题无解。 堆栈到底没有可能情况了无解Exit SubEnd If ElseIf Len(tStr) 1 ThenCells(r, c) tStrElseCells(r, c) tmpStrEnd IftmpStr origStrtStr 跳到下一单元格:NextNext If change False ThenFor r 1 To 9For c 1 To 9 分析同一行的情况判断是否出现可确定数字的单元格For tmpc 1 To 9If Len(Cells(r, tmpc)) 1 ThentStr tStr Cells(r, tmpc)End IfNext For i 1 To 9If Len(tStr) - Len(Replace(tStr, i, )) 1 ThenFor tmpc 1 To 9If InStr(Cells(r, tmpc), i) 0 ThenCells(r, tmpc) iGoTo 填写End IfNextEnd IfNexttStr 分析同一列的情况判断是否出现可确定数字的单元格For tmpr 1 To 9If Len(Cells(tmpr, c)) 1 ThentStr tStr Cells(tmpr, c)End IfNextFor i 1 To 9If Len(tStr) - Len(Replace(tStr, i, )) 1 ThenFor tmpr 1 To 9If InStr(Cells(tmpr, c), i) 0 ThenCells(tmpr, c) iGoTo 填写End IfNextEnd IfNexttStr 分析同一区域的情况判断是否出现可确定数字的单元格If r 4 Thentr 1ElseIf r 6 Thentr 7Elsetr 4End IfIf c 4 Thentc 1ElseIf c 6 Thentc 7Elsetc 4End IfFor tmpr tr To tr 2For tmpc tc To tc 2If Len(Cells(tmpr, tmpc)) 1 ThentStr tStr Cells(tmpr, tmpc)End IfNextNextFor i 1 To 9If Len(tStr) - Len(Replace(tStr, i, )) 1 ThenFor tmpr tr To tr 2For tmpc tc To tc 2If InStr(Cells(tmpr, tmpc), i) 0 ThenCells(tmpr, tmpc) iGoTo 填写End IfNextNextEnd IfNext NextNextFor r 1 To 9For c 1 To 9If Len(Cells(r, c)) 1 Then找到可填数字最少的未定单元格也就是其中字符串长度最短的使堆栈最小tmpLen 17For i 1 To 9For j 1 To 9If Len(Cells(i, j)) 1 And Len(Cells(i, j)) tmpLen ThentmpLen Len(Cells(i, j))targetRow itargetCol jEnd IfNextNextRange(targetRegion).Copyp 1s Replace(Cells(targetRow, targetCol), ,, )将所有可能情况入栈最后一种可能情况直接在目标区修改While p Len(s)stackR stackR 10Range(A stackR).SelectPasteCells(stackR targetRow - 1, targetCol) Mid(s, p, 1)p p 1WendCells(targetRow, targetCol) Mid(s, p, 1)GoTo 填写End IfNextNext ElseGoTo 填写End IfApplication.ScreenUpdating TrueendTime Now()MsgBox ~\(≧▽≦)/~解决了!耗时 Application.Text(endTime - startTime, m:s)End Sub
文章转载自: http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.cpqqf.cn.gov.cn.cpqqf.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.gkmwx.cn.gov.cn.gkmwx.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.jyznn.cn.gov.cn.jyznn.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.qcrhb.cn.gov.cn.qcrhb.cn http://www.morning.tnfyj.cn.gov.cn.tnfyj.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.ntdzjx.com.gov.cn.ntdzjx.com http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.sgrwd.cn.gov.cn.sgrwd.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn http://www.morning.swzpx.cn.gov.cn.swzpx.cn http://www.morning.mfmx.cn.gov.cn.mfmx.cn http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.yrmpz.cn.gov.cn.yrmpz.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.cfocyfa.cn.gov.cn.cfocyfa.cn http://www.morning.yxplz.cn.gov.cn.yxplz.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.qcwrm.cn.gov.cn.qcwrm.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.pdghl.cn.gov.cn.pdghl.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.wtnyg.cn.gov.cn.wtnyg.cn http://www.morning.fnssm.cn.gov.cn.fnssm.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.tpkxs.cn.gov.cn.tpkxs.cn http://www.morning.yhywr.cn.gov.cn.yhywr.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.bpmdn.cn.gov.cn.bpmdn.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.rzczl.cn.gov.cn.rzczl.cn