Macro to copy and change set numbers Excel - vba

so am working on a spreadsheet to simplify logging of data. However it's quite a tedious process of copy, paste & change. Is there a way I can create a Macro to copy the following:
=IF(Trade2!I16=99,0,Trade2!I15)
Paste it the cell directly below but as
=IF(Trade3!I16=99,0,Trade3!I15)
Beyond this it needs to be copied 100 times. Am at 15 typing it manually each time and losing the will to live haha
This seems insanely simple but everything am trying is leading to a massive mess, am good at getting the spreadsheet side working but the coding isn't my strong point.
Any advice would be greatly appreciated

If you use this formula:
=IF(INDIRECT("Trade"&ROWS(A$1:A1)&"!I16")=99,0,INDIRECT("Trade"&ROWS(A$1:A1)&"!I15"))
this returns:
=IF(Trade1!I16=99,0,Trade1!I15)
But you can now copy it down, and the sheet number increases.
The ROWS formula functions as a counter as you copy it down. The INDIRECT formula returns a reference to a sheet.

not sure what you mean but try this
Sub Main()
Dim i As Long
For i = 1 To 10000
Range("A" & i).Formula = "=IF(Trade2!I" & i + 1 & "=99, 0, Trade2!I" & i & ")"
Next i
End Sub

Related

Excel vba running differently on two computers

I am pulling my hair out over this as I cannot see why this isn't working. Any help or guidance would be greatly appreciated.
I have inherited a macro in Excel which runs differently depending on which PC we run it on. Essentially, the macro inserts blank rows into a table of data (which should also have conditional formatting to make it blue to break up the table and make it easier to read). On my PC instead of a blank row being entered, it is a row of #ref errors and loses the formatting.
We have a different macro assigned to a button which runs three macros one after another. The one I have the issue with is the third of these three. When I run the macros as three separate events it works, but together it has the #ref errors.
Sorry for the small snippet of the file, but it is sensitive information.
I have tried adding a pause in between the second and third, but this doesn't help.
Are there some security settings which I should check?
The macro looks at a formula in column A and looks for the number 5 and inserts a row after this line. The code is as follows:-
Sub Main()
Dim r As Range
Dim i As Long
i = 1
Do While Range("A" & i).Value <> ""
If Left(Range("a" & i), 2) = "5" Then
i = i + 1
Rows(i).Insert
Range("A" & i).Value = Range("a" & i - 1).Value
End If
i = i + 1
Loop
End Sub
What I am struggling to get my head around is that it works in isolation, but not when run with other macros. For completeness the macro which runs the three has the following code:-
Sub runall_CVR()
Application.ScreenUpdating = False
Call hiderows_CVR
Call Delete0s_cvr
Call Main
Application.ScreenUpdating = True
End Sub
Many thanks
James

VBA crashing after autofill line of code

