Replace All Cells in Column G with a Formula - excel

Am I missing something here? I'm getting a 1004 during debug
Sub perc()
Dim lastRow As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("G2:G" & lastRow).Formula = "=IF((AND(A2<>"",F2>0)),F2/C2, "")"
End Sub

You need to qualify your objects - What workbook/worksheet does the Range and Rows object exist on? The assumed object references may be incorrect
You need to double up on your blank quote strings inside the formula
Sub Perc()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lr As Long
lr = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws.Range("G2:G" & lr).Formula = "=IF((AND(A2<>"""", F2>0)), F2/C2, """")"
End Sub

Related

Variable range base on cell value

Not sure if I did the code correctly but it is there.
My quandary is this line ws.Range("B" & J, Range("K" & J)).copy. It is giving me a Run-time error 1004 Method range of object worksheet failed.
What I am trying to do is copy/paste any row b:k if column "P" indicated "recorded".
Your assistance is greatly appreciated. Thank you.
Sub Clear_Recorded()
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim lRow As Integer 'Data Tab
Dim count As Integer
Set ws = Sheet1 'Data
Set ws1 = Sheet11 'Archive
count = 0
lRow = ws.Range("B" & Rows.count).End(xlUp).Row
For J = 2 To lRow
If ws.Range("P" & J).Value = "Recorded" Then
count = count + 1
ws.Range("B" & J, Range("K" & J)).copy
ws1.Range("A" & count).PasteSpecial
End If
Next J
You just need to get rid of the second "Range" and the extra parenthesis. Hope this helps!
ws.Range("B" & J, "K" & J).Copy
Edit: Spelling
You are trying to set a worksheet object without referencing any kind of sheet.
How to fix this issue:
set ws = Thisworkbook.Sheets("SheetName")
Also you should rather use this:
if ws.Range("P2").Offset(J-1) = "Recorded" then
'Rest of code goes here
.Offset has the parameters RowOffset ,ColumnOffset
This should solve your problem.

Can this VBA code be adapted to start in cell A2

I have some VBA code which highlights the cells in A if they contain a value.
Is there a way to adapt this to start in cell A2 as A1 is a header.
Option Explicit
Sub LRow()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<=== Edit Sheet Name
Dim LRow As Long
LRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws.Range("A1:A" & LRow).Select
End Sub
You only need to change the starting cell to A2: ws.Range("A2:A" & LRow).Select
In your code it looks like this:
Sub LRow()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<=== Edit Sheet Name
Dim LRow As Long
LRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
ws.Range("A2:A" & LRow).Select
End Sub

Averaging different length ranges in excel with VBA

I'm trying to write a short macro that includes a line that averages a range of cells. In each worksheet that I want to run the macro in the range of cells is a different length.
After running the macro the cell E1 contains "=AVERAGE(Rng)"
Dim homeSheet As Worksheet
Set homeSheet = ActiveSheet
Dim lastRow As Long
Dim Rng As Range
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("B2:B" & lastRow)
Range("E1").Formula = "=Average(Rng)"
Range("E2").Formula = "=STDEV(Rng)"
Range("E3").Select
ActiveWindow.SmallScroll Down:=-2
End Sub
I've also tried
Range("E1").Formula = "=Average(Range("B2:B" & lastRow))"
without trying to use Set Rng = Range("B2:B" & lastRow)
You need to use Rng.Address in your formulas. Try to change your code into this:
Sub Avg()
Dim homeSheet As Worksheet
Set homeSheet = ActiveSheet
Dim lastRow As Long
Dim Rng As Range
lastRow = Range("A" & Rows.Count).End(xlUp).Row
Set Rng = Range("B2:B" & lastRow)
Range("E1").Formula = "=Average(" & Rng.Address & ")"
Range("E2").Formula = "=STDEV(" & Rng.Address & ")"
Range("E3").Select
End Sub
If you were to use the second method you have tried, you would need to change that line of code to:
Range("E1").Formula = "=Average(" & Range("B2:B" & lastRow).Address & ")"

