Deleting chr160 characters in excel using vba - excel

In the following code I would like to delete the char 160 and 34 characters at the end of the text string.
For Column = 12 To 13
For i = 16 To last_row
'sString = ("=CODE(160)")
'Range(Cells(i, Column), Cells(i, Column)).Select
'Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column)).Value = Replace(Range(Cells(i, Column), Cells(i, Column)).Value, sString, "", 1, -1)
Set current_cell = Range(Cells(i, Column), Cells(i, Column))
current_cell.Select
text_to_change = Range(Cells(i, Column), Cells(i, Column)).Value
Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column)).Select
Selection.NumberFormat = "General"
With Range(Cells(last_row + 1, Column), Cells(last_row + 1, Column))
.Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")"
.Value = .Value
End With
Next i
Next Column
I'm getting an error at this line:
.Formula = "=SUBSTITUTE(" & text_to_change & ";CODE(160);" & Chr(34) & Chr(34) & ")".
The error message indicates:
Run-time error '1004': Application-defined or object-defined error.
Could you help me what is wrong here?

You need to escape your quote marks:
.Formula = "=SUBSTITUTE(""" & text_to_change & """;CODE(160);""" & Chr(34) & Chr(34) & """)"

Related

how to write the formula in excel vba with use of special symbols

Twsht.Cells(Trow + 1, 15).Formula = "=IFERROR(VLOOKUP(M" & Trow + 1 & Chr(38) & "-" & Chr(38) & "N" & Trow + 1 & ",'Table Array'!$A$2:$D$26,4,FALSE)," & Chr(34) & Chr(34) & ")"
Getting below error message
"Run-Time error 1004"
Application-defined or object-defined error
if i paste below formula directly in cell it working
=IFERROR(VLOOKUP(M604&-N604,'Table Array'!$A$2:$D$26,4,FALSE),"")
use:
Twsht.Cells(Trow + 1, 15).Formula = "=IFERROR(VLOOKUP(M" & Trow + 1 & "&-N" & Trow + 1 & ",'Table Array'!$A$2:$D$26,4,FALSE),"""")"

VBA Code adding incorrect column rows for sub-totals

I am trying to update a previous employees code.
Column D and E are not adding the correct sub-totals. It seems for each sub total row, it is counting A4 which is the first row of numbers.
Not sure how to adjust the code.
Set firstSub = Range("D" & cTL.Row) 'set first sum from
For Each c In Range("D" & cTL.Row, "D" & cBR.Row)
If c.Value2 = "" Then
c.ClearContents
End If
'This if will only run for column D, but will fill column D and E with total fields
If Right(c.Offset(0, -2).Value2, Len(sTotal)) = sTotal Then
c.FormulaR1C1 = "=sum(R" & firstSub.Row & "C" & c.Column & ":R" & c.Offset(-1, 0).Row & "C" & c.Column & ")"
c.Offset(0, 1).FormulaR1C1 = "=sum(R" & firstSub.Row & "C" & c.Offset(0, 1).Column & ":R" & c.Offset(-1, 0).Row & "C" & c.Offset(0, 1).Column & ")"
formulaStrD = formulaStrD & c.Address([], [], xlR1C1) & ","
formulaStrE = formulaStrD & c.Offset(0, 1).Address([], [], xlR1C1) & ","
ElseIf Right(Range("A" & c.Row), Len(sTotal)) = sTotal Then
formulaStrD = Left(formulaStrD, Len(formulaStrD) - 1)
formulaStrE = Left(formulaStrE, Len(formulaStrE) - 1)
c.FormulaR1C1 = "=SUM(" & formulaStrD & ")"
c.Offset(0, 1).FormulaR1C1 = "=SUM(" & formulaStrE & ")"
End If
Next c
For Each c In Range("E" & cTL.Row, "H" & cBR.Row)
If c.Value2 = "" Then
c.ClearContents
End If
Next c
End Function
The key to fixing this (I think) is to 'reset' the "first row" each time the value in column B changes - otherwise, each sub-total for each distinct value in column B will reflect the aggregate of all rows above it - including the other sub-totals.
Set firstSub = Range("D" & cTL.Row) 'set first sum from
For Each c In Range("D" & cTL.Row, "D" & cBR.Row)
If c.Value2 = "" Then
c.ClearContents
End If
'This if will only run for column D, but will fill column D and E with total fields
If Right(c.Offset(0, -2).Value2, Len(sTotal)) = sTotal Then
c.FormulaR1C1 = "=sum(R" & firstSub.Row & "C" & c.Column & ":R" & c.Offset(-1, 0).Row & "C" & c.Column & ")"
c.Offset(0, 1).FormulaR1C1 = "=sum(R" & firstSub.Row & "C" & c.Offset(0, 1).Column & ":R" & c.Offset(-1, 0).Row & "C" & c.Offset(0, 1).Column & ")"
formulaStrD = formulaStrD & c.Address([], [], xlR1C1) & ","
' Fix the Column E subtotal reference
formulaStrE = formulaStrE & c.Offset(0, 1).Address([], [], xlR1C1) & ","
' Reset the "firstRow" so that we don't accidentally pickup
' the other subtotals
Set firstSub = c.Offset(1, 0)
ElseIf Right(Range("A" & c.Row), Len(sTotal)) = sTotal Then
formulaStrD = Left(formulaStrD, Len(formulaStrD) - 1)
formulaStrE = Left(formulaStrE, Len(formulaStrE) - 1)
c.FormulaR1C1 = "=SUM(" & formulaStrD & ")"
c.Offset(0, 1).FormulaR1C1 = "=SUM(" & formulaStrE & ")"
' Reset the subtotal formulas along with the "firstRow"
formulaStrD = ""
formulaStrD = ""
Set firstSub = c.Offset(1, 0)
End If
Next c
For Each c In Range("E" & cTL.Row, "H" & cBR.Row)
If c.Value2 = "" Then
c.ClearContents
End If
Next c

