VBA to use sum formula for cells in same row. Row number will change with each loop - excel

I'm using VBA to extract numbers from a website using a loop. I have these numbers imported into column F and G. I'm trying to include a line in my VBA that would allow me to use the sum formula to add these two cells and post the result in column E.
The numbers will always be in the same row, but in columns F and G.
Can i use the (ActiveCell.Row) function in this way?
or should just use an If function at the end of my loop.
Range("E" & (ActiveCell.Row)).Formula = "=SUM("Range("F" & (ActiveCell.Row)) & Range("G" & (ActiveCell.Row))")"

I have these numbers imported into column F and G.
If you want to insert the formula in Col E then use this. This will enter the formula in all the relevant cells in Col E in one go.
See this example
Sub Sample()
Dim lRow As Long
Dim ws As Worksheet
'~~> Change this to the releavnt sheet name
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find Last row in col E which has data
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> This will put the formula from E1 to E last row
.Range("E1:E" & lRow).Formula = "=SUM(F1+G1)"
End With
End Sub

In the forumla dont use Range() as it will not return a String. And also, you forgot the operator, you need to add "+" or ":" into the Forumla:
Range("E" & (ActiveCell.Row)).Formula = "=SUM(F" & ActiveCell.Row & "+" & "G" & ActiveCell.Row & ")"
Otherwise you'll need to access the Address method on the Ranges in the forumla (which is a waste because you have entered the address anyway)
but for clarity the code below demonstrates what I mean:
Range("E" & (ActiveCell.Row)).Formula = "=SUM(" & Range("F" & (ActiveCell.Row)).Address(False,False) & ":" & Range("G" & (ActiveCell.Row)).Address(False,False) & ")"

Related

How to append the string value of the first column to all the cell values in the rest of the row

In Excel VBA
I have a string value in the first column of each row. Each column after on that row contains a single word string. I need to append the first column to each of the other cells in that row. Rows are unique.
Use a different worksheet. Let's say yout source data is in a worksheet named Sheet1 Just go to another worksheet and in column A paste those string values. In cell B2 type:
=Sheet1!B1&" <"&$A1&">"
Notice the mixed references in the formula. Drag to right and bottom and see the magic:
You need to drag until last cell of data from Sheet1 so if your original data goes until cell, let's say H203, in the second sheet you would drag directly until column H first and then until row 203.
VBA code would be the same:
Dim LR As Long
Dim WkSource As Worksheet
Dim WkDestiny As Worksheet
Set WkSource = ThisWorkbook.Worksheets("Sheet1")
Set WkDestiny = ThisWorkbook.Worksheets("Sheet2")
With WkSource
LR = .Range("A" & .Rows.Count).End(xlUp).Row
WkDestiny.Range("A1:A" & LR).Value = .Range("A1:A" & LR).Value 'copy first column
End With
With WkDestiny.Range("B1:F" & LR)
.Formula = "=Sheet1!B1&" & """" & " <" & """" & "&$A1&" & """" & ">" & """"
.Value = .Value 'paste as values
End With

for loop over sheets in a workbook with formulas until the end of a column with data

