Drag down formula from last row with formula to next, until "A" ends - vba

My problem is that i have big Excel file and need to drag down formulas as i copy values, I explain it further with an example table:
I have a vba script running in the background that on execution copys missing data from another datasheet, this copy procedure is in a For Each and in there i want to also copy down the formulas from the last row where there are formulas from the Range D:F.
For those who want to see it, this is my copy function:
If rowNotFoundMain = True Then
If cell2.Offset(0, 7).Value = 0 Then ' Offset 7, sind die Ehrungen
cell2.Interior.Color = vbYellow
Else
Set rFirstBlank = ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Offset(1)
ws2.Range(cell2.Address, cell2.Offset(0, 2).Address).Copy
rFirstBlank.PasteSpecial
' INSERT DRAG DOWN FUNCTION HERE '
End If
End If
I looked up various ways on the Internet but didn't find suitable answers for my problem.
Why am I asking this directly for an answer and can't help anyhow ? Simple, I can program in theory but never used VBA, my boss wants this from me and I am searching everything together from the web.
I hope my explanation makes sense and if you need further Information, please don't hesitate and ask away.
Have a nice week!

Select D2:F6 range
Double-click black square in the bottom right hand side corner
Your formulas are copied!
Now do the same while recording a macro
Check recorded result and adopt to your case.

Related

VBA cut and paste row to another sheet based on cell value and delete empty row

Hi I have almost no experience coding. I took a crappy high school course, but I need to create code to cut rows from one sheet to the next empty line on another "archive" sheet when a condition (ex cell F) has data in it. Then delete the now empty row that was cut from. Ideally this would be done once the worksheet was saved as well. I've found a few things that come close but for some reason they won't work
I'm using Excel 2016 if that helps
This solution was close but didn't work for me
Any help would be greatly appreciated thanks.
In the solution you link in your question, changing
If .Cells(n, "G") = "" Then
to
If .Cells(n, "G") <> "" Then
should accomplish your objective of copy-paste-deleting rows with data in the given column "G"

Excel macro to normalize data

I am currently trying to create a macro for Excel in which a column containing certain values (numbers basically) will be displayed in a new column in a normalized way (highest number = 1, lowest number = 0).
Usually, I would just use the formula:
=(J2-MIN($J$2:$J$XXX))/(MAX($J$2:$J$XXX)-MIN($J$2:$J$XXX))
However, as the length of the column is dynamic and will change for each set of values, I cannot enter a value for XXX.
Now, I found out how to have a dynamic range (e.g.: numRows = Range(Selection, Selection.End(xlDown)).Rows.Count) but I did not manage to merge both functions.
I found a thread already in this site about normalization of data but I think it was a bit of a different story and this one here should be simpler.
I would appreciate any help! As I just started working with macros (2h ago) I would also appreciate if this will be in simple language.
EDIT:
First of all, thanks for the quick reply!
I naively tried making it work with this code:
Sub Normalize_TEST()
'
' Normalize_TEST Makro
'
'
Range("A1").Select
numRows = Range(Selection, Selection.End(xlDown)).Rows.Count
Range("K2").Select
ActiveCell.FormulaR1C1 = "=(("J2")-MIN($J$2:$J$numRows$))/((MAX($J$2:$J$numRows$)-MIN($J$2:$J$numRows$))
Range("K2").Select
Selection.AutoFill Destination:=Range(Cells(2, 11), Cells(numRows, 11))
End Sub
But it is not working and I get an error message ("error of compiling").
I just realize now that you are absolutely right, I don't even need VBA. Your line of =(J2-MIN($J:$J))/(MAX($J:$J)-MIN($J:$J)) works fine. I wasn't aware that with $j:$J it realizes to not include empty cells.
I simply used this code now for cell K2 and then did a VBA autofill function for the rest.
I think this can be closed.
Thank you #tigeravatar for your super quick help!

Do While ActiveCell <> Range

