Copy a line to a DB sheet based on a calculated trigger - excel

After trying a few different approaches I am stuck. Basically I have a table where a line is completed every 30 mins or so. The User will enter 3 numbers in a row, the next 3 rows are calculated. I would like to use one of the calculated rows as a trigger to copy the entire row to a DB sheet (sheet2). And repeat when the next line is added and so on. I can only seem to get the 1st line to copy across every time.
The sheet is used as a hardcopy batch record so I have shied away from building a FORM. I started with the following, which worked for line 1 (also calling a recorded macro to copy in some header data)
`Private Sub Worksheet_Calculate()
If IsNumeric(Range("$h$9")) Then
If Range("$h$9").Value >= 1 Then
Application.Run "Macro1"
End If
End If
End Sub `
Macro1 just copying some of the header form data (date/time,machine info etc that is only entered once for the sheet & the row described above).
I hope this is clear, any questions please let me know

Welcome to SO
Try using a loop like below? Change the upper limit of i as you want, and maybe use some form of row counter like Cells(Rows.Count, 1).End(xlUp).Row
Private Sub Worksheet_Calculate()
Dim i As Integer
For i = 1 To 9000
If IsNumeric(Cells(i, 8)) Then
If Cells(i, 8).Value >= 1 Then
Application.Run "Macro1"
End If
End If
Next i
End Sub

Related

Copy a range of 2 rows and paste in next available blank space

I'm new to VBA and am trying to create a window order form to fit a landscape 8x11.5 page. The rows are set up as followed:
Each cell has a different data validation dropdown menu. I'd like to be able to copy this template and paste it below Line #1.
In this case Line #2 would begin at A5.
This code does the trick very crudely:
Sub CopyTemplate()
Worksheets("WINDOW TEMPLATE").Range("A3:R4").Copy Range("A5:R6")
End Sub
As you can see, the job is done crudely but I'd have to create a macro for each new line manually.
Is there a way to replace the destination range with a variable that will find the next available empty row to create Line 2 like
In the case of Line 2 it would be A7 (and then every two lines would be the loop I guess).
I've seen several example similar to mine but they never do what I need or I'm too inexperienced to retrofit the code to my needs. As I understand it, .End finds the next blank cell but all it seems to do is goto the bottom of the sheet.
Find the last numbered item in Col A then move down two rows:
Sub CopyTemplate()
Dim c as range
With Worksheets("WINDOW TEMPLATE")
Set c = .Cells(.Rows.Count, "A").End(xlUp).Offset(2) 'move down two rows from last numbered item
.Range("A3:R4").Copy c
c.value = c.offset(-2).value + 1 'increment item#
End With
End Sub

Conditionally copy, paste, delete between worksheets data missing

I have inherited a workbook that has suffered months of abuse (poor data entry, poor cut and paste, duplications, etc). I have nearly finished sorting the data but to prevent similar problems I am trying to automate some of the process. I am new to VBA and cannot figure out why my code isn't working. There are several worksheets that track an application through our new client process. Each worksheet has the same fifteen columns, metadata in the first 10 rows (=sum counts) and headers in the 11th row. When we receive interest in joining our organisation some simple data is entered into columns A-D and L-O inclusive. I want a particular row to move to the next worksheet when a date is entered to indicate a step in the process has been completed. For example when a client returns an application form, the date is entered into column G and the specific row is moved to the next worksheet and deleted from the original. The date entry works but columns L-O are not being copied. HELP!
The code I'm using is:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G12:G100" & Range("A" & Rows.Count).End(xlUp).Row)) Is Nothing And Target.Cells.Count = 1 Then
If IsDate(Target.Value) = True Then
Range(Cells(Target.Row, "A"), Target).Copy Sheets("Awaiting Interview").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Target.EntireRow.Delete
End If
End If
End Sub
Thank in advance.
AlVBA

Excel VBA that will copy real time data from a column into the next column, by time intervals across the sheet