VBA, NumberFormat when pasting an array

I have the following vba code, but I want to paste it giving the format of dd-mm-yyyy.
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
I've tried:
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr.Numberformat = ('dd-mm-yyyy')
I am unsure on the format of this. Where do I put numberformat?
on a different line, two actions:
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
Worksheets("stack").Range("M" & LastRowM + 1 & ":" & Cells(LastRowM + UBound(PasteArr, 1) - 1, 18).Address).NumberFormat = "dd-mm-yyyy"
But we can shorten it a little with With and Resize
With Worksheets("stack").Range("M" & LastRowM + 1).resize(Ubound(pasteArr,1),18)
.Value = PasteArr
.NumberFormat = "dd-mm-yyyy"
End With

.FormulaR1C1 = Application.WorksheetFunction.CountIfs Arguments not working

I'm trying to use a countifs statement by looking in the first 2 columns and comparing them to another table in the same Wokbook. The reference RrC1, RC1 or anything else does not work. I only get "0" as a result. If i type in constants it works. I'm sure that my arguments 2, 4, 6 are the problem. I just can' figure out why!
Sub DataBase()
'Set my tables
Dim Answers As ListObject
Dim Table As ListObject
Set Answers = Worksheets("quantitativ").ListObjects("DataQuant")
Set Table = Worksheets("Database").ListObjects("Tabelle7")
'Set my Ranges for filters (Organizational level, Location, Function...)
Set OrgRange = Answers.ListColumns(1).Range
Set LocRange = Answers.ListColumns(2).Range
'Set Ranges for Answers to Questions (Scale)
Set Q1 = Answers.ListColumns(5).Range
Dim r As Long 'Row variables for For-Loop
For r = 5 To Table.DataBodyRange.Rows.Count + 4
'Q1
Cells(r, 6).FormulaR1C1 = _
Application.WorksheetFunction.CountIfs(Q1, RrC5, OrgRange, RrC1, LocRange, RrC2)
Next r
End Sub
Cells(r, 6).FormulaR1C1 = _
Application.WorksheetFunction.CountIfs(Q1, RrC5, OrgRange, RrC1, LocRange, RrC2)
This is quite a mess. You're attempting to load a formula with the result of a worksheet function.
If you want to load the formula to the cell then I'd do this:
Cells(r, 6).Formula = "=CountIfs(" & Q1.Address & ", " & _
Cells(r, 5).Address & ", " & OrgRange.Address & ", " & _
Cells(r, 1).Address & ", " & LocRange.Address & ", " & _
Cells(r, 2).Address & ")"
Or even:
Cells(r, 6).Formula = .Formula = "=CountIfs(" & _
Q1.Address & ", E" & r & ", " & _
OrgRange.Address & ", A" & r & ", " & _
LocRange.Address & ", B" & r & ")"
However, if you want the formula evaluated and just the result dumped in the cell..
Cells(r, 6).Value = Application.WorksheetFunction.CountIfs(Q1, _
Cells(R, 5), OrgRange, Cells(R, 1), LocRange, Cells(R, 2))
Keep in mind though with all of these options, Cells(.. are not fully qualified.
Changing all to .Cells(.. would make this much better, wrapping the lot
in a
With WorkSheet("DESTINATION_SHEET")
...
...
End With
is highly advisable.

Run-time error: 1004 Unable to set the FormulaArray property of the Range Class

I am trying to have VBA write a formula in a certain ranges of cells with row values defined by the variable: Arr(,). Because in EXCEL I would Ctrl+Shift+Enter the formula, I am using the FormulaArray command. However I am getting: Run-time error: 1004 Unable to set the FormulaArray property of the Range Class.
I have thoroughly checked the string format of the formula by VBA printing is as a string in a cell and comparing it to my normal input in EXCEL. So the formula should be fine. I have checked the length of the FormulaArray input and made sure it is well below the 255 character limit. Following a suggestion from online (http://dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/), I used the .Replace command to overcome the word limit.
I have also tried to replace the With Sheets("Detail analysis").Cells(a, j) command with With Sheets("Detail analysis").Range(Cells(a,j).Address(0,0)); however this still gives the FormulaArray error.
Nevertheless, I am still getting the error: Run-time error: 1004 Unable to set the FormulaArray property of the Range Class. QUESTION EDIT: When this error is displayed the debugger points towards the line: .FormulaArray = formulaP1.
Can anyone suggest where I am going wrong with the code?
' Define variables '
Dim top As Integer
Dim bottom As Integer
Dim a As Integer
Dim sumrows As Double ' Summation of the Main Loads in a list '
Dim totalsum As Double ' Generator Loads total '
Dim etotalsum As Double ' Emergency Generator Loads total '
Dim g As Integer
Dim formulaP1 As String
Dim formulaP2 As String
Dim formulaP3 As String
Dim formulaP4 As String
Dim nill As String
nill = Chr(34) & Chr(34)
j = 6
' Loop for the number of "Actual Load" columns required '
Do While Sheets("Detail analysis").Cells(9, j).Value = Sheets("Detail analysis").Cells(9, 6).Value
totalsum = 0
etotalsum = 0
' Nested Loop for the list ranges identified by the previous code block (i.e. between orange and blue rows) '
i = 1
Do While Arr(i, 1) <> green ' Green is a previously defined row number '
''''' Identify the Orange (Top) and Blue (bottom) rows of the current list '
top = Arr(i, 1)
bottom = Arr(i, 2)
''''' Write formula in the "Actual Load" column between the Arr() rows '
For a = (top + 1) To (bottom - 1)
formulaP1 = "=IF(OR($B" & a + 1 & "=" & nill & ",$A" & a & "=" & nill & "),IF(OR($A" & a & "<>" & nill & ",$B" & a & "<>" & "X_X_X()"
formulaP2 = nill & "),$C" & a & "*$D" & a & "*" & Sheets("Detail analysis").Cells(a, j - 1).Address(0, 0) & "," & nill & ")," & "Y_Y_Y()"
formulaP3 = "SUM(" & Sheets("Detail analysis").Cells(a + 1, j).Address(0, 0) & ":INDIRECT(ADDRESS(SMALL(IFERROR(IF($A" & a + 2 & ":$A$" & bottom & "<>" & nill & "Z_Z_Z()"
formulaP4 = ",ROW($A" & a + 2 & ":$A$" & bottom & ")-1),#NULL!),1),COLUMN(" & Sheets("Detail analysis").Cells(a, j).Address(0, 0) & "),1,1,))))"
With Sheets("Detail analysis").Cells(a, j)
.FormulaArray = formulaP1
.Replace "X_X_X()", formulaP2
.Replace "Y_Y_Y()", formulaP3
.Replace "Z_Z_Z()", formulaP4
End With
Next a
Next a
i = i + 1
Loop
j = j + 2
Loop
QUESTION EDIT Following some further trials I have tried to VBA code some of the conditions in the formula. This divided the formula in two: one statement is =cell*cell*cell and so does not require FormulaArray. When I ran the code, this commands are executed well.
The second statement is the summation which considers a range of cells to calculate the value. The code is now failing specifically when my conditions call for the FormulaArray line. N.B. I checked the number of characters in formula and they add up to 250 (less than the 255 limit stated on the MSDN website http://msdn.microsoft.com/en-us/library/office/ff837104(v=office.15).aspx).
ws= Sheets("Detail analysis")
With ws
formula = "=SUM(" & .Cells(a + 1, j).Address(0, 0) & ":INDIRECT(ADDRESS(SMALL(IFERROR(IF($A" & a + 2 & ":$A$" & bottom & "<>" & nill & _
",ROW($A" & a + 2 & ":$A$" & bottom & ")-1),1E+99),1),COLUMN(" & .Cells(a, j).Address(0, 0) & "),1,1,))))"
End With
For a = (top + 1) To (bottom - 1)
If ws.Cells(a + 1, 2) = "" Or ws.Cells(a, 1) = "" Then
If (ws.Cells(a, 1) <> "" Or ws.Cells(a, 2) <> "") And ws.Cells(a, j - 1) <> "" Then
ws.Cells(a, j).formula = "=$C" & a & "*$D" & a & "*" & ws.Cells(a, j - 1).Address(0, 0)
End If
Else
ws.Cells(a, j).FormulaArray = formula
End If
Next a
I changed the #NULL! that you had to 1E+99 so it would never be SMALL. Not sure where #NULL! comes from but it isn't an accepted Excel error code. I also changed the method of assembling the array formula, choosing to assemble it as a string in the cell and only make it an array formula after the replacements were made and the formula was fully formed. With no data to test on and some vars made up (the values were missing in the sample), I came up with this.
' Write formula in the "Actual Load" column between the Arr() rows '
For a = (top + 1) To (bottom - 1)
With Sheets("Detail analysis")
formulaP1 = "'=IF(OR($B" & a + 1 & "=" & nill & ",$A" & a & "=" & nill & "),IF(OR($A" & a & "<>" & nill & ",$B" & a & "<>" & "X_X_X()"
formulaP2 = nill & "),$C" & a & "*$D" & a & "*" & .Cells(a, j - 1).Address(0, 0) & "," & nill & ")," & "Y_Y_Y()"
formulaP3 = "SUM(" & .Cells(a + 1, j).Address(0, 0) & ":INDIRECT(ADDRESS(SMALL(IFERROR(IF($A" & a + 2 & ":$A$" & bottom & "<>" & nill & "Z_Z_Z()"
formulaP4 = ",ROW($A" & a + 2 & ":$A$" & bottom & ")-1),1E99),1),COLUMN(" & .Cells(a, j).Address(0, 0) & "),1,1,))))"
With .Cells(a, j)
.Value = formulaP1
.Replace What:="X_X_X()", Replacement:=formulaP2, lookat:=xlPart
.Replace What:="Y_Y_Y()", Replacement:=formulaP3, lookat:=xlPart
.Replace What:="Z_Z_Z()", Replacement:=formulaP4, lookat:=xlPart
.FormulaArray = .Value
End With
End With
Next a
Addendunm: the .Replace functionality would have defaulted to what was last used. If this was xlWhole then the .Replace and the subsequent .FormulaArray assignment would again fail. I've modified to specify the , lookat:=xlPart parameter.

Resources