I am trying to "print" formulas into an excel sheet with VBA.
The code works without the "=" sign in front of the COUNTIF, but when I Insert it I get a Run-Time error 1004.
Can someone help me?
Here is the code that gets the error
Sub Looksie()
Dim Tru As Range
Dim i As Integer
i = 2
For Each Tru In Range("A2:A300")
Sheets("Resultat").Select
Cells(i, 2).Formula = "=COUNTIF(Input!" & Cells(19, i + 1).Address & ":" & Cells(5000, i + 1).Address & ";" & """" & "A" & """" & ")"
i = i + 1
Next Tru
End Sub
Thank you
Could you use next code?
I'm edited only ":" to ",".
Sub Looksie()
Dim Tru As Range
Dim i As Integer
i = 2
For Each Tru In Range("A2:A300")
Sheets("Resultat").Select
Cells(i, 2).Formula = "=COUNTIF(Input!" & Cells(19, i + 1).Address & ":" & Cells(5000, i + 1).Address & ",""A"")"
i = i + 1
Next Tru
End Sub
Related
I'm trying to select differrents worksheets, based on a variable.
All this in a purpose to print those sheets into a pdf.
I've tried with arrays but i don't know much.
So I'm continuing with string and split.
But I have no chance too.
All is ok except the last codeline , the sheets selection.
I receive a Subscript OutOf range
Any advice?
Sub Print_Full_Report()
Call Clear_clipboard
Call Stop_Calcul_Screen
LastOverviewRow = WS_Rep_Overview.Cells(Rows.Count, 3).End(xlUp).Row
tabtoprint = ""
SelectedDate = Date + 90
For i = 5 To LastOverviewRow
If WS_Rep_Overview.Range("E" & i) < SelectedDate Then
Module = WS_Rep_Overview.Range("D" & i).Value
tabtoprint = tabtoprint & Chr(34) & Module & Chr(34) & "," & Chr(34) & Module & "-Cow" & Chr(34) & "," & Chr(34) & Module & "-SubSys" & Chr(34) & ","
End If
Next i
tabtoprint = Mid(tabtoprint, 1, Len(tabtoprint) - 1)
Sheets(Split(tabtoprint, ",")).Select
End Sub
Try this code:
Sub Print_Full_Report()
Const template = ",#,#-Cow,#-SubSys" ' for further use in the loop
' previous your code ...
' in the code some redundant variables have been removed
With WS_Rep_Overview
SelectedDate = Date + 90
For i = 5 To .Cells(Rows.Count, 3).End(xlUp).Row
If .Range("E" & i) < SelectedDate Then
tabtoprint = tabtoprint & Replace(template, "#", .Range("D" & i).Value)
End If
Next i
End With
tabtoprint = Mid(tabtoprint, 2) 'remove the leading comma
' for debug; check that it prints something like this: Module5,Module5-Cow,Module5-SubSys,Module6,Module6-Cow,Module6-SubSys,Module7,Module7-Cow,Module7-SubSys
Debug.Print tabtoprint
Worksheets(Split(tabtoprint, ",")).Select
End Sub
Im confused with the range.formula on excel VBA,
Im trying to use the formula RIGHTas follows on my VBA code:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("R" & i).Formula = "=RIGHT("cel", "cel2" & "" - "" & "cel3")"
The last row of my code, I trying to do right(A1, P1-Q1)
However, Im getting syntax error and Im struggling to solve
Thanks in advance
You were very, very close:
Sub marine()
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("R" & i).Formula = "=RIGHT(" & cel & "," & cel2 & " - " & cel3 & ")"
Next i
End Sub
I am new to VBA programming and am trying to add text to a cell based upon its value within an If Statement. I have tried using a find statement, but it ends in a null value and ultimately, I need this to work for 3 different values. I am not sure I need to define "MyCell" as a variable. It has not worked either way. When I run the code below it says MyCell is an invalid qualifier.
Sub Macro2()
'
' Macro to insert Text into Cells
Dim i As Long
Dim MyCell As String
Worksheets("Tax").Active
For i = 2 To Range("H" & Rows.Count).End(xlUp).Row
ActiveCell.Select
MyCell = ActiveCell.Value
If MyCell.Value = "State Tax" Then
MyCell = Join("State Tax - ", Range(i, "D"))
ElseIf MyCell.Value = "SUI" Then
MyCell = Join("SUI - ", Range(i, "D"))
ElseIf MyCell.Value = "Local Tax" Then
MyCell = Join("Local Tax - ", Range(i, "D"))
End If
Next i
End Sub
You don't need myCell. If you did you would have instanced it as a range, not a string. Try this and see if it accomplished what you need.
Sub Macro2()
'
' Macro to insert Text into Cells
Dim i As Long
With Worksheets("Tax")
For i = 2 To .Range("H" & Rows.Count).End(xlUp).Row
If .Range("H" & i).Value = "State Tax" Then
.Range("H" & i).Value = "State Tax - " & .Range("D" & i).Value
ElseIf .Range("H" & i).Value = "SUI" Then
.Range("H" & i).Value = "SUI - " & .Range("D" & i).Value
ElseIf .Range("H" & i).Value = "Local Tax" Then
.Range("H" & i).Value = "Local Tax - " & .Range("D" & i).Value
End If
Next i
End With
End Sub
I'm concatenating 4 different formula based columns into one using VBA (to be able to change formatting while still concatenating). The concatenating VBA code works, but when the 4 individual columns update and pull the new information, the concatenated column doesn't change.
My concatenated code is this and it lies in column D or 4:
Sub joint1()
ActiveSheet.Range("a2", ActiveSheet.Range("a2").End(xlDown)).Select
Row = 2
Col = 4
For Each Cell In Selection
AE = Cells(Row, Col + 15)
Name = Cells(Row, Col + 9)
SC = Cells(Row, Col + 16)
PM = Cells(Row, Col + 10)
Cells(Row, Col) = Name & Chr(10) & "(" & AE & " - " & SC & ")" & Chr(10) & PM & " - PM"
With Cells(Row, Col)
.ClearFormats
.Characters(1, Len(Name)).Font.Bold = True
End With
Row = Row + 1
Next
End Sub
If you know how to add a feature to help my problem, I would be very appreciative!
Try this:
Option Explicit
Sub joint1()
Dim iRow As Long
Dim iCol As Long
Dim rng As Range
Dim rngSelect As Range
Dim Name As String
Set rngSelect = ActiveSheet.Range("a2", ActiveSheet.Range("a2").End(xlDown))
iRow = 2
iCol = 4
For Each rng In rngSelect
Name = Cells(iRow, iCol + 9)
Cells(iRow, Col) = "=M" & iRow & Chr(10) & " & ""("" & S" & iRow & " & "" - "" & T" & iRow & " & "")"" &" & Chr(10) & "N" & iRow & " & ""-PM"""
With Cells(iRow, iCol)
.ClearFormats
.Characters(1, Len(Name)).Font.Bold = True
End With
iRow = iRow + 1
Next
End Sub
This code creates a formula in each cell, rather than just copying the values.
The job could probably be done just as well with an excel formula. The formatting doesn't work with my version of excel (2007).
I am trying to use .formulaR1C1 to sum up values from different sheets on my main sheet for my workbook.
Everytime I run through my code it errors on the last bit where the formula is entered.
code:
For cRow = 9 To row
For Each WS In Worksheets
If Left(WS.Name, 5) <> "Total" Then
If Left(WS.Name, InStr(WS.Name, " ") - 1) = "December" Then
ytdSheet.Cells(cRow, 2).FormulaR1C1 = "=SUM(" & ytdSheet.Cells(cRow, 2).Value & "'" & WS.Name & "'!RC)" 'HERE IS WHERE ERROR OCCURS!!!
Else
ytdSheet.Cells(cRow, 2).Value = ytdSheet.Cells(cRow, 2).Value & "'" & WS.Name & "'!RC,"
End If
End If
Next WS
Next cRow
I feel like I am missing something simple, but I cannot figure it out.
Well I figured out why it wasn't working! my code was putting in a leading ' but in the cell, it reads it as a "Make this a text entry" so it doesn't show up in the debug.print ..
I changed my code to use an array instead of the cell to hold the string while I build it, and then use the formulaR1C1 line to output to the cell..
For cRow = 9 To row
For Each WS In Worksheets
If Left(WS.Name, 5) <> "Total" Then
If Left(WS.Name, InStr(WS.Name, " ") - 1) = "December" Then
ytdSheet.Cells(cRow, 2).FormulaR1C1 = "=sum(" & sArr(1) & "'" & WS.Name & "'!R" & cRow & "C2)" 'HERE!!!
'Debug.Print "=" & ytdSheet.Cells(cRow, 2).Value & "'" & WS.Name & "'!R" & cRow & "C2" 'HERE!!!
Else
sArr(1) = sArr(1) & "'" & WS.Name & "'!R" & cRow & "C2, "
Debug.Print sArr(1)
End If
End If
Next WS
Next cRow
works like a charm!