I have this VBA excel macro code
Sub fillcells()
Range("J14").Select
Do While ActiveCell <> Range("J902")
ActiveCell.Copy
ActiveCell.Offset(6, 0).Select
ActiveCell.PasteSpecial
Loop
End Sub
At first it was working fine but now sometimes when I try to run the macro the loop suddenly stops at cell J242, other times is arising an error 'mismatch type' and sometimes the macro just select cell J14 without doing the loop
Not sure what you want to do, but (as noted in the comments to your OP), don't use .Select/.Activate. The following should do what (I think) you wanted:
Sub fillcells()
Dim i& ' Create a LONG variable to count cells
For i = 14 To 901 Step 6
Cells(i, 10).Offset(6, 0).FormulaR1C1 = Cells(i, 10).FormulaR1C1
Loop
End Sub
This will loop from cell J14 to J901, copy/paste* to a cell 6 rows offset.
* Note I didn't actually copy/paste. Since your original code used PasteSpecial, I'm assuming you just want the values pasted. In this case, you can set the two ranges/cells equal.
Just an addition to what #BruceWayne already said: whenever you have this typical phenomenon that something happens only "sometimes" it is often a case of using keywords such as Active or Current or Selection. These are not specific but change each time that you call the macro. Whatever you have selected is the starting point. You might even start clicking around and thus change Selection while the macro is running. In short, you should start coding explicitly and don't allow VBA / Excel to assume / make the decision for you.
Let's start with Range("J14").Select. This line of code asks VBA to make already two assumptions:
If you have several Excel files open. Which Excel file should it start with?
Within the file there might be several sheets. On which of these sheets should J14 be selected?
Explicit coding means that you (hopefully at all times) be very specific what you are referring to. So, instead of just stating Range("J14") you should use:
ThisWorkbook.Worksheets("SheetNameYouWantToReferTo").Range("J14")
But is pointed out in the other answer, this is not even necessary in this case. Rather loop the rows as shown and use:
ThisWorkbook.Worksheets("SheetNameYouWantToReferTo").Cells(i, 10).Offset(6, 0).Formula = ThisWorkbook.Worksheets("SheetNameYouWantToReferTo").Cells(i, 10).Offset(i, 10).Formula
Since this is a bit lengthy you can shorting it by using a With statement:
With ThisWorkbook.Worksheets("SheetNameYouWantToReferTo")
.Cells(i, 10).Offset(6, 0).Formula = .Cells(i, 10).Formula
End With

sumifs to loop all sheets

I have been searching different forums and cant seem to find my answer.
I have rather basic VBA knowledge and build most of my code from bits online!
Regardless of cell references as I would be able to work these out at a later date.
Please can you let me how I would make a sumifs formula reference across multiple sheets.
This is being build into a template and there would be a different number of sheets with different names each time it is run so I would be not be able to reference the sheets.
sorry thats a bit vague
thanks in advanced
Thanks, so for anyone else who needs this, this is how it was done in full
my original formula was
"=SUMPRODUCT(SUMIF(INDIRECT(" '"&Invoices&"'!"&"A2006:A3005"),A3,INDIRECT("'"&Invoices&"'!"&"B2006:B3005")))"
this worked when putting straight into a cell but as you can see, when adding it to VBA it reads it as a comment. To fix this, every time you use a " you need to add extra " as shown bellow (apart form before the" = at the start and after the )" at the end of the formula)
*****'list all the sheet names in cell AI1 of the sheet summary*****
For i = 1 To Sheets.Count
Sheets("Summary").Range("AI1")(i, 1).Value = Sheets(i).Name
Next i
***'clear the first 3 entries in AI as i didnt need the first three sheet names***
Sheets("Summary").Select
Sheets("Summary").Range("AI1:AI3").Clear
***'select the first sheet name, which is in AI4 as we cleard the first 3 to the last used cell, e.g Ctrl.Shift.down***
Sheets("Summary").Activate
Sheets("summary").Range(ActiveSheet.Range("AI4"), ActiveSheet.Range("AI4").End(xlDown)).Select
***' Name the range invoices***
Selection.Name = "Invoices"
' ***Formula to do a sumIf looping all the shets in the named range Invoices***
Sheets("summary").Range("B3").Formula = "=SUMPRODUCT(SUMIF(INDIRECT(""'""&Invoices&""'!$A$2006:$A$3005""),$A3,INDIRECT(""'""&Invoices&""'!B$2006:B$3005"")))"

How to copy data to an existing sheet without losing any data types or formatting

Here's the problem. I tried to build a simple regression test.
I have two sheets linked together and some other vba functionality.
In order to test regularly, I used copy-move-> make a copy and created precise copy of good sheet a then repeated with good sheetb and took a screenshot of how they should look when working correctly after I run my code.
All I have to do is copy in this known data, run the code then check the output against my screenshot. Or so I thought.
When I ran the code lots of things just changed themselves despite the fact that I a coping a range of data forma clone of this sheet. using
range( a ).value = range( b).value '(pseudo-code)
1 thing I had column with age/weight like this 35/12-11 now its formatted as a date and no fiddling with data types can recover it.
HOW CAN I STOOP eXCEL DOING THIS KIND OF MADNESS?
next thing the text i.e. names of people in a general column show up as 0 in the destination column. Why? it is coming from a clone of the one its always come form without a problem.
Can anyone shed light on this. it's devastaing trying to write a olution up agianst this kind of thing, but I''ve already invetsed a lot in it.
Any help gladly accepted
You can try something like this. Just don't forget to declare your variables. This also assumes that Row A is where your information is
With shttocopy
'finds last row with information
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'finds last column with information
LastColumn = .cells(LastRow , .Columns.Count).End(xlLeft).Row
'copies cells with information regarding the customer information
'pastes those copied cells into the sheet you want the information moved to
.Range(LastRow:LastColumn).Copy _
destSheet.Cells(Rows.Count, "A").End(xlUp).Offset(1)
end with