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

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

Related

How do I move specific valued cells in VBA?

I am working with a dataset that contains both numbers and names. In the dataset, some numbers and names are displayed and instead of manually going through thousands of rows I tried to make a script but it doesn´t happen anything.
Here is the code:
Sub MoveCells()
Dim row As Long
For row = 2 To LastRow
If Range("C" & row).Value Like "*0*" Then
Dim i As Integer
For i = 1 To 2
Range("C" & row).Insert Shift:=xlToRight
Next
End If
Next
End Sub
I am trying to move the cell that has a 0 in it, and the cell to the right of it, one step to right.
E.g. Cells C4 & D4 to D4 & E4.
I've made some adjustments to your code which will acheive the outcome you described.
Private Sub MoveCells()
Dim TargetRow As Long
Dim LastRow As Long
Dim ColumnCValue As Variant
Dim ColumnDValue As Variant
With Sheets("Sheet1")
LastRow = .Cells(.Rows.Count, 3).End(xlUp).row
End With
For TargetRow = 2 To LastRow
If Sheets("Sheet1").Range("C" & TargetRow).Value Like "*0*" Then
ColumnCValue = Sheets("Sheet1").Range("C" & TargetRow).Value
ColumnDValue = Sheets("Sheet1").Range("D" & TargetRow).Value
Sheets("Sheet1").Range("D" & TargetRow).Value = ColumnCValue
Sheets("Sheet1").Range("E" & TargetRow).Value = ColumnDValue
Sheets("Sheet1").Range("C" & TargetRow).ClearContents
End If
Next
End Sub
Now we first assign a value to for LastRow and when the If...Then statement is true, assign the values of Column C and Column D to the respective variables. Then, write those values 1 row to the right and finally clear the contents from Column C.

Excel copy cut paste data from 1 sheet to another with a status in updated in sheet 2 new column

I am new to macro I have created a macro that copies data from excel sheet1 column A & B and paste it in sheet 2 with status as updates in column c. However, it is not working properly it executes with incorrect/incomplete way like for some values in sheet 2 column B it shows updated in column c but for some, it does not... Please help me below is my code.
Secondly, I have coded first for copy paste data from sheet1 to sheet2 there I have specified the range A2:A9999 and B2:B9999 I am not able to simplify it. I mean it should take the entire column A and B than the specified range. Please help me with these 2 parts.............
Sub CopyData()
Dim i As Long
Dim wt As Excel.Worksheet
Set wr = Worksheets("Sheet2")
'Copies and cuts the data from sheet1(TIS) and paste the same in sheet2
With Worksheets("SampleFile")
.Range("A2:A9999").Copy wr.Range("A2") 'Copy
.Range("A2:A9999").Cut wr.Range("A2") 'Cut
.Range("B2:B9999").Copy wr.Range("B2") 'Copy
.Range("B2:B9999").Cut wr.Range("B2") 'Cut
End With
For i = 1 To wr.Cells(wr.Rows.Count, "B").End(xlUp).Row
If wr.Range("B" & i).Value = "FXV" Then
wr.Range("C" & i).Value = "Updated"
ElseIf wr.Range("B" & i).Value = "FST" Then
wr.Range("C" & i).Value = "Updated"
ElseIf wr.Range("B" & i).Value = "FLB" Then
wr.Range("C" & i).Value = "Updated"
ElseIf wr.Range("B" & i).Value = "FFH" Then
wr.Range("C" & i).Value = "Updated"
ElseIf wr.Range("B" & i).Value = "FFJ" Then
wr.Range("C" & i).Value = "Updated"
End If
Next i
End Sub
This code should cut data from A2 to B and LastRow in worksheet SampleFile and paste it to Range A2 in worksheet Sheet2. Then it will loop through all the rows in Sheet2 looking for the value in column B, if it matches the Select Cases will input Updated in column C:
Option Explicit
Sub CopyData()
Dim wr As Worksheet: Set wr = Worksheets("Sheet2")
'Copies and cuts the data from sheet1(TIS) and paste the same in sheet2
'there is no need to copy if you are going to cut
'also use a defined range to copy instead 9999 rows
With ThisWorkbook.Worksheets("SampleFile")
Dim LastRow As Long: LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
'you can also cut both columns at once
.Range("A2:B" & LastRow).Cut wr.Range("A2") 'Cut
End With
Dim i As Long
With wr
For i = 1 To .Cells(.Rows.Count, "B").End(xlUp).Row
'in this case is way shorter to code using the Select statement
'you could also use If x = y or x = z or x = a but Select looks cleaner.
.Cells(i, "B") = Trim(.Cells(i, "B"))
Select Case .Range("B" & i)
Case "FXV", "FST", "FLB", "FFH", "FFJ"
.Range("C" & i) = "Updated"
End Select
Next i
End With
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

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

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) & ")"

Resources