Simple question, yet I can't get it to work. I simply need to sum 2 sets of cells and paste the totals in a different column.
For example, I need to add the time value G4 and time value in G14 and paste the sum in B25. This should loop down to G10+G20 and paste into B31.
I know how easy it is with a simple excel function, but I need it in VBA terms, and ideally in a loop, not the recorder version of code.
Range("B25") = "=SUM(" & Range(G4, G14) & ")"
Try this:
Sub AddValues()
Dim i As Integer
Dim j As Integer
Dim k As Integer
j = 4
k = 14
For i = 25 To 31
Range("B" & i) = "=SUM(G" & j & ",G" & k & ")"
j = j + 1
k = k + 1
Next i
End Sub
Edit: Thanks #Steve for the hint
Related
I want to get a lot of data from Sheet1 to Sheet2 but when I pull a command to the right it should only increase the "C" to a "D" every third row.
Because I have 3 commands to pull to the right and all should just increade by one not by 3.
A1 looks like this:
=WENN(Sheet1!C3>0;Sheet1!C$2;"")
B1 like this:
=WENN(Sheet1!C3>0;Sheet1!$A3;"")
C1 like this:
=WENN(Sheet1!C3>0;Sheet1!C3;"")
When I pull it to the right it they change like this:
=WENN(Sheet1!F3>0;Sheet1!F$2;"")
=WENN(Sheet1!F3>0;Sheet1!$A3;"")
=WENN(Sheet1!F3>0;Sheet1!F3;"")
But I want:
=WENN(Sheet1!D3>0;Sheet1!D$2;"")
=WENN(Sheet1!D3>0;Sheet1!$A3;"")
=WENN(Sheet1!D3>0;Sheet1!D3;"")
I hope you know what I mean.
Is there any way to do this?
It is possible with VBA:
What you entered in Excel in VBA language is the following:
Range("A1").Formula = "=IF(Sheet1!C3>0,Sheet1!C$2,"""")"
Range("B1").Formula = "=IF(Sheet1!C3>0,Sheet1!$A3,"""")"
Range("C1").Formula = "=IF(Sheet1!C3>0,Sheet1!C3,"""")"
If now you put a small loop you can add 1 to the Column "C" each time you reenter your formula. I used a small Function for this (from Function to convert column number to letter?)
Sub Macro1()
Dim k As Integer
k = 1
Range("A1").Formula = "=IF(Sheet1!C3>0,Sheet1!C$2,"""")"
Range("B1").Formula = "=IF(Sheet1!C3>0,Sheet1!$A3,"""")"
Range("C1").Formula = "=IF(Sheet1!C3>0,Sheet1!C3,"""")"
For k = 1 To 10 ' Here change 10 into whatever you need.
Cells(1, 3 * k + 1).Formula = "=IF(Sheet1!" & Col_Letter(3 + k) & "3>0,Sheet1!" & Col_Letter(3 + k) & "$2,"""")"
Cells(1, 3 * k + 2).Formula = "=IF(Sheet1!" & Col_Letter(3 + k) & "3>0,Sheet1!$A3,"""")"
Cells(1, 3 * k + 3).Formula = "=IF(Sheet1!" & Col_Letter(3 + k) & "3>0,Sheet1!" & Col_Letter(3 + k) & "3,"""")"
Next k
End Sub
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
Hope it helps
If you want a formula based solution
A1:
=IF(INDIRECT(CONCATENATE("Sheet1!";LEFT(SUBSTITUTE(ADDRESS(1;(COLUMN(A:A)+2)/3+2);"$";"");1);"3"))>0;INDIRECT(CONCATENATE("Sheet1!";LEFT(SUBSTITUTE(ADDRESS(1;(COLUMN(A:A)+2)/3+2);"$";"");1);"$2"));"")
B1:
=IF(INDIRECT(CONCATENATE("Sheet1!";LEFT(SUBSTITUTE(ADDRESS(1;(COLUMN(A:A)+2)/3+1);"$";"");1);"3"))>0;Sheet1!$A3;"")
C1:
=IF(INDIRECT(CONCATENATE("Sheet1!";LEFT(SUBSTITUTE(ADDRESS(1;COLUMN(C:C)/3+2);"$";"");1);"1"))>0;INDIRECT(CONCATENATE("Sheet1!";LEFT(SUBSTITUTE(ADDRESS(1;COLUMN(C:C)/3+2);"$";"");1);"1"));"")
I can't guarantee that there's no mistake as I don't have your workbook, however when I made a test of the formula it worked fine for me.
As a beginner, I would like to ask if it is possible in VBA to autofill formulas in a multidimensional array.
Ι thought and wrote this code and it works with values but not with formulas.
I think that what I am trying to do is very ambitious for my skills
Sub eucldiist()
Dim e(10,10) As Double, i As Integer, j As Integer
For i = 1 To 10
For j = 1 To 10
e(i, j).FormulaArray = "=sqrt((offset('Data1'!$Q$14,$BD$5(offset(i)) -
offset('Data1'!$Q$14,$BD$5(offset(j))^2+ ((offset('Data1'!$U$14,
$BD$5(offset(i))-OFFSET('Data1'!$U$14,$BD$5(offset(j)))^2)"
Next j
Next i
Sheets("Calculations").Select: Range("A20").Select
For i = 1 To 10
For j = 1 To 10
ActiveCell.Value = e(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Offset(1, -10).Select
Next i
End Sub
Is it possible what I am trying to do?
check this out. put some formulas in a20:j29 before hand
Option Explicit
Sub test()
Dim aaa As Variant
aaa = Range("a20").Resize(10, 10).Formula
Stop ' now examine aaa in "locals" window
' maybe all you want is this
range("a20").Resize(10,10).FormulaArray = "=SQRT((OFFSET(DATA1!Q14,BD5,0)-OFFSET(DATA1!Q14,BD7,0))^2+((OFFSET(DATA1!U14,BD5,0)-OFFSET(DATA1!U14,BD7,0))^2))"
End Sub
i think that this is what you want
NOTE: formula uses absolute addressing, so a column or row insert will break it
delete $ from formula to make it use relative addressing
Sub eucldiist()
Dim base As Range
Set base = Sheets("Calculations").Range("A20")
' Set base = ActiveSheet.Range("A20")
Dim formula As String
Dim i As Integer, j As Integer
For i = 0 To 9 ' rows
For j = 0 To 9 ' columns
formula = "=sqrt(" _
& " (offset('Data1'!$Q$14,$BD$" & 5 + i & ",0) - offset('Data1'!$Q$14,$BD$" & 7 + j & ",0))^2" _
& " + (offset('Data1'!$U$14,$BD$" & 5 + i & ",0) - offset('Data1'!$U$14,$BD$" & 7 + j & ",0))^2" _
& ")"
' Debug.Print formula
base.Offset(i, j) = formula
Next j
Next i
End Sub
I have the following data. How can I code in VBA to execute the following?
E: I know it would be something with Looping, i.e: For Next.
But I am having troubles to loop the values that are not blank, in EACH column to the last column of the excel
Before:
After:
Run this VBA macro in your sheet,
Sub copyvalues()
Dim i As Long, j As Long, k As Long
k = 1
'Change the value to 200 to the last row number of your range
For i = 1 To 200
For j = 7 To 255
If Cells(i, j) <> "" Then
Range("F" & k).Value = Cells(i, j)
k = k + 1
End If
Next j
Next i
End Sub
B C D
1/1 1/2 1/3
2 Product 1 ?
3 Product 2
I'm trying to use VBA to automate inserting formula based on product name and date criteria. Tried multiple ways including index match, intersect but can't seem to get it to work!
Currently my formula is manually placed based on dates of another worksheet. Formula for reference
=IFERROR(IF(VLOOKUP($B$1&$C39&Japan!$E39,TDM!$A:$AA,23,0)=Japan!U$33,SUMIF(Allocation!$A:$A,Japan!$A39,Allocation!$S:$S),"")*$G39,"")
Is there a way to automate this using VBA?
Try this :
Sub Fill_Formula()
Dim StartRow As Long
StartRow = 2
For i = StartRow To 50 'Start and End Rows
For j = 2 To 10 'Start and End Columns
Cells(i, j).Formula = _
"IFERROR(IF(VLOOKUP($B$1&$C" & 39 + (i - StartRow) & "&Japan!$E" & 39 + (i - StartRow) & ",TDM!$A:$AA,23,0)=Japan!U$" & 33 + (i - StartRow) & ",SUMIF(Allocation!$A:$A,Japan!$A" & 39 + (i - StartRow) & ",Allocation!$S:$S),"")*$G" & 39 + (i - StartRow) & ","")"
Next j
Next i
End Sub
Trying to convert specific data in a specific column through the whole worksheet.
This is my current code, it does not error out, however it doesn't display the first result correctly (Shows 00:00:00), and only stops at the first record.
Sub CleanEntry()
Dim i As Integer
Dim Seconds As Integer
Dim j As Long
Dim c As Long
j = 2
For i = Sheet1.UsedRange.Rows.Count To 1 Step -1
c = Range("B" & j).Value
c = c / 86400
Range("B" & j).Value = Format(c, "hh:mm:ss")
j = j + 1
Next
End Sub
Though I do not understand why you are using two counters, with one going backwards and the other forward, this can be done with one line:
Sub CleanEntry()
Dim i As Long
Dim j As Long
j = 2
For i = Sheet1.UsedRange.Rows.Count To 1 Step -1
Range("B" & j).Value = Format(TimeSerial(0, 0, Range("B" & j).Value), "hh:mm:ss")
j = j + 1
Next
End Sub