Auto increment without mouse [closed] - excel

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm write in a cell 1, then put the mouse over the black square at the bottom-right corner of that cell, right click and drag.
Principal question - I do not want to use a mouse. And look for script or keyboard hotkeys to got similar numbers
1 3
3 6
5 9
from keyword selected
1 3
3 6

I put the following macros in my Personal Macro Workbook and assigned a keyboard shortcut.
Sub FillSeries()
Dim lFirstBlank As Long
If TypeName(Selection) = "Range" Then
If Selection.Columns.Count = 1 Or Selection.Rows.Count = 1 Then
lFirstBlank = GetFirstBlank(Selection)
If lFirstBlank = 0 Then
SelectAdjacentCol
lFirstBlank = GetFirstBlank(Selection)
End If
If lFirstBlank > 1 Then
If Selection.Columns.Count = 1 Then
Selection.Cells(1).Resize(lFirstBlank - 1).AutoFill _
Selection, xlFillSeries
ElseIf Selection.Rows.Count = 1 Then
Selection.Cells(1).Resize(, lFirstBlank - 1).AutoFill _
Selection, xlFillSeries
End If
End If
End If
End If
End Sub
Function GetFirstBlank(rRng As Range) As Long
Dim i As Long
i = 0
For i = 1 To rRng.Cells.Count
If IsEmpty(rRng.Cells(i)) Then
GetFirstBlank = i
Exit For
End If
Next i
End Function
Sub SelectAdjacentCol()
Dim rAdjacent As Range
If TypeName(Selection) = "Range" Then
If Selection.Column > 1 Then
If Not IsEmpty(Selection.Offset(0, -1).Value) Then
With Selection.Offset(0, -1)
Set rAdjacent = .Parent.Range(.Cells(1), .End(xlDown))
End With
Selection.Resize(rAdjacent.Cells.Count).Select
End If
End If
End If
End Sub
See also http://dailydoseofexcel.com/archives/2008/07/17/fillseries-keyboard-shortcut/
Update
If you only want to fill columns and you want to fill all the columns in a selection, then the below code should do what you want. It also looks at the NumberFormat of the last cell in the column and changes the NumberFormat for filled cells back to that. Picking the last cell is a little arbitrary, but it is what it is.
Sub FillSeriesForAllColumns()
Dim lFirstBlank As Long
Dim rCol As Range
Dim sOldNumberFormat As String
If TypeName(Selection) = "Range" Then
For Each rCol In Selection.Columns
sOldNumberFormat = Selection.Cells(Selection.Cells.Count).NumberFormat
lFirstBlank = GetFirstBlank(rCol)
If lFirstBlank = 0 Then
SelectAdjacentCol
lFirstBlank = GetFirstBlank(rCol)
End If
If lFirstBlank > 1 Then
rCol.Cells(1).Resize(lFirstBlank - 1).AutoFill _
rCol, xlFillSeries
End If
rCol.Offset(lFirstBlank - 1, 0).Resize(rCol.Row - (lFirstBlank - 1)).NumberFormat = sOldNumberFormat
Next rCol
End If
End Sub

Using the arrow keys select the column section including the header cell:
Hold down the Alt key and touch hfis sequentially. Then release the Alt key and touch the Enter key
..after:

Related

Copy a range of cells in a row and paste same row/range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
Thats my data. I want to copy B2:M9 and insert it to P2. Furher, I want to copy B13:M20 and paste it to P2. Then I want to copy B24:M31 and paste it to P2 and so on.
Does anyone have an idea how I can do it?
Thank you!
Sub copypasta()
Dim nb_iter as Integer 'Number of ranges you have to copy
nb_iter = 3 'for example
Dim step as Integer
step = 0
for k = 1 to nb_iter
Range("B" & step + 2, "M" & step + 9).Copy
Range("P2").PasteSpecial , Paste:=xlPasteValues
step = step + 11 'you are adding 11 to both row numbers for each iteration
Next k
End Sub
My first attempt:
Sub Copypasta()
Dim i As Long
For i = 0 To 31
Range("B" & 2 + i, "M" & 9 + i).Copy
Range("P2").PasteSpecial Paste:=xlPasteValues
Next i
End Sub