I have have the following problem with vba and would like to ask you for help.
I hope I haven't overseen a similar question in the froum
I have several sheets in an excel workbook. On each sheet I would like to run the same formulas. The formula gets its data from the cell C3 on the same sheet and should run until the end of the data in column C xxx . The length of the data is different on each sheet. Each sheet has its own data set.
The code I have written works fine in the sense that it works itself through through the sheets starting from sheet 2 until the end of the sheets.
The code gets executed via an icon in the toolbar.
On each sheet I have values what the formulas should use. The values in column C are different long. Some have for example just 15 some have over 300000.
For example, if I press the icon when I'm on sheet 1 with no data in column C the macro takes the first 15 values/range of the column for the formulas in all sheets. Which means I miss all values from for example at sheet 3 with 300000 values. If I am on sheet 3 and press the icon there I takes the 300000 values/range and uses the range of the 300000 values in all the other sheets.
If I run the code without the loop on each sheet it works fine. It selects the right range with values of the column.
There are no empty cells in the column.
Has anyone an idea what is wrong in the code that it does not select the real amount of values in column C for the different sheets?
I work on a mac with excel 14.
The following code is what I have so far:
Sub Start()
Dim i As Long
lastrow = Cells(Rows.Count, 3).End(xlUp).Row
For i = 2 To Worksheets.Count
With Sheets(i)
.Range("K11").Formula = "=COUNT(C3:C" & lastrow & ")"
.Range("K12").Formula = "=MEDIAN(C3:C" & lastrow & ")"
.Range("K13").Formula = "=AVERAGE(C3:C" & lastrow & ")"
.Range("K14").Formula = "=MIN(C3:C" & lastrow & ")"
.Range("K15").Formula = "=MAX(C3:C" & lastrow & ")"
.Range("K16").Formula = "=STDEVP(C3:C" & lastrow & ")"
End With
Next i
End Sub
You have to use .Cells to make it refer to the object in the With statement, otherwise it refers to the current sheet:
Sub Start()
Dim i As Long
For i = 2 To Worksheets.Count
With Sheets(i)
lastrow = .Cells(Rows.Count, 3).End(xlUp).Row
Debug.Print (i)
Debug.Print (lastrow)
.Range("K11").Formula = "=COUNT(C3:C" & lastrow & ")"
.Range("K12").Formula = "=MEDIAN(C3:C" & lastrow & ")"
.Range("K13").Formula = "=AVERAGE(C3:C" & lastrow & ")"
.Range("K14").Formula = "=MIN(C3:C" & lastrow & ")"
.Range("K15").Formula = "=MAX(C3:C" & lastrow & ")"
.Range("K16").Formula = "=STDEVP(C3:C" & lastrow & ")"
End With
Next i
End Sub

How to use variables in a VBA code using the sumif function

I download a data set that always has a different number of rows. I store two columns as variables, the imports and the months. Then I need to run a Sumif formula that sums the value of imports by the months. I am writing a Sumif formula that uses the two variables and references the cell to its left.
The cells however vary in location based on the changing size of the data set. So I write a code to select the last active cell on a column and select the cell 3 rows down.
When writing the formula with the variables and the cell its giving me an error. Please help sorry for any typos fist time doing this.
I select all the active cells in range D and store them as months, I do the same for the imports. Then using range I find the last active cell on column M, and use select the cell 3 rows down, where I wish to write my formula.
Please see my codes to see what am I doing wrong, I am a novice coder.
Sub Importaciones()
'
' Importaciones Macro
'
Dim LastRow As Long
LastRow = Range("L" & Rows.Count).End(xlUp).Row
Dim Months As Long
Months = Range("D2", Range("D2").End(xlDown)).Select
Dim Imports As Long
Imports = Range("M2", Range("M2").End(xlDown)).Select
Dim LastRowM As Long
LastRowM = Range("M" & Rows.Count).End(xlUp).Row
Range("M" & LastRowM + 3).Formula = "=sumif(" & Months & ", " &
Range("L" & LastRow + 3) & ", " & Imports & ")"
End Sub
For the formula to work and the sum of the month that I choose comes up
As per all the comments:
Sub Importaciones()
With Worksheets("Sheet1") 'Change to your sheet
Dim LastRow As Long
LastRow = .Range("L" & .Rows.Count).End(xlUp).Row
Dim Months As Range
Set Months = .Range("D2", .Range("D2").End(xlDown))
Dim Imports As Range
Set Imports = .Range("M2", .Range("M2").End(xlDown))
Dim LastRowM As Long
LastRowM = .Range("M" & .Rows.Count).End(xlUp).Row
.Range("M" & LastRowM + 3).Formula = "=sumif(" & Months.Address(0, 0) & ", " & .Range("L" & LastRow + 3).Address(0, 0) & ", " & Imports.Address(0, 0) & ")"
End With
End Sub

Filling columns with formula based on another Cell

