Excel VBA error 1004 when using range -


i have work in vba need make square size of 512x512 (cellsxcells). square suppose borders of cells. made size of square dynamic user can insert size wants (max 512).

now tried few techniques above fail because of error 1004 run time.

sub printgrid(gridsize)  range(cells(1, 1), cells(gridsize, gridsize)).select selection.borders(xledgeleft)         .linestyle = xlcontinuous         .weight = xlthick end  selection.borders(xledgebottom)         .linestyle = xlcontinuous         .weight = xlthick end    selection.borders(xledgetop)         .linestyle = xlcontinuous         .weight = xlthick end  selection.borders(xledgeright)         .linestyle = xlcontinuous         .weight = xlthick end  selection.borders(xledgeright)         .linestyle = xlcontinuous         .weight = xlthick end  end  end sub 

my second attempt cell cell...

sub printborders(gridsize) = 1 gridsize ' right side     cells(i, 1).select     selection.borders(xledgeleft)         .linestyle = xlcontinuous         .weight = xlthick     end next   = 1 gridsize ' bottom     cells(gridsize, i).select     selection.borders(xledgebottom)         .linestyle = xlcontinuous         .weight = xlthick     end next  = gridsize 1 step -1 ' top     cells(1, i).select     selection.borders(xledgetop)         .linestyle = xlcontinuous         .weight = xlthick     end next  = 1 gridsize ' center     cells(i, 64).select     selection.borders(xledgeright)         .linestyle = xlcontinuous         .weight = xlthick     end next    = 1 gridsize ' left     cells(i, gridsize).select     selection.borders(xledgeright)         .linestyle = xlcontinuous         .weight = xlthick     end next  end sub 

at printborders fail when try make left grid. (cells(i, gridsize).select).

i starting think sort kind of limit in excel. help!

edit:

when mean dynamic: please trying running inputs 64 or 512.

sub main() dim gridsize integer, numofdots integer call setgrid end sub  sub setgrid() dim size integer cells.select selection.delete shift:=xlup  gridsize = setgridsize()  call printgrid2(1, 1, gridsize, gridsize) 'call printgrid2(1, 1, gridsize, gridsize / 2)  end sub  function setgridsize() while true     gridsize = inputbox("enter grid size:")     if (isnumeric(gridsize)) , (gridsize mod 2 = 0)         setgridsize = gridsize         exit     end if loop end function  sub printgrid2(x, y, rowsize, colsize) dim rng range set rng = range(cells(x, y), cells(rowsize, colsize))   rng.borders(xledgeleft)         .linestyle = xlcontinuous         .weight = xlthick end  rng.borders(xledgebottom)         .linestyle = xlcontinuous         .weight = xlthick end  rng.borders(xledgetop)         .linestyle = xlcontinuous         .weight = xlthick end  rng.borders(xledgeright)         .linestyle = xlcontinuous         .weight = xlthick end  end sub 

this how run code, "dynamically". after removing option explicit, on new excel , trying not @ selections, works:

sub dynamictest()      printborders (10)     printborders (20)     printborders (30)     printborders (inputbox("dynamically"))  end sub  sub printborders(gridsize long)     = 1 gridsize    ' right side         cells(i, 1).select         selection.borders(xledgeleft)             .linestyle = xlcontinuous             .weight = xlthick         end     next       = 1 gridsize    ' bottom         cells(gridsize, i).select         selection.borders(xledgebottom)             .linestyle = xlcontinuous             .weight = xlthick         end     next      = gridsize 1 step -1    ' top         cells(1, i).select         selection.borders(xledgetop)             .linestyle = xlcontinuous             .weight = xlthick         end     next      = 1 gridsize    ' center         cells(i, 64).select         selection.borders(xledgeright)             .linestyle = xlcontinuous             .weight = xlthick         end     next        = 1 gridsize    ' left         cells(i, gridsize).select         selection.borders(xledgeright)             .linestyle = xlcontinuous             .weight = xlthick         end     next  end sub 

this have changed:

from: sub printborders(gridsize)

to: sub printborders(gridsize long)

or may ask numeric input in inputbox this: inputbox("dynamically", type:=1). (credit david zemens)

in general, why error appears:

the cells takes parameter overloading in vba. means, can refer cells of these two:

cells(long, long) -> cells(1,1)

cells(long, string) -> cells(1,"a")

gridsize string, no problem used in for-loop such, because internally casted numeric value.

however, when try select cells(i, gridsize), vba looks function cells(long, string) first. expects string column name. however, not have column named 111, throws error. enter image description here


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -