Paste long set of numerals as string

Jeff H
4StarLounger
Posts: 415
Joined: 31 Oct 2017, 20:07

Paste long set of numerals as string

Post by Jeff H »

I have the following code to replicate sudoku givens as a string value in order to upload and download to different apps. It works fine for an uncompleted puzzle because I represent the blank cells with dots. But when I try to generate a string for the solution, Excel insists on displaying it in scientific notation because it is 81 numerals. Is there a way I can paste this as a literal string? (I have set the target cell to Text format, but that doesn't help.)

Code: Select all

Sub ExportFile()
Dim c
Dim txtString As String

txtString = ""

For Each c In Range("rngGrid")
    If c > 0 And c < 10 Then
        txtString = txtString & Trim(Str(c))
    Else
        txtString = txtString & "."
    End If
Next c

Range("txtExportString") = Str(txtString)

End Sub

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Paste long set of numerals as string

Post by YasserKhalil »

Did you try using the function CStr to convert it to a string?
Another trick is to format the target cells as text.

Jeff H
4StarLounger
Posts: 415
Joined: 31 Oct 2017, 20:07

Re: Paste long set of numerals as string

Post by Jeff H »

Unfortunately, no luck. I tried formatting all the cells in the grid as Text as well as the cell where I put the result. I tried Str(), Str$(), and CStr(), and none of them worked. I also tried adding "x" at the beginning if the string represents a completed puzzle, but then I don't know how to subsequently get rid of the "x" without the value reverting to scientific notation.

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Paste long set of numerals as string

Post by YasserKhalil »

What about putting an apostrophe at the start of the string?

User avatar
Doc.AElstein
BronzeLounger
Posts: 1499
Joined: 28 Feb 2015, 13:11
Location: Hof, Bayern, Germany

Re: Paste long set of numerals as string

Post by Doc.AElstein »

Hello Jeff
Another idea to try....

I am not quite familiar with what it is you are trying to do, or what the problem is, but just a shot in the dark… some variation of this might give you some idea
Paste your string into a cell as a formula of a string. You will end up with a formula in the cell , but you might see displayed what you want to see, I am not sure... but worth a quick try maybe

Code: Select all

Sub testit()
Dim txtString As String: Let txtString = "1.2.3.4.5.6"
 Let Range("A1").Value = "=" & """" & txtString & """"         
 '    same as      Let Range("A1").Value =  "="  &  """"  &  "1.2.3.4.5.6"  &  """"  
   '         "="  is VBA making an  =               """" is VBA making a "          "1.2.3.4.5.6" is VBA making a  1.2.3.4.5.6               so    = and " and 1.2.3.4.5.6 and "  is  finally what is put in the cell which is   ="1.2.3.4.5.6"  as you see in the formula bar 
End Sub
A1StringAsFormula.JPG

Alan

Edit P.S. if you take the value from the cell, you will end up back at the value, usually...

Code: Select all

Sub testitbackasvalue()
Dim txtString As String: Let txtString = "1.2.3.4.5.6"
 Let Range("A1").Value = "=" & """" & txtString & """"      
 MsgBox Prompt:=Range("A1").Value: Debug.Print Range("A1").Value
End Sub
You do not have the required permissions to view the files attached to this post.
Last edited by Doc.AElstein on 21 Mar 2021, 10:55, edited 6 times in total.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also

Jeff H
4StarLounger
Posts: 415
Joined: 31 Oct 2017, 20:07

Re: Paste long set of numerals as string

Post by Jeff H »

Thank you both for your suggestions. In fact, both methods worked! They both insert a number displayed as text, I can copy the string from the formula bar and paste it into a sudoku app like Hodoku and it works perfectly. Thanks!

FYI, Alan, sudoku is a 9x9 grid. Applications can recognize the placement of numbers and blanks if they are imported as a single, 81-character string. The characters are read into the grid from left to right and top to bottom.

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Paste long set of numerals as string

Post by LisaGreen »

Hi Jeff,'

You must be delimiting the digits in some way. Try using a space.

This is my code for putting the default string in a cell before I start looking for and deleting digits.

Code: Select all

slBase = " "
For lnglN = 0 To lnglBaseNumber
  slBase = slBase & CStr(lnglN + 1) & " "
Next lnglN

For Each rlCell In rlAll
  slCellValue = rlCell.Value
  If Not IsNumeric(slCellValue) _
  Or _
    Len(Trim$(slCellValue)) = 0 _
  Then
    rlCell.Value = slBase
    subSetBase rlCell
  Else
    subSetBigBlue rlCell
  End If
Next rlCell
As you can see there are other "Subs" involved in my code but that puts the basic 1 2 3 4 5 6 7 8 9 into each cell.

HTH
Lisa

Jeff H
4StarLounger
Posts: 415
Joined: 31 Oct 2017, 20:07

Re: Paste long set of numerals as string

Post by Jeff H »

Thanks Lisa. This looks quite interesting, but I'll have to try it out to understand what you mean. I'll reply back here when I've had a chance to do that. By any chance are you putting the candidates into an unsolved cell with this code?

My problem was about recording the overall solution to the puzzle. When all the cells are solved, Excel sees it as a single number with 81 digits instead of a string with 81 characters. The main sudoku generator I use is Hodoku and I can import and export unsolved puzzles from there using my code with dots representing the unsolved cells. The strings I'm saving are being used in a library of certain puzzles that help me learn.

Jeff H
4StarLounger
Posts: 415
Joined: 31 Oct 2017, 20:07

Re: Paste long set of numerals as string

Post by Jeff H »

After looking at the code a little closer, it does look to me like this must be a means to enter the remaining candidates in an unsolved cell. I'm guessing that subSetBase is a procedure to check the current cell's row, column, and block. And perhaps subSetBigBlue enters the cell's solution if the digit doesn't appear as a possible candidate in any of its houses. Am I correct about (any of) this?

My Excel sudoku grid is much simpler. I use Hodoku for utilizing all the candidates. But currently I've been studying Harold Nolte's method for paper and pencil solving. Nolte doesn't use any notations, not even the simplified Snyder notation. It's pretty fascinating and I'm enjoying learning to do it his way.

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Paste long set of numerals as string

Post by LisaGreen »

Jeff H wrote:
22 Mar 2021, 21:04
By any chance are you putting the candidates into an unsolved cell with this code?
Yes... I'm putting that string into all cells that do not have a number in them.. unsolved, and then for example looking at a row. If any of the other cells in the row have 1-9 in them I delete that number from the from the string in the cell I'm looking at.

The other subs just set the font size and colour.


BRIEFLY..

so if a row has..
Cell 1 2 3 4 5 6 7 8 9
Contents 3 / 1 2 3 4 5 6 7 8 9 / 1 2 3 4 5 6 7 8 9 / 5 / 7 / 9 / 1 2 3 4 5 6 7 8 9 / 1 / 1 2 3 4 5 6 7 8 9 /
Numeric Y N N Y Y Y N Y N

Then looking at cell 1 I skip it because it's numeric.
For cell 2 I look along the row and remove all "numerics". These are 3 5 7 9 and 1 and put the result back into cell 2. The result is 1 2 4 6 8.
I repeat for each cell and so on.

To delete the numbers from the string I'm using a simple Replace..
slCell = Replace(" " & trim$(slCell) & " ", " " & Cstr(lnglNumber) & " " , " ")
..and then removing double spaces.

HTH
Lisa

Code: Select all

Sub subSetBase( _
                rpRange As Range _
              )
subSetFontSizeAndColour rpRange
' *********************************************************************
End Sub
Sub subSetBigRed( _
                rpRange As Range _
                )
subSetFontSizeAndColour rpRange, 24, 255
' *********************************************************************
End Sub
Sub subSetBigBlue( _
                rpRange As Range _
                  )
subSetFontSizeAndColour rpRange, 24, 16763904
' *********************************************************************
End Sub
Sub subSetFontSizeAndColour( _
                    Optional rpRange As Range = Nothing, _
                    Optional lngpFontSize As Long = 11, _
                    Optional lngpFontColour As Long = 0 _
                        )
' This is here solely to make the calling easier.
' For example..
'  subSetBigRed
'
' ColorIndex is limited to 0-57.
'
' Normal Fill colour = -4105
' Green = 5287936
' Red = 255
' Blue = 16763904
' Ordinary black = 0
' Normal Fill
'    color = 16777215
'    index = -4142
' Dark Green
'     color =5287936
'     index = 14
' Red vbred color colorindex = 255
'    vbred = 3
'    index  = 3
'    color = 255
' Blue = 16763904
' Ordinary black = 0
'
' BOLD is set in any case.
' Font TYPE left alone. Default is usually Calibri.
'

Dim rlRange As Range

If rpRange Is Nothing Then '
  Set rlRange = Selection
Else
  Set rlRange = rpRange
End If

With rlRange
    .Font.Size = lngpFontSize
    .Font.Color = lngpFontColour
    .Font.Bold = True
End With

' *********************************************************************
End Sub

Jeff H
4StarLounger
Posts: 415
Joined: 31 Oct 2017, 20:07

Re: Paste long set of numerals as string

Post by Jeff H »

That's really nice, Lisa! Thanks for providing this code. I'm going to save it because it would be fun to be able to do that. For now I'm sticking with the Nolte method, but this falls under the category of interesting VBA projects. I appreciate you sharing this with me.

- Jeff

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Paste long set of numerals as string

Post by LisaGreen »

Jeff,

You're welcome. It's still a work in progress but I'm pretty much finished.

I'm not designing to actually solve a puzzle, just to fill in the possible numbers to print off.

I'm trying to code to make it fill in jigsaw sudoku puzzles as well like those here https://www.sudokuwiki.org/jigsaw.htm.

That site is quite good I think. https://www.sudokuwiki.org.

HTH
Regards
Lisa

Jeff H
4StarLounger
Posts: 415
Joined: 31 Oct 2017, 20:07

Re: Paste long set of numerals as string

Post by Jeff H »

Yes, I used that solver in the past when I'd get stuck doing a Upuzzle sudoku. It's been renamed to sudokuwiki, I think. My bookmark still refers to it as Sudoku Solver by Andrew Stuart.

Lately though I've been using Hodoku which Jaco ("Sudoku Swami") recommended. It's a downloaded, Java-based generator/solver. You can specify a difficulty level and it will generate puzzles for you. It also has a very good online Solving Guide.

It determines a puzzle's level of difficulty by using techniques in a similar order to sudokuwiki and applying values to each technique. If the puzzle can be solved with simple techniques alone, it gets a low score and as more advanced techniques are required the level-score goes up.

I haven't gotten into any variations. I'm still at a very low level of classic sudoku competence. But I do enjoy it and I am learning.

- Jeff

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Paste long set of numerals as string

Post by LisaGreen »

Cool!! Will check it out.... thank you!

Lisa