Active Cell in VBA - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Active Cell in VBA (/showthread.php?tid=44779)
Active Cell in VBA by mwe99 on 05-15-2005 at 10:03 PM
Excel VBA,
When a command button is clicked, it puts a number in D11, what code do i use to make it move along to the next cell E11.
tricky part, i need this for all six command buttons when the first one is clicked, it moves to the next cell on any of the buttons
RE: Active Cell in VBA by CookieRevised on 05-15-2005 at 11:20 PM
There are many possebilities.
Ranging from the use of the objects: Range(), Cells(), Selection(), Offset(), etc...
The principle is to keep your locations in variables (and update them) and use those variables as your locations in those objects.
I can't give a strait forward solution, as there are so many possebilities to do this...
You should already know that:
* to set a numerical value 123 inside the cell A1 on a worksheet called "mysheet":
Worksheets("mysheet").Range("A1").Value = 123
* to set a numerical value 123 inside the range A1:H8 on a worksheet called "mysheet":
Worksheets("mysheet").Range("A1:H8").Value = 123
(this is basic VBA/Excel knowledge you should know before you start programming anything)
From here it is easy to make the location "A1" as a variable...
Dim mylocation As String
mylocation = "A1"
Worksheets("mysheet").Range(mylocation).Value = 123
mylocation = "A2"
Worksheets("mysheet").Range(mylocation).Value = 123
mylocation = "A3"
Worksheets("mysheet").Range(mylocation).Value = 123
But to change the location you need some things todo and to interpret the alphanumerical data and all. In your case, it is easier to define an offset from the topleft of the location and count upwards from there:
In your global variable space you define an array which will hold all the current locations. You don't need to use an array but the use of an arrray depends on how and what you want to do. And since I have to do with the info you gave me I assume you want to hold all the locations globally and independantly:
Dim CellLocation(1 to 6) As Integer
In the click procedure of button 1 you need to add something like:
Worksheets("mysheet").Range("D11").Offset(0, CellLocation(1)).Value = mynumber
CellLocation(1) = CellLocation(1) + 1
In the click procedure of button 2 you need to add something like:
Worksheets("mysheet").Range("D12").Offset(0, CellLocation(2)).Value = mynumber
CellLocation(2) = CellLocation(2) + 1
Etc...
Important part is to use the integrated help function of VBA!!!! Search and read the help for the objects used in this post: Range, Cells, Selection, Offset, Worksheets, etc... For the other things on how to do something (working with variables) you need to learn how to program, this is very hard to explain in 1 post and you're better of getting a book on VBA from your local library.
RE: Active Cell in VBA by mwe99 on 05-15-2005 at 11:33 PM
yeah thanks its for my mastermind game lol
still stuck on the basis but this bit been cleared up thanks
|