I'm trying to use this logic:
Range(Selection, Selection.End(xlUp)).Select
In a loop string. Right now the data is create by the loop, so I'm attempting to select the row above, and all the rows for the table for that column, and add the SUM to the cell below.
I'm using a loop as the table can change depending on the reference, and when the data is received I add a new table. Everything works good up to when I want to sum a column in the table and add it below. The cell always returns a 0.
At this point
i = 6
j = 13
ws2.Cells(i , j ).Value = Application.Sum _
(Range(Cells(i, j - 1), Cells(i, j - 1).End(xlUp)))
Thanks for any advice!
Here is a technique using WorksheetFunction.Sum to create a running total.
Sub AddRunningTotal()
Const FIRST_ROW = 2
Const SOURCE_COLUMN = 11
With Worksheets("Sheet2")
With .Range(.Cells(2, SOURCE_COLUMN), .Cells(.Rows.Count, SOURCE_COLUMN).End(xlUp))
.Offset(0, 1).FormulaR1C1 = "=SUM(R2C[-1]:RC[-1])"
'Uncomment to replace the formulas with their values
'.Value = Value
End With
End With
End Sub
Related
I am new to VBA and am trying to create a report template for work. I also want to see how this would be coded for my own personal understanding instead of using a formula.
Put simply I have a variable set of values in column A and dates in column B. Column D is a variable range of dates (user input. I would like to have this as an array within my code.)
I would like to sum column A while excluding the dates specified in column D, and have this sum output into cell G1. I have attached a picture below.
Thanks in advance!
Picture of the sheet
Try out this code, this is self explanatory.
Sub sumVariables()
Dim i As Long, j As Long, sum As Long, match As Boolean
sum = 0
match = False
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
For j = 2 To Cells(Rows.Count, 4).End(xlUp).Row
If Cells(i, 2) = Cells(j, 4) Then
match = True
End If
Next j
If match = False Then
sum = sum + Cells(i, 1)
End If
match = False
Next i
Cells(1, 7) = sum
End Sub
This code would work for n number of rows in your sheet and give the total in G1. Let me know if you need any help.
I am new and learning Excel VBA. I am now having this problem
There is more than 10 tables in a worksheet (number of tables is not consistent)
The number of columns are consistent but not the rows in each tables
I would like to apply a total row to the end of every table
After that, I will apply the same formula to every table and put the results on the right side of each table
This could be easy but the core problem is that the range is unknown.
- As it is not an actual table in Excel, so I tried to first define the range of the data by creating table for it, then again, I don't have idea on how to create the table without knowing the range.
Below is something I came up with (which is not very "dynamic")
Sub plsWork()
Set u = ThisWorkbook.Worksheets("Sheet2")
Set f = u.Range("A").Find(what:="Name", lookat:=xlPart)
a = f.Address
Set sht = u.Range(a)
'trying to insert this at the end of the table
Total = Sum(u.Offset(2, 1) + u.Offset(3, 1) + u.Offset(4, 1))
If Cells(i, 2) = vbNullString Then 'this is already not applicable as the top 2 row in colB has null string
u.Offset(i, 1).Value = Total
'putting the table name at F2
u.Offset(-2, 5).Value = u.Offset(-3, 0).Value
u.Offset(-2, 6).Value = Total
u.Offset(-1, 5).Value = u.Offset(2, 0).Value
u.Offset(-1, 6).Value = Sum(u.Offset(2, 1) + u.Offset(2, 2) + u.Offset(2, 3))
u.Offset(0, 5).Value = u.Offset(3, 0).Value
u.Offset(0, 6).Value = Sum(u.Offset(3, 1) + u.Offset(3, 2) + u.Offset(3, 3))
u.Offset(1, 5).Value = u.Offset(4, 0).Value
u.Offset(1, 6).Value = Sum(u.Offset(4, 1) + u.Offset(4, 2) + u.Offset(4, 3))
End Sub
Oh, and when I run above code, I got error "Sub or Function not defined" on "SUM"
Here is the image of the tables in a sheet
yellow highlighted is what going to be there after executing the sub.
It was quite easy applying formula in Excel sheet and copy paste the formula to each tables,
but it was tedious, so I try to come out with a vba code to help so that the macro could run based on schedule.
I'm scratching my head and searching to and fro for the past two days,
I still haven't got a clue on how to code this.
So can any expert tell me if this is possible? like without knowing the range?
If so, could you guys shed me with some info on how to achieve this?
Thank you. I really want to know if this can be done or not.
Here is an image of my attempt using provided answer
You may try something like this...
The code below will insert a Total Row for each table which has more than one row and four columns in it.
Sub InsertTotalInEachTable()
Dim ws As Worksheet
Dim rng As Range
Dim i As Integer, r As Long, j As Long
Application.ScreenUpdating = False
Set ws = ActiveSheet
For Each rng In ws.UsedRange.SpecialCells(xlCellTypeConstants, 3).Areas
If rng.Rows.Count > 1 And rng.Columns.Count = 4 Then
j = 2
r = rng.Cells(rng.Rows.Count, 1).Row + 1
Cells(r, rng.Columns(1).Column).Value = "Total"
For i = rng.Columns(2).Column To rng.Columns(2).Column + 2
Cells(r, i).Formula = "=SUM(" & rng.Columns(j).Address & ")"
j = j + 1
Next i
End If
Next rng
Application.ScreenUpdating = True
End Sub
Alright for this project I am trying to take columns headers and combine them in row headers in one column. For instance
There a column header plant store it has rows with corresponding data tr1, tr2, tr3.
I want to make one full column with the data so it appears like this "plant store tr1", "Plant store tr2" etc...
this is the code I have so far.
J represents an arbitrary range that I want all the data to fill
X represents the location of all the tr1, tr2s, I want added to the end of plant store
plant store is located at J15 in detailed ratings.
Sub Double_column_method()
Dim J As Variant
Dim x As Variant
Set J = Range("A6:A400")
Sheets("Sheet2").Select
Range("A6").Select
For x = Sheets("Detailed Ratings").Range("J15") To Sheets("Detailed Ratings").Range("BQ15")
If J.Value <> "" Then J.Value = x&(Sheets("Detailed Ratings")).Range("I16")
Next
End Sub
Thank you any help is appreciated.
If I'm reading your post correctly, I think the following is what you need. It can be done without VBA. Type the formula in yellow and copy down/across.
Sub test()
Row = Cells(Rows.Count, "A").End(xlUp).Row
r = Row - 15
Column = Cells(16, Columns.Count).End(xlToLeft).Column
c = Column - 9
For i = 1 To r
For J = 1 To c
n = n + 1
Cells(n, "BU") = Cells(i + 15, "I") & Cells(15, J + 9)
Next J
Next i
This solved it for me, produced a clean list of all my headings combined.
I am looking for a solution for the following example. I have a client who sends me a spreadsheet with 321 columns of random length. In row 2, they have a store number. For each column, there is a different store number. I need to insert a blank column in between each column and then copy the store number in the new column the same length of number of cells.
I use the following to add the columns:
Sub InsertColumns()
Dim J As Integer, k As Integer
J = Range("A1").End(xlToRight).Column
'j is the last column
For k = J To 2 Step -1
Range(Cells(1, k), Cells(1, k)).EntireColumn.Insert
Next k
I just need the code for count up base on the data in the previous column, copying the value from the previous store and pasting this in the column based on the number of rows of the previous column.
Thanks!
Joe
Sub tgr()
Dim cIndex As Long
For cIndex = Cells(2, Columns.Count).End(xlToLeft).Column To 2 Step -1
Columns(cIndex + 1).Insert
Range(Cells(1, cIndex + 1), Cells(Rows.Count, cIndex).End(xlUp).Offset(, 1)).Value = Cells(2, cIndex).Value
Next cIndex
End Sub
Try adding the following just below your posted code block:
Range(Cells(2, 1), Cells(2, (J * 2))).Copy
Range("B2").Select
ActiveSheet.PasteSpecial Format:=1, Link:=1, DisplayAsIcon:=True, IconFileName:=False
Basically it copies the second row up to the point where you have data (previous counted columns times 2 to account for the added blanks), selects the first blank cell, then does a Paste Special with the "Skip Blanks" attribute turned on.
I will be referencing the below picture:
I seek to split up the FirstValue Column into the two columns right of it; however, I want to split the columns based off the Parameter column. When the Parameter value is odd, I want to copy the values only to the OtherValue1 column. When the Parameter value is even, I want to copy the values only to the OtherValue2 column. After reading forums and trying excel's "Text to Columns" feature, I am unable to find a solution.
Is there a way implement this using VBA?
*Note: The worksheet is actually about 10,000 rows long, so speed would also be helpful.
EDIT:
Here is the code I have so far. I am getting Object errors in this line of code: .Cells(2, MF1Col).Formula = "=IF(MOD(paraformula,2)=1,WTRfor,"")"
Dim rw As Worksheet
Dim secondCell, MF1Cell, MF2Cell, paraCell, MF1formula, MF2formula, paraformula, WTRfor As Range
Dim secondCol As Long, MF1Col As Long, MF2Col As Long, paraCol As Long
Set rw = ActiveSheet
With rw
Set secondCell = .Rows(1).Find("FirstValue”)
' Check if the column with “FirstValue” is found
'Insert Two Columns after FirstValue
If Not secondCell Is Nothing Then
secondCol = secondCell.Column
.Columns(secondCol + 1).EntireColumn.Insert
.Columns(secondCol + 2).EntireColumn.Insert
.Cells(1, secondCol + 1).Value = "OtherValue1"
.Cells(1, secondCol + 2).Value = "OtherValue2"
.Activate
Set MF1Cell = .Rows(1).Find("OtherValue1")
MF1Col = MF1Cell.Column
Set MF2Cell = .Rows(1).Find("OtherValue2")
MF2Col = MF2Cell.Column
Set paraCell = .Rows(1).Find("Parameter")
paraCol = paraCell.Column
Set paraformula = Range(.Cells(2, paraCol).Address(RowAbsolute:=False, ColumnAbsolute:=False))
Set MF1formula = Range(.Cells(2, MF1Col).Address(RowAbsolute:=False, ColumnAbsolute:=False))
Set WTRfor = Range(.Cells(2, secondCol).Address(RowAbsolute:=False, ColumnAbsolute:=False))
.Cells(2, MF1Col).Formula = "=IF(MOD(" & paraformula & ",2)=1," & WTRfor & ","""")"
Range(.Cells(2, MF1Col).Address).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveSheet.Paste
Set MF2formula = Range(.Cells(2, MF2Col).Address(RowAbsolute:=False, ColumnAbsolute:=False))
.Cells(2, MF2Col).Formula = "=IF(MOD(" & paraformula & ",2)=0," & WTRfor & ","""")"
Range(.Cells(2, MF2Col).Address).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveSheet.Paste
End If
End With
in C2, =IF(MOD(E2,2)=1,B2,"")
in D2, =IF(MOD(E2,2)=0,B2,"")
copy these down to the end of your data
assuming the same format (Data,Col1,Col2,Parameter), but using relative addressing
Column 1: =IF(MOD(OFFSET(C2,0,2),2)=1,OFFSET(C2,0,-1),"") replace C2 with the current cell
Column 2: =IF(MOD(OFFSET(D2,0,1),2)=0,OFFSET(D2,0,-2),"") replace D2 with the current cell
again, copy and paste - once you have the first one correct, excel will adjust the formula for the current cell
For Cell D2:
=IF(MOD(E2,2),B2,"")
Explanation:
If Range E2 is not divisible by two, the display value from B2, otherwise display nothing.
you can reverse this by inserting a 'NOT' around the MOD for Cell C2:
=IF(NOT(MOD(E2,2)),B2,"")
VBA:
Sub odd_even()
a = 1 ' start row
b = 10 ' end row
c = 1 ' column with values inputs
For d = a To b ' FOR loop from start row to end row
If ActiveSheet.Cells(d, c) Mod 2 Then 'mod becomes high when value is odd
ActiveSheet.Cells(d, c + 2) = ActiveSheet.Cells(d, c) 'odd value gets copied to the odd-column ( two to the right of the values)
ActiveSheet.Cells(d, c + 3) = "" 'same row on even-column gets cleared
Else:
ActiveSheet.Cells(d, c + 3) = ActiveSheet.Cells(d, c) 'even value gets copied to the even-column ( three to the right of the values)
ActiveSheet.Cells(d, c + 2) = "" 'same row on odd-column gets cleared
End If
Next d ' go to next row
End Sub