Add offset to a last row

I just want to add an offset (0,6) to my lastRow . any help?
lastRow = oSht.Range(vari1 & Rows.Count).End(xlUp).Row
After your clarification I could suggest that you could do it by range expansion. Instead of Offset you could use Resize.
As you said this is working correct for your S column:
oSht.Range(vari2, vari1 & lastRow).Select
After we add resize to that you get your new range:
oSht.Range(vari2, vari1 & lastRow).Resize(,6).Select
I think this is what you want?
Option Explicit
Sub Sample()
Dim oSht As Worksheet
Dim lastRow As Long
Dim Rng As Range
Dim vari1 As String
'~~> Change this to the relevant column letter
vari1 = "S"
'~~> Change this to the relevant sheet
Set oSht = ThisWorkbook.Sheets("Sheet1")
With oSht
lastRow = .Range(vari1 & .Rows.Count).End(xlUp).Row
Set Rng = .Range(vari1 & 2 & ":" & .Range(vari1 & lastRow).Offset(, 6).Address)
'~~> S2:X32 in your case if lastrow is 32
Debug.Print Rng.Address
End With
End Sub

Append cell to a different cell

I Have a Column C that has names in all its cells and another Column E that has the same company name in all its cells I need to append the names in Column C to the company name in column E
Thanks
Ex:
ColC ColE
Bob SampleCo
Sally SamplCo
I get
ColC ColE
Bob SampleCo Bob
Sally SamplCo Sally
I am trying but failing with
Sub CompanyName()
Dim LastRow As Long
Dim Ws As Worksheet
Dim rRange As range
Set rRange = range("E2")
rRange.Select
Set Ws = Sheets("WP_SubjectList_Ready")
LastRow = Ws.range("F" & Ws.Rows.Count).End(xlUp).Row
Ws.range("E2:E" & LastRow).FormulaR1C1 = "=rRange &RC[-1]"
range("E2:E" & LastRow).Copy
range("E2:E" & LastRow).PasteSpecial xlPasteValues
End Sub
Code
Sub CompanyName()
Dim LastRow As Long
Dim Ws As Worksheet
Set Ws = Sheets("WP_SubjectList_Ready")
LastRow = Ws.Range("E" & Ws.Rows.Count).End(xlUp).Row
Ws.Range("F2:F" & LastRow).FormulaR1C1 = "= RC[-1] & "" "" & RC[-3]"
End Sub
If you want the output in Column E its not possible using FormulaR1C1.
Any formula which work for excel interface will work for FormulaR1C1.
With that i mean (considering the image) in cell F2 you can manullay enter a formula = E2 & " " & C2 which will give you desired output. But if you enter in cell E2the formula as =E2 & " " & C2 the cell E2 will loose its value and this may even lead to circular reference issue.
It can be achieved using below code.
Sub CompanyName()
Dim LastRow As Long
Dim Ws As Worksheet
Dim rng As Range, cell As Range
Set Ws = Sheets("WP_SubjectList_Ready")
LastRow = Ws.Range("E" & Ws.Rows.Count).End(xlUp).Row
Set rng = Ws.Range("E2:E" & LastRow)
For Each cell In rng
cell = cell & " " & cell.Offset(0, -2)
Next
End Sub
Here's some code that should help you with what you want...I don't typically use ranges for loops because it's easier to use .Cells(row, col) for me, but anyways:
EDIT: Added Sub Opening/Closing Syntax and edited to use WS instead of ActiveSheet so it's closer to what you want
Sub CompanyName()
Dim WS as Worksheet
Dim vRow
Dim vRowCount As Integer
Set WS = Sheets("WP_SubjectList_Ready")
'Gets Row # of Last Row for Column E
vRowCount = Range("E" & Rows.Count).End(xlUp).row
'Assuming Both Columns have the same row count and you have a header row
For vRow = 2 To vRowCount
WS.Cells(vRow, 5).Value = WS.Cells(vRow, 5).Value & " " & WS.Cells(vRow, 3).Value
Next vRow
End Sub

Resources