I am having trouble creating some vba for a project that involves real time intervals.
The sheet originally created and used for this some 8 years a ago has disappeared in a Windows update last year. For the life of me I can not recreate the code after all this time.
I want to have have the data in column A move to the next column ater a specific time interval. Column A has real time data that changes in milliseconds and I want to record data from that column at a set time interval eg 1 minute.
At the next 1 minute mark, the new data in column A is to be moved into column B and the previous saved data in Column B is now pasted into Column C. At the next 1 minute mark the data in Column A goes to B, B goes to C and C goes to D etc.
Attached is a mockup of how it would look in the sheet, with original data feed in Column A and the captured data from the feed every 1 minute moving across the sheet -
Data Mockup
What I did find was a snippet of code I started with that may explain what I was trying to achieve. The friend that helped me with this in 2012 is no longer alive but he started with this -
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Columns.Count <> 16 Then Exit Sub
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Range("b5:b30").Value = Range("b5:b30").Value
Range("b5:b30").Value = Range("a5:a30").Value
I tried to copy/paste and change the `Range' lines in the VBA page for each column but it didn't work. I am actually hoping that there may be a simpler way to do this rather than writing the range lines for each column of the sheet.
Any help is appreciated
Try this one:
Dim i As Long
Sub test()
If i = 0 Then
i = 1
Else
i = i + 1
End If
Range("A5:A34").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B3").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B3").Value = "Minute " & i
Application.OnTime Now() + TimeValue("00:01:00"), "test"
End Sub
Sub Reset()
i=0
End Sub
First line is to declare one counter that will work each time that you use the code. One Procedure call test that will shift the values one the first column to the second one, as well as it will introduce the header. After one minute, it will shift 1st and 2nd columns and so on. There is another procedure to reset the counter.
In order to make it easy for your, just create 2 buttons on top of the header like "Run" and "Reset" and call this macros from there.
If you have any issue, just let me know.
Hope it helps!

Inserting data via macros (on top of other macros)

I have a series of macros that will insert data pertaining to which button is selected. The problem that I am having is that I have a series of 5 buttons each running a macro containing a different number of rows of data. After the data is entered from the macro, I have the same 5 buttons for the user to select another group of data. Since each macro is a different number of rows it keeps screwing up the formating (example: the 2nd set of macros is set to run in row 3, but when the first set is selected the data continues until row 5). Any advise on how to fix this?
Here is my macro's code:
Sub Realestate()
Sheet14.Unprotect Password:="Loan101"
Range("AV1:CP20").Select
Selection.Copy
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Range("A11:AU11").Select
Selection.Insert Shift:=xlDown
Range("U13:AA13").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=Application!R[10]C[3]"
Range("I14:Q14").Select
ActiveCell.FormulaR1C1 = "=Application!R[119]C[9]"
Range("K24:O24").Select
ActiveCell.FormulaR1C1 = "='Consumer Loan Request'!R[-4]C[-1]"
Range("P24").Select
Rows("20:20").RowHeight = 3
Rows("30:30").RowHeight = 3
Range("j12:w12").Select
Sheet14.Protect Password:="Loan101"
End Sub
The code needs several fixes to be optimized, but the way SO works is to answer one question each time so that future users will find it useful as well. I will hence, in this thread, limit to answer your question: "How can I make a macro starting from the row where the previous one ended?"
The trick is to store the value of the row in a global variable, i.e. a variable whose scope is not limited to the single macro but to the whole module. In order to reach this (please adapt this small example to your code, since nobody can see the project so this can only be a hint):
1) Declare the global variable on top of all macros:
Dim rowStart As Long
2) Write your macros:
Dim rowStart As Long
Sub Macro1()
'code
End Sub
Sub Macro2()
'code
End Sub
So, here's the hint. Make your first macro perform some operations, for example putting 20 random numbers into the first 20 cells of the column "B":
Sub Macro1()
For j = rowStart To rowStart + 20 'rowStart has not been modified yet, so it's equal to 0 by default
Sheets("YourSheet").Range("B" & j+1).Value = Rnd() '"B" & j will be "B1" the first time, "B2" the second time etc.
Next j
rowStart = j 'here we store the value "20", i.e. the last value of j, into the global variable so that we can reuse it after
End Sub
Once this is done, let's say that your second macro will want to add other 10 random numbers to the existing list already created from the first macro. To do this:
Sub Macro2()
For j = rowStart To rowStart + 10 'now rowStart = 20, so we will start from the last row where the previous macro ended!
Sheets("YourSheet").Range("B" & j+1).Value = Rnd()
Next j
rowStart = j 'now we store the value "30", so next time you want to know the last row used, you will know it
End Sub
Unfortunately, your code is very complicated to read and understand what it's doing (I imagine it's recorded since I still see the ActiveWindow.ScrollColumn command coming from you moving up and down your spreadsheet manually).
So, my suggestion is to start from these two tips:
Global variables will help you to keep in memory the last rows touched from your macros;
String merging (Range("B" & j)) will help you to reference objects dinamically.
In general, use the Macro recorder to learn about the commands, but write your own code: it might take a bit longer in the beginning, but you will make sure that the code is flexible and able to adapt to any kind of similar problem. Here's a good tutorial to get started with.

Changing columns for Excel userform comboboxes

Good morning,
I am in yet another rut and need some help. I have created a user form that allows a user to delete an entire rows worth of data on a second sheet (rawdata). Everything works fine using the code below, however the combo box ONLY shows the row number. I am in desperate need of changing the column so it will show the project names of the rows that need to be deleted.
Example:
Row: Project
1 Alpha
2 Beta
I would like the combo box to show Alfa and Beta and have the user be able to select the row they would like to delete based on that criteria.
The code below unhides and then hides the sheet that I want this deletion to occur on. This was done with purpose.
Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim lRw As Long
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetVisible
'get the row number. add 1 because ListIndex starts at zero
lRw = Me.ComboBox1.ListIndex + 1
ActiveWorkbook.Sheets("RAWDATA").Select
Cells(lRw, 1).EntireRow.Delete
ActiveWorkbook.Sheets("RAWDATA").Visible = xlSheetHidden
End Sub
Private Sub CommandButton1_Click()
End Sub
Private Sub UserForm_Initialize()
'assumes data starts in A1 and has a header row
Me.ComboBox1.List = ActiveWorkbook.Sheets("RAWDATA").Cells(1, 1).CurrentRegion.Offset(1).Value
End Sub
Thanks for the help!
Change .Cells(1, 1) to .Cells(1, 2)
The Cells() method gives the code co-ordinates to a specific range using the row and the column number like so:
Cells(r, c)
so in your original code, the .Cells(1, 1) points to "A1" and then uses .CurrentRegion to get all cells within the region of A1.
By replacing the column number and using .Cells(1, 2) we tell it to look at "B1" instead - therefore shifting the column over to the right.
EDIT:
You could apply this logic to the Offset(r, c) function to shift the returned value over by 1 column - so:
.Cells(1, 1).CurrentRegion.Offset(1, 1)
This will more than likely be the culprit as the .Cells() method will point to a specific cell, but the .CurrentRegion() method will return the same result regardless unless we offset it.

Resources