VBA Find a Row with two variables then time stamp a column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could anyone give me advice on how to look up a row in a spreadsheet which matches two criteria, then change the data in specific Cells.
The data will be like this..
Reference Version date1 date2 date3
ABC1 1 11/12/2013
ABC1 2 31/12/2013
ABC2 1 12/12/2013
ABC3 1 12/12/2013
ABC1 3 01/01/2014
In VBA I wish to be able to find the row that matches the reference and the version number, then datestamp column 4 of that 1 unique row.
Any help would be really appreciated.
Many Thanks
Solving for a position for two criteria can be solved by this array formula (in this sample looking up ABC3 and 1 in A2:B6
This formula can be used in VBA to provide the position for the timestamp:
VBA Equivalent
Sub OneWay()
Dim lngRow As Long
lngRow = Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")
Cells(lngRow + 1, 4) = Now()
End Sub
or just:
Sub OneWay2()
lngRow = Cells(Evaluate("=MATCH(1,(A2:A6=""ABC3"")*(B2:B6=1),0)")+ 1, 4) = Now()
End Sub
Try this code:
Sub test()
Dim refToFind As String
Dim versToFind As String
refToFind = "ABC1"
versToFind = "1"
'address of first reference
Set Rng = Sheet1.Range("B2")
lastrow = Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp).Row - Rng.Row
For i = 0 To lastrow
If CStr(Rng.Offset(i, 0).Value) = refToFind _
And CStr(Rng.Offset(i, 1).Value) = versToFind Then
Rng.Offset(i, 4) = Now
End If
Next i
End Sub

