I try following VBA Programming, but its formula in excel cell is "=SUM('F12':'F91')" which ' mark can not remove. Can anyone teach me what's problem in my following program?
Sub Step7()
' Step 7 : Insert S code for VBA formula Input
' S - Subtotal
' Prepare formula to each Code
Dim c1, c2 As Long
c1 = 2
For i = 2 To 9999
If Range("H" & i).Value <> "s" Then
c2 = i - 1
ElseIf Range("H" & i).Value = "s" Then
Range("F" & i).Select
ActiveCell.FormulaR1C1 = "=SUM(F" & c1 & ":F" & c2 & ")"
c1 = c2
End If
Next i
End Sub
I need to create a formula that is going to concatenate 3 different cells into one date. The formula is going to be a part of a loop function so I need the cell reference to change as the loop function runs.
I am having trouble with the syntax such as the "&" and the """" that are necessary to distinguish parts of the formula from the cell references.
For now I am just trying to get the formula to paste into a single cell without the loop. The 3 cells that I am combining are in columns: N,O & P. I am trying to paste the formula into column M.
I tried creating the formula on a separate "Data" tab and then simply copy and pasting it into each cell using VBA but the row number does not update according to the row that the formula is pasted.
I tried rearranging the & and "" for a while and could not figure out the winning combination.
FormulaRow = Cells(Rows.Count, "M").End(xlUp).Offset(1).Row
M_Formula = "=N" & FormulaRow & "" / "" & "O" & FormulaRow & "" / "" & "P" & FormulaRow
Range("M" & FormulaRow).Value = M_Formula
I am expecting to get the following result: =N5&"/"&O5&"/"&P5 with the row number corresponding to the row that the formula is pasted.
When I tried the copy and paste method I got this message: "Object Doesn't Support this Property or Method"
Any help would be appreciated. Thank you!
Maybe:
Sub sub1()
' If you have 12 in N2 and 34 in O2 and 5678 in P2:
Dim FormulaRow&, M_Formula$
FormulaRow = 2
M_Formula = "=N" & FormulaRow & "&" & """" & "/" & """" & "&" & _
"O" & FormulaRow & "&" & """" & "/" & """" & "&" & _
"P" & FormulaRow
Cells(FormulaRow, ColNum("M")) = M_Formula ' gives the formula you want 12/34/5678
Cells(FormulaRow, 13) = M_Formula ' also gives the formula you want 12/34/5678
End Sub
Function ColNum&(col$)
ColNum = Range(col & 1).Column
End Function
Excel is smart enough to increment the row reference when you do a range in one go:
Range("M5:M" & Range("N" & rows.count).end(xlup).row).formula = "=N5 & ""/"" & O5 & ""/"" & P5"
That will do what you want in 1 line.
Then you can copy the result, paste as values then format to date with something like this:
Sub EnterDate()
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = "=N5 & ""/"" & O5 & ""/"" & P5"
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Copy
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).PasteSpecial xlPasteValues
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).NumberFormat = "DD/MM/YYYY"
'Force a reevaluate to make it see actual dates
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Value
End Sub
Using this method I just tested on over 12,000 rows and it took under a second.
With regards to the comments about using the date function, using the date function is a much better method, I wanted to show you how to do it using your own method but you can get rid of the formatting code if you use Date like so:
Sub EnterDate()
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = "=DATE(P5,O5,N5)"
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Copy
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).PasteSpecial xlPasteValues
End Sub
If you want to leave the formulas then simply delete the last 2 lines in there.
I want to apply a formula that is automatically calculated by a column input from an Excel table to a specific column. At this time, since the column to which the formula is applied should appear at the same interval, the following code value is used and an error occurs. What am I doing wrong?
1004 runtime error
For b = 68 To 299 Step 21
Cells(2, b).Select
ActiveCell.Formula = "=IFERROR" & "([#[" & Cells(1, b - 2).Value & "]] " & "*" & "([#[" & Cells(1, b - 1).Value & "]]" & "/" & "1000000" & ")," & Chr(34) & Chr(34) & ")"
Cells(2, b).Select
Selection.AutoFill Destination:=Range("table1" & "[" & Cells(1, b) & "]")
Next b
cells(1,b) => table Header
Please help to run this formula if the cell C2 value is saturday
Range("F2").Formula = "=IF(AND(D2>=TIME(9,16,0),D2<=TIME(11,59,0)),D2-TIME(9,0,0),IF(AND(D2>=TIME(14,16,0),D2>=TIME(12,00,0)),D2-TIME(14,0,0)*1,0))"
and if Cell C2 value is Sunday to Friday
Range("F2").Formula = "=IF(AND(D2>=TIME(8,16,0),D2<=TIME(10,30,0)),D2-TIME(8,0,0),IF(AND(D2>=TIME(12,16,0),D2>=TIME(10,31,0)),D2-TIME(12,0,0)*1,0))"
Until the last row which has data in the sheet.
In short check the value in C2, if = saturday formula 1 or formula 2.
From C2 onwards Column C contains date. eg
05/03/2019
Sample Excel file Screenshot
Try this:
Option Explicit
Sub Test()
Dim i As Long
For i = 2 To Cells(Rows.Count, "C").End(xlUp).Row
If Weekday(Range("C" & i)) = 7 Then
Range("F" & i).Formula = "=IF(AND(D" & i & ">=TIME(9,16,0),D" & i & "<=TIME(11,59,0)),D" & i & "-TIME(9,0,0),IF(AND(D" & i & ">=TIME(14,16,0),D" & i & ">=TIME(12,00,0)),D" & i & "-TIME(14,0,0)*1,0))"
Else
Range("F" & i).Formula = "=IF(AND(D" & i & ">=TIME(8,16,0),D" & i & "<=TIME(10,30,0)),D" & i & "-TIME(8,0,0),IF(AND(D" & i & ">=TIME(12,16,0),D" & i & ">=TIME(10,31,0)),D" & i & "-TIME(12,0,0)*1,0))"
End If
Next i
End Sub
I am relatively new to Excel VBA script and am having trouble with the syntax quotes here on this line
ActiveSheet.Range("H" & EndRowH + 2).Formula = "=SUMIFS(H2:H &EndRowH & ,C1:C1000,&"">=""&lngStart&)"
Let me know if you need any other details.
You had some basic string concatenation syntax errors.
Both the criteria range and the sum range need to be the same number of rows.
ActiveSheet.Range("H" & EndRowH + 2).Formula = "=SUMIFS(H2:H" & EndRowH & ", C2:C" & EndRowH & ", "">=" & lngStart &""")"
Your original formula had the sum range start at H2 while the criteria range started at C1. While you can offset the sum range and the criteria range if necessary, they still have to be the same number of rows.
ActiveSheet.Range("H" & EndRowH + 2).Formula = "=SUMIFS(H2:H" & EndRowH & ", C1:C" & EndRowH - 1 & ", "">=" & lngStart &""")"