Excel VBA code for simple formula between cells - excel

I need a code for these orders:
Go to first blanks cell in column C.
Calculate the number in column D minus 14 and do it as long there is a number right to the cell in D (there is blanks in column D)
Copy the result in column C and paste it as values to Column B.
note: I need it to stay in original rows.
OP_wb.Sheets("Optic Main").Activate
Dim FirstRow As Range
Dim LastRow As Range
Set FirstRow = Range("C1").End(xlDown).Offset(1, 0)
Range("E1").End(xlDown).Offset(0, -2).Select
Range(FirstRow, LastRow).FormulaR1C1 = "=(c4-14)"`

try this:
1.
Set FirstRow = Range("C1").End(xlDown).Offset(1, 0)
Set lastrow = Range("E1").End(xlDown).Offset(0, -2)
Range("E1").End(xlDown).Offset(0, -2).Select
2.
Range(FirstRow, lastrow).FormulaR1C1 = "=(c4-14)"
3.
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & lastrow)
hope this helps

Related

Is there a way to loop through a range and return a formula in the cell next to it if there is data?

I have been researching through google and stack overflow to see if I can find a similar problem, but I haven't seen it yet.
I am trying to tell excel to look at an entire range (entire column F) within my dataset (The dataset amount changes often). This column gets generated with Vendor numbers or blanks. I need to loop through the entire column and each cell and write a logical statement where If there is a vendor number in the specific cell within Column F, return a VLOOKUP in Column G in the same row that looks up the vendor number in column F. And if there is no data in column F in a particular cell, I need it to insert an equals sign and point to column Q but in the same Row. So for example, if F13 is Blank, I need to insert in cell G13 "=Q13" and so on for each cell. So if F14 is Blank, G14 would have to be "Q14". And if there is a vendor number in F20, I would need to return a VLOOKUP in G20 such as "=VLOOKUP(F20,Mapping!$A$4:$B$1000,2,FALSE)".
This is what I currently have so far, but I am really struggling on how to loop through and tell excel to point to the same row but different column within the code.
Dim LastRow As Long
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "F").End(xlUp).Row
Dim CL As Range
Dim Rng As Range
Set Rng = Worksheets("Sheet1").Range("LastRow")
For Each CL In Rng
If CL.Value = "" Then
CL.Value = "=VLOOKUP("
If I am not on the right track, any help is appreciated.
Something like this?
Dim ws As Worksheet
Dim rc As Long, i As Long
Set ws = ActiveSheet
rc = ws.Range("F" & Rows.Count).End(xlUp).Row
For i = 1 To rc
If ws.Range("F" & i) <> "" Or Not IsEmpty(ws.Range("F" & i).Value) Then
ws.Range("G" & i).Formula = "=VLOOKUP(F" & i & ",Mapping!$A$4:$B$1000,2,FALSE)"
Else
ws.Range("G" & i).Value = ws.Range("Q" & i).Value
End If
Next

How to have conditional sumif formula throughout a range?

i'm trying to do a sumif of cells in column K to the right whenever there's a cell value in column J = "subtotal". Right now my code isn't working and i'm not sure if it's because i wrote my sumif statement wrong. My range starts at row 13 and the last row varies. My condition to sum is to sum the cells in column K as long as the cells in column B are the same.
Please see below for my code! Would appreciate any help for this...I'm getting an optional error but i'm not 100% sure why.
enter code here
dim startrow as long
dim groupstartrow as long
groupstartrow = 13
with ws.name("sheet 1")
mergelastrow = .Cells(.rows.Count, 2).End(xlUp).Row
'every time a cell in column J says GL total,
'add a formula next to it in column K that sums cells from i from beginning of the range
'as long as i in column b is equal to the b on the same row
'as the cells being summed is my goal
For i = startrow + 1 To mergelastrow + 1
If Cells(i, 10) = "subtotal" Then
Range.Cells(i, 11) = "=SUM(" & Cells(groupstartrow, 11).Address & ":" & Cells(i - 1, 11).Address & ")"
groupstartrow = i + 1
End If
end with
end sub

subtract between last cell from a column and last cell from another column, and put the result in a specific cell

I need some vba code to subtract between last cell from column B and last cell from column D and put the result in cell N1. The position of last cell, in both columns may variate.
Thx
This code does the trick for you. It takes the last cell of both column B and D and it subtracts their value on the N1 cell:
Sub MySub()
Dim lastBRow As Long
Dim lastDRow As Long
lastBRow = Range("B" & Rows.count).End(xlUp).Row
lastDRow = Range("D" & Rows.count).End(xlUp).Row
Range("N1").Value = Range("B" & lastBRow).Value - Range("D" & lastDRow).Value
End Sub

if column A has text and column G is blank then copy row to new spreadsheet

I am trying to create a summary list for people in a downstream application to feed several of my production machines. Each machine is going to have their own tab to request material, and I want all of their requests to be summarized on one tab (called "Core_Cutter_List").
So basically I am trying to create a VBA that will copy over a row from spreadsheet "2" into the next blank line on spreadsheet "Core_Cutter_List". I want it to copy if there is text in column A and column G is blank. I have limited knowledge of VBA. The code that I found was able to only test for one of my criteria which was that column G is blank, but basically it runs through every single cell on my file. Do you know how I can add the other criteria of column A having text in it so that it doesn't look through every cell on my sheet? Thanks for any help!
Sub Test()
'
' Test Macro
'
Sheets("2").Select
For Each Cell In Sheets(1).Range("G:G")
If Cell.Value = "" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets("Core_Cutting_List").Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets("2").Select
End If
Next
End Sub
If you need two conditions, then you should write them carefully in the IF statement with And:
Something like If cell.Value = "" And Len(cell.Offset(0,-6)) Then should be workable.
Using Select is a bit not advisable, but it works at the beginning - How to avoid using Select in Excel VBA
The Sub bellow does the following
Determine the last used row in Worksheets("2") based on values in column A
Determine the last used col in Worksheets("2") based on values in row 1
Determine the last used row in Worksheets("Core_Cutter_List") based on values in column A
Loop through all used rows in Worksheets("2")
If the cell in col A is not empty And cell in col G is empty
Copy entire row to next empty row in Worksheets("Core_Cutter_List")
Increment next empty row for Worksheets("Core_Cutter_List")
Loop to the next used row in Worksheets("2")
Option Explicit
Public Sub CopyRows()
Dim ws1 As Worksheet, ws2 As Worksheet, ws1r As Range, ws2r As Range
Dim ws1lr As Long, ws1lc As Long, ws2lr As Long, i As Long
Set ws1 = ThisWorkbook.Worksheets("2")
Set ws2 = ThisWorkbook.Worksheets("Core_Cutter_List")
ws1lr = ws1.Range("A" & Rows.Count).End(xlUp).Row 'last row in "2"
ws1lc = ws1.Cells(1, Columns.Count).End(xlToLeft).Column 'last col in "2"
ws2lr = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1 'last row in "Core_Cutter"
For i = 1 To ws1lr
If Len(ws1.Cells(i, "A")) > 0 And Len(ws1.Cells(i, "G")) = 0 Then
Set ws1r = ws1.Range(ws1.Cells(i, 1), ws1.Cells(i, ws1lc))
Set ws2r = ws2.Range(ws2.Cells(ws2lr, 1), ws2.Cells(ws2lr, ws1lc))
ws2r.Value2 = ws1r.Value2
ws2lr = ws2lr + 1
End If
Next i
End Sub
My test file
Worksheets("2")
Worksheets("Core_Cutter_List")

Excel - copying text from one cell to another without deleting original content

Basically I have the following scenareo:
2 columns, with 600 rows of data.
I need to copy the data from column 2 and place it at the end of the content in column1 for the same rows. This would result in column 1 having its original content plus the additional content of column 2.
Any information in how I can do this will be greatly appreciated.
Thanks in advance!
Here's a VBA in a simple form. Just create a macro, add these lines to it. Then select your original column (what you're calling column 1), and run the macro.
a = ActiveCell.Value
b = ActiveCell(1, 2).Value
ActiveCell.Value = a + b
The bracketed cell reference is a relative statement - 1, 2 means "same row, one column to the right" so you can change that if you need. You could make it loop by expanding thusly:
Do
a = ActiveCell.Value
b = ActiveCell(1, 2).Value
ActiveCell.Value = a + b
ActiveCell.Offset(1, 0).Select
If ActiveCell.Value = "" Then
Exit Do
End If
Loop
That loop will carry on until it finds a blank cell, then it'll stop. So make sure you have a blank cell where you want to stop. You could also add extra characters into the line that combines.. so in the above example it's ActiveCell.Value = a + b, but you could make it ActiveCell.Value = a + " - " + b or anything else that may help.
This should take the values from column 2 and place them sequentially at the bottom of column 1.
Sub test()
Dim rng1 As Range
Dim rng2 As Range
Dim cl As Range
Dim r As Long
Set rng1 = Range("A1", Range("A1048576").End(xlUp))
Set rng2 = Range("B1", Range("B1048576").End(xlUp))
r = rng1.Rows.Count + 1
For Each cl In rng2
Cells(r, 1).Value = cl.Value
r = r + 1
Next
End Sub
Just keep it simple. Here is the code.
Sub copyCol()
Dim lastRow As Long
lastRow = Range("A65000").End(xlUp).Row
Range("B1:B" & lasrow).Copy Range("A" & lastRow).Offset(1, 0)
End Sub
As this question received a number of views (10,000+) I thought it was important to also share another and far simpler solution:
In cell C1 use the formula:
=(A1 & B1)
This will copy the content of cell A1 and B1 into cell C1. Drag the formula to all other rows (row 600 in my case).
Then copy the column and paste using 'values only'.
You will then have cells in column C containing the content of column A and column B in a single cell on a row-to-row basis.

Resources