Function GetHeight(selectionX)

   On Error Resume Next
   GetHeight = selectionX.bounds(0)(2)
   If Err.Number = -2147352565 Then
   	GetHeight = InputBox("No selection, insert Height:")
   else
   	if Err.Number > 0 then
   		Msgbox ("Error :"&Err.Number)
   	end if
   end if
    
End Function



Function GetWidth(selectionX)

   On Error Resume Next
   GetWidth = selectionX.bounds(0)(3)
   If Err.Number = -2147352565 Then
   	GetWidth = InputBox("No selection, insert Width:")
   else
   	if Err.Number > 0 then
   		Msgbox ("Error :"&Err.Number)
   	end if
   end if

End Function



Dim appRef
Set appRef = CreateObject("Photoshop.Application")

Dim docRef
Set docRef = appRef.ActiveDocument

Dim squareSizeX, squareSizeY
Dim ShapeRef

squareSizeX = GetWidth(docRef.Selection)
squareSizeY = GetHeight(docRef.Selection)
'squareSizeX = InputBox("No selection, insert Width:")
'squareSizeY = InputBox("No selection, insert Height:")
If appRef.preferences.rulerUnits <> 1 Then
    appRef.preferences.rulerUnits = 1
End If
    
'msgbox  squareSizeX
'msgbox squareSizeY

If MsgBox("x=" & squareSizeX & " / y=" & squareSizeY, vbYesNo) = vbYes Then
   
    Dim layerRef
    Set layerRef = docRef.activeLayer
    
    If InputBox("1.Each layer place 2.all together layers") = 1 Then
    
        For y = 0 To docRef.Height - 1
        
            For x = 0 To docRef.width - 1
        
		layerRef.Copy
		docRef.Paste
                
                ShapeRef = Array(Array(x, y), Array(x, y + squareSizeY), Array(x + squareSizeX, y + squareSizeY), Array(x + squareSizeX, y))                              
                docRef.Selection.Select ShapeRef, psReplaceSelection, 0, False
        
                docRef.Selection.Invert()
                docRef.Selection.Clear()
                                
                x = x + squareSizeX - 1
            Next 
            y = y + squareSizeY - 1
        Next 
        
    Else
    
        For y = 0 To docRef.Height - 1
        
            For x = 0 To docRef.width - 1
        
		layerRef.Copy
		docRef.Paste
                
                ShapeRef = Array(Array(x, y), Array(x, y + squareSizeY), Array(x + squareSizeX, y + squareSizeY), Array(x + squareSizeX, y))
                docRef.Selection.Select ShapeRef, psReplaceSelection, 0, False
        
		docRef.Selection.Invert()
		docRef.Selection.Clear()
		docRef.Selection.Invert()

		docRef.Selection.translate -x,-y 
                
                x = x + squareSizeX - 1
            Next 
            y = y + squareSizeY - 1
        Next 
    
    End If

End If