I am currently sending my formula to column K & L based on a user form. It sends the formula based on Column G's input. I want to send the formula to columns K & L only if G has a value in it. Can any one help me?
picture of datasheet
'Sets Columns K & L to Method 6 Formula and looks for last populated row in the Nominal Column (G)
LastRow = Range("G" & Rows.Count).End(xlUp).Row
Range("K7").Formula = "=M7+(Q7*(1.04-EXP(0.38*(LN(P7))-0.54)))"
Range("L7").Formula = "=N7-(Q7*(1.04-EXP(0.38*(LN(P7))-0.54)))"
If LastRow = 7 Then
Else
Range("K7").AutoFill Destination:=Range("K7:K" & LastRow)
Range("L7").AutoFill Destination:=Range("L7:L" & LastRow)
End If
If you mean only run the code if G has at least one non-blank cell you could use this. You can apply the formulae across the whole range in one go.
Sub x()
Dim LastRow As Long
LastRow = Range("G" & Rows.Count).End(xlUp).Row
Range("K7:K" & LastRow).Formula = "=IF(G7="""", """",M7+(Q7*(1.04-EXP(0.38*(LN(P7))-0.54))))"
Range("L7:L" & LastRow).Formula = "=IF(G7="""", """",N7-(Q7*(1.04-EXP(0.38*(LN(P7))-0.54))))"
End Sub

Is there any way to make the time stop counting up in excel?

In my excel sheet I have the following code:
=IF(ISERROR(MATCH(D2,'Sheet 2'!A:A,0)),"",NOW())
This basically checks to see if the value in D2 matches any values in column A:A in sheet 2, and then populates the cell with a date and time with NOW().
My problem is the date and time is counting up because I am using the NOW() function whereas what I need is the date to, in a way, take a snapshot of the date or freeze the date. This table I am creating is acting like a log so I need the date to stay as it is when it is put into the cell.
Any help with this is much appreciated.
You could have this automatically run, if you paste it in the code behind your sheet (Where the range theCells is the column where the timestamps are going):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("theCells")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
If Range(Target.Address).Value <> "" Then
Range(Target.Address).Copy
Range(Target.Address).PasteSpecial xlPasteVaues
End If
End Sub
Ok so I solved it, In my VBA I have the following code which pretty much creates a log file when my button is clicked, it takes various information in different cells and populates it in the next defined available row:
Sub copylog()
Dim LastRow As Long, ws As Worksheet
Dim wt As Worksheet
Set ws = Sheets("Create Log")
Set wt = Sheets("PDF Creation")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("A" & LastRow).Value = wt.Range("N11").Value
ws.Range("B" & LastRow).Value = wt.Range("N12").Value
ws.Range("C" & LastRow).Value = wt.Range("N13").Value
ws.Range("D" & LastRow).Value = wt.Range("N14").Value
ws.Range("E" & LastRow).Value = wt.Range("N15").Value
ws.Range("F" & LastRow).Value = wt.Range("N16").Value
ws.Range("G" & LastRow).Value = wt.Range("AT19").Value
ws.Range("H" & LastRow).Value = wt.Range("AT21").Value
ws.Range("I" & LastRow).Value = wt.Range("AT23").Value
ws.Range("J" & LastRow).Value = wt.Range("AT25").Value
ws.Range("K" & LastRow).Value = wt.Range("AT27").Value
ws.Range("L" & LastRow).Value = wt.Range("A2").Value
ws.Range("M" & LastRow).Value = Environ("Username")
End Sub
The code above will populate a table in the next available row, for example, the first line:
ws.Range("A" & LastRow).Value = wt.Range("N11").Value
This will take the value that populates N11 in the PDF create sheet and populate the next available row in column A in the create log sheet (using copy and paste).
The line that has fixed my time problem is the line:
ws.Range("L" & LastRow).Value = wt.Range("A2").Value
In cell A2, I have the NOW() function and the button copies and pastes it into the next available space in column L (copies and pastes as TEXT).

Resources