I have a set of code below. The "NPPA_Alive_Row" is a string = 60000. Before the autofill line, the underlying workbooks will save and close fine (I have stepped through this). However after this line, no open workbooks will close or save (I have tried calculating the workbook in question after this line at still doesn't work)
WB_Checking_NPPA_Alive.Sheets("NPPA_Alive").Cells(2, 81) = "=VLOOKUP(B2,NPPA_Alive.xlsx!B2:BR" & NPPA_Alive_Row & ",68)"
WB_Checking_NPPA_Alive.Worksheets("NPPA_Alive").Activate
WB_Checking_NPPA_Alive.Worksheets("NPPA_Alive").Range("CC2").Select
Range("CC2").AutoFill Destination:=Range("CC2:CC" & NPPA_Alive_Row)
Does anyone have any ideas to why this is?
Without seeing more code, it's hard to say for sure. If the file is pretty big, the processing time to calculate 60k vlookups might be what's causing the crash. If you manually copy the vlookup down, does it crash too?
You could simplify those 4 lines of code a few ways, which may or may not help
Sheets("NPPA_Alive").Range(Sheets("NPPA_Alive").Cells(2, 81), Sheets("NPPA_Alive").Cells(NPPA_Alive_Row, 81)).Formula = "=VLOOKUP(B2,NPPA_Alive.xlsx!$B$2:$BR$" & NPPA_Alive_Row & ",68)"
or
WB_Checking_NPPA_Alive.Sheets("NPPA_Alive").Range("CC2:CC" & NPPA_Alive_Row).Formula = "=VLOOKUP(B2,NPPA_Alive.xlsx!$B$2:$BR$" & NPPA_Alive_Row & ",68)"

How to select last cell in a column and insert it into another sheet in last open cell

Just started coding with excel in VB. The problem I am having is this:
I want to copy the last cell in a column on one sheet, then paste it into the next open cell in a column on the next sheet. If it is at all possible can I have it do this automatically instead of having to call the function or click a button every time?
Here is what I have so far:
Sub UpdatePromoCalendar()
Dim LR As Long
Dim TR As Long
LR = WorksheetFunction.Max(20, Range("A" & Rows.Count).End(xlUp).Row + 1)
TR = WorksheetFunction.Max(20, Range("O" & Rows.Count).End(x1Up).Row - 1)
Sheets("Sheet1").Range("O" & TR).CopySpecial xlCopyValues
Sheets("Sheet2").Range("A" & LR).PasteSpecial xlPasteValues
End Sub
Now it works using the paste option above with the LR. But I can't get it to copy the last populated cell in the column.
Sorry if it's a simple fix I am new to using excel macros and VB in excel.
Thanks for the help in advance!
Instead of using a macro I decided to redo my spreadsheet and go with a pivot table with slicers. I created separate sheets with each item and added a new column called price master. This way I could then filter the results with the slicers showing a cleaner and easier to use template.
The macro was a lot of trouble and basically was taking the place of the slicer which was a waste of time doing in the long run.

Looping the whole cells to change a specific formula issue

I'm writing a function to change an entire column to new values using a formula, here's the code I'll elaborate more on the idea down there.
The problem is that it hangs and I have to rerun Excel and I'm not sure why.
Sub Button2_Click()
Dim i As Long
For i = 2 To Rows.Count
Cells(i, 4).Formula = "=B" & i & "+6*3600000/86400000+25569"
Next i
End Sub
So what's this about? I'm changing the fourth column to excel time because what I have in column B is epoch time, and this is the formula I'm using, it works with my case if I tried one by one, but for some reason it won't work as a whole. I'm not sure what's done wrong? But I'd appreciate your help.
Writing to cells one-by-one is very slow.
Writing formulas one-by-one is slower still, because each must be evaluated before Excel accepts them as formulas.
Doing this a million times can literally freeze Excel.
The solution is to write them all in one shot (no loops):
Sub Button2_Click()
[d2:d1048576] = "=B2+6*3600000/86400000+25569"
End Sub
' Another way of doing mass calculation is by using copy and paste method.
It will be better to convert the columns into values so that the sheet won't calculate again and again. It helps to prevent the sheet from hanging issues
Sub Button2_Click()
Range("D2").Formula = "=b1" & "+6*3600000/86400000+25569"
Range("D2").Copy
Range("D2:d1048576").PasteSpecial xlValues
Application.CutCopyMode = False
Range("D:D").Value = Range("D:D").Value
End Sub

VBA VLOOKUP Convert to Values Gives #N/A

I'm having some trouble with VLOOKUP in my VBA. Here's an example of the code I'm using:
Sub Macro15()
'
' Macro15 Macro
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range("B1:B" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-1],'https://internal_sharepoint_address
/[Vendor_Information.xlsx]Sheet1'!R3C3:R150C18,4,FALSE)"
Range("C1:C" & LR).FormulaR1C1 = _
"=VLOOKUP(RC[-2],'https://internal_sharepoint_address
/[Vendor_Information.xlsx]Sheet1'!R3C3:R150C18,5,FALSE)"
With Range("B1:C" & LR)
.Value = .Value
End With
End Sub
The problem is that the values in Columns B & C (the VLOOKUP formulas) return a value of #N/A.
However, if I stop the code before converting the formula to values (the "With Range("B1:C" & LR)" line), the VLOOKUP formula returns the correct values.
Also strange - if I clear the contents of Columns B & C and re-run the above code, the values return fine. If I try to add a second cycle to the VBA, however, it does NOT work.
Any wisdom that anyone can provide would be a huge help. I've been stuck on this for a long time, and I'm just at my wit's end.
Thanks all,
David
You'll probably need to add in a step that runs a calculation cycle before you try to replace with the value:
Application.Calculate
Edit from comment: I would imagine that retrieving lookup data from a linked workbook on a Sharepoint site would take awhile. Maybe add some delay loops? Can you make two separate macros (one ending with the formulas, and a second one starting at the Paste Values), and run them separately with a pause in between?