How to fix a sum formula that changes ranges - excel

Sub formu
Range(“d” & Rows.count).end(xlUp).offset(2,0).formular1c1 = “=sum(R[-9]c:r[-1]c)”
End sub
Currently this is my formula but it changes on a weekly basis so I t could be R[-14]c:r[-1]c the next week or R[-6]c:r[-1]c the next week. How do I get my formula to change weekly so I don’t have to manually re sum the cell?

Say the following would be your current weeks data:
The following code would add a formula in the cell below the last cell:
Sub formu()
Dim lr As Long
With Formulas
lr = .Cells(.Rows.Count, 4).End(xlUp).Row
.Cells(lr + 1, 4).FormulaR1C1 = "=sum(R[-" & lr & "]c:r[-" & lr - (lr - 1) & "]c)"
End With
End Sub
Whereas the sheet reference is the sheet codename containing the data. Output:
The formula in that cell is currently:
=SUM(R[-10]C:R[-1]C)
You can find a sheet's CodeName in the project explorer and give it a meaningfull name to reference directly :)

First of all, is important to know if your weekly report always put the information in te same order. For example all the numbers start in the Range("A2"). If that the case i recomend you to work whit TagNames. Asuming that the data you need to sum start in the Range("A2")
Dim Col as integer
Dim sRow, eRow as long 's = start, e = end
Col = Range("A2").Column 'Col = 1
sRow = Range("A2").Row 'sRow = 2
eRow = Range("A2").Rnd(xlDown).Row 'eRow = row of the last cell with information to sum
Range(Cells(sRow,Col),Cells(eRow,Col)).Name = "range2Sum" 'Yo assign a tagname
'The you can use what you have done
Range(“d” & Rows.count).end(xlUp).offset(2,0).formula = “=sum(range2Sum)” 'use that tagname that is visible in excel

Related

If value matches from list, insert corresponding value below

Attempting to write some vba but not having much luck. I have column A with a whole list of values that I am counting and looping through. For Each value in column A, there can be a match in range C:D. If a value in column A matches a value in column C. I want to insert the corresponding value in column D below the Column A value. I am not too certain on what my IF then statement should look like. I have my counter and loop... I am just unsure where to go with the middle portion of the code.
Sub SetListOrder()
Dim wp As Worksheet
Dim ef As Long
Set wp = Workbooks("Packing Slip FIXED").Worksheets("Locate Order")
ef = wp.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To ef
IF (UNSURE WHAT TO PLACE HERE!) THEN
Next i:
End Sub
Edit: adding sample data
Sample Data screenshot
In this example, I would like to insert a new row under the value in "A" where A=C. ie. Range in column "A" = Range in Column "C". I would like to then insert the value from "D". The new order in rows 4-6 would be:
Range
Order Group 1
2604291
I already have written the code to manually move my sheets around to follow the specific order once I am able to get the names in said order.
I agree with #BigBen that the simpler approach would be to insert a formula in column D that only replicates the column A value when a match is detected. Such a formula would probably look like the following -
=IF($A1=$C1,$A1,"")
This would be copied into cell D2 of your column and copied down as far as needed.
However, if you did want to achieve this with VBA and I have noted you used the word insert a value (as opposed to simple enter a value or copy & paste a value) then this could be your approach -
Sub SetListOrder()
Dim wp As Worksheet
Dim ef As Long
Dim i As Long
Set wp = Workbooks("Packing Slip FIXED").Worksheets("Locate Order")
ef = wp.Range("A" & Rows.Count).End(xlUp).Row
For i = ef To 1 Step -1
If wp.Range("A" & i).Value = wp.Range("C" & i).Value Then
wp.Range("D" & (i + 1)).Insert xlShiftDown
wp.Range("D" & (i + 1)).Value = wp.Range("A" & i).Value
Else
End If
Next i
End Sub
This approaches the problem in reverse by going up your column instead of going down. Note that by inserting your data, will cause each previous value to move down as well. If you don't want this, then simply erase the .Insert line and it will enter the value instead of inserting a cell.
Modify the below code and use:
Formula:
=IFNA(VLOOKUP(A1,$C$1:$D$5,2,0),"Missing")
VBA Code:
Option Explicit
Sub test()
Dim rngSearch As Range, rngFound As Range
Dim LastRowA As Long, LastRowC As Long, i As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
LastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row
Set rngSearch = .Range("C1:D" & LastRowC)
For i = 1 To LastRowA
Set rngFound = rngSearch.Find(.Range("A" & i).Value, LookIn:=xlValues, Lookat:=xlWhole)
If Not rngFound Is Nothing Then
.Range("B" & i).Value = .Range("D" & rngFound.Row).Value
Else
.Range("B" & i).Value = "Missing"
End If
Next i
End With
End Sub
Result:

copy till last row from specific row number with cell reference

I have the below code where I need to make it work as below.
copy from row 10 till last row with value.
the last row will be with reference to column N starting from cell N10..
any suggestions from SO team?
wbSource.Sheets(SITE_TEMPLATE).Rows(10).EntireRow.Copy wbMaster.Sheets(SITE_TEMPLATE).Range("A" & insertRow2)
insertRow2 = insertRow2 + 1
Try this:
Sub test()
Dim LastRow As Long
With Workbooks("wbSource").Worksheets("SITE_TEMPLATE")
LastRow = .Cells(.Rows.Count, "N").End(xlUp).Row
.Rows(10 & ":" & LastRow).EntireRow.Copy wbMaster.Sheets(SITE_TEMPLATE).Range("A" & insertRow2)
insertRow2 = insertRow2 + 1
End With
End Sub

Replace values in rows after certain GAP of row numbers

I need to replace the value in a certain row that is repeated after a certain amount of rows. I have multiple Excels I need to do this.
Current data:
7th row: IN
16th row: IN ( comes after 9 rows)
25th row: IN ( comes after 9 rows)
I need these values to be replaced by OUT.
I did some research and seems like I could use macros but I am not familiar with macros.
Can anyone please help with macros with a loop or suggest any other ideas?
A simple excel formula can work per sheet, and of course a macro can work
The excel solution, starting in B2
=IF(AND($A1 = "IN",$A1 = $A2),"Out",$A2)
This formula will replicate your original column, with the fix. then a simple copy & paste as values of column B to A should work
A VBA solution, for which you need to select the relevant column:
Sub fixOut()
Dim cell As Object
For Each cell In Selection
If cell = "IN" AND cell = cell.OffSet(-1, 0) Then cell = "Out"
Next cell
End Sub
For Excel Office 365, create a macro going under View -> Macro -> View Macros -> You input a macro name and then press Create button.
A text editor screen should appear, the macro should be the following:
Sub test_macro()
Dim searching_string As String
searching_string = "IN"
replacing_string = "OUT"
searching_column = "A"
minimum_distance_to_be_modified = 3
previous_found_row = -1
row_number = 10000
For i = 1 To row_number
If Range(searching_column + CStr(i)).Value = searching_string Then
If i - previous_found_row <= minimum_distance_to_be_modified And previous_found_row <> -1 Then
Range(searching_column + CStr(i)).Value = replacing_string
End If
previous_found_row = i
End If
Next
End Sub
Set your searching_string, searching_column, minimum_distance_to_be_modified, replacing_string and you should be fine!
I did a test with the settings that you find in the snippet and this was the result:
Hope that this is going to help you.
I solved my problem via macros and VB code.
Sub Macro1()
'
' Macro1 Macro
'
Dim i As Integer
i = 7
Do While i <= Cells(Rows.Count, 1).End(xlUp).Row
Cells(i, 1).Value = "Out"
i = i + 9
Loop
End Sub
Add this code to new macros and run the macros if anyone is having a similar problem to mine.
You could try:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long
Dim arr As Variant
'Change target worksheet if needed
With ThisWorkbook.Worksheets("Sheet1")
'Find the last row of column A
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Set in arr the range with the data starting from row 1 to last row
arr = .Range("A1:A" & LastRow)
For i = LBound(arr) To UBound(arr) - 1
'Check if the current and the next Arr(i) are both "IN"
If arr(i, 1) = "IN" And arr(i + 1, 1) = "IN" Then
'Set the next arr(i) to OUT
arr(i + 1, 1) = "OUT"
End If
Next i
'Print the values
.Range("A1:A" & UBound(arr)).Value = arr
End With
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")

Cell formula using a variable

I'm using the Excel VBA Editor (I have both Excel 2007 and Excel 2016). I have a variable parameter i, all the others are fixed.
Could you please say me how I can put a formula in a cell Cells(i, 2)?
using variables from my macro (j1, j2, i1)
using variables from my worksheet (the cells J1, J2, C[-1])
C[-1] being the cell left of Cells(i, 2) eg. Cells(i, 1)?
Thans a lot,
Eduard
Try this:
Sub date_add()
Dim i As Long
Dim dt As Worksheet
Set dt = ThisWorkbook.Worksheets("Date")
With dt
lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
For i = 1 To lastRow
.Cells(i, 2).Formula = "=DATE(J1,J2,C" & i & ")"
Next i
End With
End Sub
Where you input Year on J1, Month on J2 and the numbers of dates on column C
Part of the answer. Say cell A1 contains the value 2. Running this:
Sub eddie()
Dim i As Long, s As String
i = Range("A1").Value
s = "=DATE(20,20,20)"
Cells(i, 2).Formula = s
End Sub
will place the formula in cell B2

Resources