Concatenate columns D and C using macro [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Could you please help in concatenating cells using Excel 2010 macro. I have columns A to E. I want to concatenate columns D and C in column F. Please note that I don't know the exact number of rows in column D and C but the macro should stop concatenating when there are no values in the said columns. Sample:
A B C D E F
0 Exist Echalas Gerald 25256 Gerald Echalas
....
Thank you.
Something like this (untested)
dim row as integer
row = 1
while(cells(row, 3) <> "" and cells(row, 4) <> ""
cells(row, 5) = cells(row, 3) & " " & and cells(row, 4)
row = row + 1
wend
Try this out, from MSDN, it has an example for going through each row.
http://support.microsoft.com/kb/213477
Their sample:
Sub ConcatColumns()
Do While ActiveCell <> "" 'Loops until the active cell is blank.
'The "&" must have a space on both sides or it will be
'treated as a variable type of long integer.
ActiveCell.Offset(0, 1).FormulaR1C1 = _
ActiveCell.Offset(0, -1) & " " & ActiveCell.Offset(0, 0)
ActiveCell.Offset(1, 0).Select
Loop
End Sub
This will combine values columns C, D to F as "D, C" regardless of gaps up to the last used row.
Sub CombineCols()
Dim oWS As Worksheet, lLastRow As Long, r As Long
Set oWS = ActiveSheet
lLastRow = oWS.Cells.SpecialCells(xlLastCell).Row
For r = 1 To lLastRow
' Combine if both C and D are not empty
If Len(oWS.Cells(r, 3)) > 0 And Len(oWS.Cells(r, 4)) > 0 Then
oWS.Cells(r, 6).Value = oWS.Cells(r, 4).Value & " " & oWS.Cells(r, 3).Value
End If
Next
End Sub
You can change the check condition so that it does not require both valid text is columns C and D to combine.
Tip: You can reference the column of a cell with long number - starting from A=1.
Lets say u r in sheet1
Sub test()
Dim r As Range
Dim lr As Integer
With Sheets("Sheet1")
lr = .Range("C500000").End(xlUp).Row
For Each r In .Range("F2:F" & lr)
r.Value = r.Offset(0, -2).Value & " " & r.Offset(0, -3).Value
Next r
End With
End Sub
Tested
Seems like a good job for a formula
Sub ConcatName()
With ActiveSheet
.Range("F1").Resize(.Range("A1").CurrentRegion.Rows.Count, 1).FormulaR1C1 = "=RC[-2]&"" ""&RC[-3]"
End With
End Sub
The CurrentRegion property gets the blob of data around A1, extends F1 by the number of rows, then inserts a formula that concatenates.
The concatenate function is quite easy to implement in a Macro. All you do is place an '&' between objects you would like to concatenate. In your case is would look like this:
Range("F1").Value = Range("C1").Value & " " & Range("D1")
*Note that I added a space between the C1 and D1 values
Next you are going to need a loop to iterate through your data. There are several good methods for controlling when the loop will stop.
Method #1
The simplest method is to not worry about when the macro stops. This may work in your case be concatenating nothing with nothing yields nothing.
loop
Range("F1").Value = Range("C1").Value & " " & Range("D1")
next
Method #2
End the loop when a specified column runs out of data. In your case this could look like:
While(Range("A" & i).value <> "")
Range("F1").Value = Range("C1").Value & " " & Range("D1")
i = i + 1
Wend
Method #3
Excel has a function (.count) that will return the number of entries in a column (will not work correctly if there are gaps). Use the value return along with a for loop to stop the loop when there is no more data.

EXCEL VBA Speed up my code [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
As per title, the codes below are located in my Private Sub Workbook_open(). Therefore every time I open my workbook it is slow. How do I get to optimize the code to run fastest? Any help is appreciated.
'Sheets("Summary(FG)") ComboBox1 items
For b = 3 To Sheets("CustomerList").Cells(3, 2).SpecialCells(xlLastCell).row
If Sheets("CustomerList").Cells(b, 2) <> "" Then
Worksheets("Summary(FG)").ComboBox1.AddItem (Sheets("CustomerList").Cells(b, 2))
Else
End If
Next
'Sheets("Summary(RawMat)") ComboBox1 items
For a = 2 To Sheets("RawMatList").Cells(2, 2).SpecialCells(xlLastCell).Column
If Sheets("RawMatList").Cells(2, a) <> "" Then
Worksheets("Summary(RawMat)").ComboBox1.AddItem (Sheets("RawMatList").Cells(2, a))
End If
Next
'sheets("Summary(WIP)") ComboBox1 items
For c = 3 To Sheets("WIPList").Cells(3, 2).SpecialCells(xlLastCell).row
If Sheets("WIPList").Cells(c, 2) <> "" Then
Worksheets("Summary(WIP)").ComboBox1.AddItem (Sheets("WIPList").Cells(c, 2))
End If
Next
For Each Worksheet In Worksheets
Application.Goto Reference:=Range("A1"), Scroll:=True
Next Worksheet
It looks like your loop is iterating through every row or every column on a worksheet. Instead of using the last row or last column try using the last used row or last used column. This way instead of moving through thousands of blank rows you only check rows containing data.
Try:
'Sheets("Summary(FG)") ComboBox1 items
For b = 3 To Sheets("CustomerList").UsedRange.Rows.Count
If Sheets("CustomerList").Cells(b, 2) <> "" Then
Worksheets("Summary(FG)").ComboBox1.AddItem (Sheets("CustomerList").Cells(b, 2))
Else
End If
Next
'Sheets("Summary(RawMat)") ComboBox1 items
For a = 2 To Sheets("RawMatList").UsedRange.Columns.Count
If Sheets("RawMatList").Cells(2, a) <> "" Then
Worksheets("Summary(RawMat)").ComboBox1.AddItem (Sheets("RawMatList").Cells(2, a))
End If
Next
'sheets("Summary(WIP)") ComboBox1 items
For c = 3 To Sheets("WIPList").UsedRange.Rows.Count
If Sheets("WIPList").Cells(c, 2) <> "" Then
Worksheets("Summary(WIP)").ComboBox1.AddItem (Sheets("WIPList").Cells(c, 2))
End If
Next
For Each Worksheet In Worksheets
Application.Goto Reference:=Range("A1"), Scroll:=True
Next Worksheet

How can you make Game of life in Excel? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know only little how to make macros in Excel.
You can find many examples through Google.
The very first result is a post from David Gainer's blog that uses Conway’s Game of Life to teach about circular reference formulas and iteration (no VBA involved):
http://blogs.office.com/2007/11/02/iteration-conways-game-of-life
Should have done this a long time ago but here's my version of Conway's Life in excel.
Here's a hack of the code. By no means a perfect solution (didn't spend an age on this) but you might be able to pick some bits out.
Private arrGrid(100, 100) As Boolean
Private arrGridNextGeneration(100, 100) As Boolean
Private Sub PopulateParentArrayData()
For k = 1 To Sheet1.Range("C2:AM20").Cells.Count
If Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color Then
arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) = True
Else
arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) = False
End If
DoEvents
Next
End Sub
Private Sub ApplyParentArrayData()
For k = 1 To Sheet1.Range("C2:AM20").Cells.Count
If arrGrid(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) Then
Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color
Else
Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("B1").Interior.Color
End If
DoEvents
Next
End Sub
Private Sub ApplyNextGenerationArrayData()
For k = 1 To Sheet1.Range("C2:AM20").Cells.Count
If arrGridNextGeneration(Sheet1.Range("C2:AM20").Cells(k).Row, Sheet1.Range("C2:AM20").Cells(k).Column) Then
Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("A1").Interior.Color
Else
Sheet1.Range("C2:AM20").Cells(k).Interior.Color = Sheet1.Range("B1").Interior.Color
End If
DoEvents
Next
End Sub
Private Function GetNeighbourCount(ByVal pintRow As Integer, ByVal pintColumn As Integer) As Integer
Dim intCount As Integer
intCount = 0
For r = pintRow - 1 To pintRow + 1
For c = pintColumn - 1 To pintColumn + 1
If r <> pintRow Or c <> pintColumn Then
If arrGrid(r, c) Then
intCount = intCount + 1
End If
End If
Next c
Next r
GetNeighbourCount = intCount
End Function
Private Sub PopulateNextGenerationArray()
Dim intNeighbours As Integer
For r = 0 To 100
For c = 0 To 100
If r > Sheet1.Range("C2:AM20").Rows(0).Row Then
If r <= Sheet1.Range("C2:AM20").Rows(Sheet1.Range("C2:AM20").Rows.Count).Row Then
If c > Sheet1.Range("C2:AM20").Columns(0).Column Then
If c <= Sheet1.Range("C2:AM20").Columns(Sheet1.Range("C2:AM20").Columns.Count).Column Then
intNeighbours = GetNeighbourCount(r, c)
If arrGrid(r, c) Then
'A1 cell
If intNeighbours < 2 Or intNeighbours > 3 Then
arrGridNextGeneration(r, c) = False
Else
arrGridNextGeneration(r, c) = True
End If
Else
'B1 cell
If intNeighbours = 3 Then
arrGridNextGeneration(r, c) = True
Else
arrGridNextGeneration(r, c) = False
End If
End If
End If
End If
End If
End If
DoEvents
Next c
Next r
End Sub
Private Sub ActionLogic()
'Application.ScreenUpdating = False
PopulateParentArrayData
PopulateNextGenerationArray
ApplyNextGenerationArrayData
'Application.ScreenUpdating = True
End Sub
To get this to work just make the background of cell A1 black, the background of cell B1 white and then add some black backgrounds in the range C2:AM20 and run the ActionLogic method.
You will need two macros. The first one should format the game sheet so the cells are square.
Have the user run this macro. After that she should enter a 1 for each cell that is alive. Use conditional formatting to turn the cell completely black (background = black if value != 0)
Now have a second macro which calculates the next step in a background sheet (another sheet). Use relative cell positioning (relative to ActiveCell) and two nested loops. When this is done, copy all values from the background sheet to the game sheet.
Search for it and look at their code. Plenty of people have made it a hobby to make full games in Excel.
Ex: http://www.geocities.jp/nchikada/pac/
Why do you say Excel is the wrong choice?
I think Excel is the best way to solve this:
Excel solves this with 1 line:
IF(OR(SUM(B2:D4)-C3=3,AND(SUM(B2:D4)-C3=2,C3=1)),1,0)
*where the above is an expression that returns the next generation value for the cell C3.
Here's the demo:
https://docs.google.com/open?id=0B4FcWULw3iQidlZSdG9GRDh0TXM
If you're in a situation where you have to implement this sort of things from scratch, then functional programming is the best way to go. Otherwise, Excel works really well. Why? Because Excel is a system that forces you to enter only pure functions. You see, the key to simulating this Game of Life is to realize that each state of the cells is a PURE FUNCTION of the previous state. Excel naturally forces you to think this way.
another tutorial on circular references in excel can be found here: http://chandoo.org/wp/2009/01/08/timestamps-excel-formula-help/
this one explains how you can insert timestamps using circular references.
Excel is definitely the wrong choice for this kind of a problem. As to how it would be possible: First learn about the game of life and then visual basic to use in Excel.

Resources