Excel VBA Copy Cell Color error - vba

I have a simple function to copy the background color of cells with similar contents in different ranges (one range is failRange the other is toColor)
It fails at the line assigning the Interior.Color and the excel debugger gives me no information at all, it just stops. I have separated out ever variable so I can easily see all values using the debugger and they are all set just fine.
Does anyone see the problem???
Function ColorRange(failRange As Range, toColor As Range)
Dim targetCell As Range
Dim failCell As Range
Dim targetValue As String
Dim failValue As String
Dim colorValue As Long
Dim compareResult As Integer
Dim counter As Integer
For Each targetCell In toColor
targetValue = Left(targetCell.Text, 7)
For Each failCell In failRange
failValue = failCell.Text
compareResult = InStr(failValue, targetValue)
If compareResult > 0 Then
colorValue = failCell.Interior.ColorIndex
rem next line causes failure
targetCell.Interior.ColorIndex = colorValue
counter = counter + 1
Exit For
End If
Next failCell
Next targetCell
ColorRange= counter
End Function

A UDF called from a worksheet cell can only return a value to that cell. It cannot affect any cell's format.
If you want to change formats, use a sub.

Related

Replacing a value in a cell with another

I am writing a macro in Excel spreadsheets to replace a value in one cell by the content of another cell and loop through the original text replacing the same value, whenever it sees this word.
For example, I have a text in a range of cells, where every line has a word "tagname" I want to replace "tagname" with the value of cell A1 of the same spreadsheet, for example to say "Maggie" instead of tagname.
This is my code thus far:
Private Sub CommandButton21_Click()
Dim OriginalText As Range
Dim CorrectedText As Range
'definition of ranges
Set OriginalText = Range("H4:H10")
'setting of ranges
For Each OriginalText In CorrectedText
CorrectedText.Value = Replace(OriginalText.Value, "tagname", Range("D2").Value)
Next OriginalText
'a loop through the original text to replace the word "tagname" with the value of cell D4
Columns(2).Clear 'clear column 2 for the Corrected Text
Range("A24:A30").Offset(, 1).Value = CorrectedText
'copy corrected text in these cells
End Sub
I get runtime error 424, object required.
Just to put all of it together, this is how I would do it.
Sub CommandButton21_Click()
Dim correctedText As Range
Dim OriginalText As Range
Dim i As Long
Dim cel As Range
Set correctedText = Range("B24")
Set OriginalText = Range("H4:H10")
OriginalText.Replace "tagname", Range("d4")
correctedText.Resize(OriginalText.Rows.Count).Value = OriginalText.Value
OriginalText.Replace Range("d4"), "tagname"
End Sub
Or if you really want the loop:
Sub CommandButton21_Click()
Dim correctedText As Range
Dim OriginalText As Range
Dim i As Long
Dim cel As Range
Set correctedText = Range("B24")
Set OriginalText = Range("H4:H10")
i = 0
For Each cel In OriginalText
correctedText.Offset(i).Value = Replace(cel.Value, "tagname", Range("d4"))
i = i + 1
Next cel
End Sub

Reading in a range using a named cell reference and a variable

I wish to read in a range that is based using a named cell reference.
The named cell reference is "AggStatsStart" and refers to AggOutput!$A$2
I also have a integer variable called "NoCohort" = 1000.
I want to use the two variables to read in the range C3:Q1000. Note the offset from AggStatsStart
So far I have the following:
Dim testRange As Range
Dim Input As Variant
With Sheets("AggOutput")
Set testRange = .Range("AggStatsStart", .Cells(NoCohort, 15))
End With
Input = Range(testRange)
After fixing several small syntax errors:
Sub BenWhite()
Dim testRange As Range
Dim Inputt As Variant
Dim NoCohort As Integer
NoCohort = 1000
With Sheets("AggOutput")
Set testRange = .Range(Range("AggStatsStart"), .Cells(NoCohort, 15))
MsgBox testRange.Address(0, 0)
End With
Inputt = testRange
End Sub

Changing value of TextBox due to selection of ComboBox VBA Excel

I have a project in which I have to change to value of a textbox to a value that is searched in a workseet against a vlaue that has been selected from a combobox. for example if I select "A" from the combobox the it should search the worksheet "test" find the input for A and change the text box value to 1 as this is the value entered for A. I have looked at some of the other questions that have been asked here but could not seem to get it to work for me. Below is the code that I have been trying to use.
Private Sub IDComboBox_Change()
Dim domainRange As Range
Dim listRange As Range
Dim selectedString As Variant
Dim lastRow As Long
If IDComboBox.ListIndex <> -1 Then
selectedString = IDComboBox.Value
lastRow = Worksheets("test").Range("A" & Rows.Count).End(xlUp).Row
Set listRange = Worksheets("test").Range("A2:A" & lastRow)
For Each domainRange In listRange
If domainRange.Value = selectedString Then
DomainOwnerTestBox.Value = "test"
End If
Next domainRange
End If
End Sub
Any help would be great. If you need anymore information then please let me know and also please be paient with me as im new to VBA.
Thanks
Try this code. It uses Excel built-in MATCH function to search for value in column A of worksheet 'test'.
Private Sub IDComboBox_Change()
Dim wks As Excel.Worksheet
Dim selectedString As Variant
Dim row As Long
Dim value As Variant
Set wks = Worksheets("test")
If IDComboBox.ListIndex <> -1 Then
selectedString = IDComboBox.value
On Error Resume Next
row = Application.WorksheetFunction.Match(selectedString, wks.Columns(1), 0)
On Error GoTo 0
If row Then
value = wks.Cells(row, 2) '<--- assuming that input values are in column 2.
DomainOwnerTestBox.value = value
Else
'Value not found in the worksheet 'test'
End If
End If
End Sub

Use VBA functions and variables in a spreadsheet

I'm new to Excel VBA. I am trying to use a VBA function I found online that enables the user to use goalseek on multiple cells at a time. How do I call the function in a spreadsheet and how do I point to the cells that are supposed to be associated with the variables in the function (e.g. Taddr, Aaddr, gval). Do I have to write the cell values and ranges in the code itself and just run it that way?
Maybe I should redefine the function so that it takes these variables as input, so I can write a formula like =GSeekA(Taddr,Aaddr,gval)
Option Explicit
Sub GSeekA()
Dim ARange As Range, TRange As Range, Aaddr As String, Taddr As String, NumEq As Long, i As Long, j As Long
Dim TSheet As String, ASheet As String, NumRows As Long, NumCols As Long
Dim GVal As Double, Acell As Range, TCell As Range, Orient As String
' Create the following names in the back-solver worksheet:
' Taddr - Cell with the address of the target range
' Aaddr - Cell with the address of the range to be adjusted
' gval - the "goal" value
' To reference ranges on different sheets also add:
' TSheet - Cell with the sheet name of the target range
' ASheet - Cell with the sheet name of the range to be adjusted
Aaddr = Range("aaddr").Value
Taddr = Range("taddr").Value
On Error GoTo NoSheetNames
ASheet = Range("asheet").Value
TSheet = Range("tsheet").Value
NoSheetNames:
On Error GoTo ExitSub
If ASheet = Empty Or TSheet = Empty Then
Set ARange = Range(Aaddr)
Set TRange = Range(Taddr)
Else
Set ARange = Worksheets(ASheet).Range(Aaddr)
Set TRange = Worksheets(TSheet).Range(Taddr)
End If
NumRows = ARange.Rows.Count
NumCols = ARange.Columns.Count
GVal = Range("gval").Value
For j = 1 To NumCols
For i = 1 To NumRows
TRange.Cells(i, j).GoalSeek Goal:=GVal, ChangingCell:=ARange.Cells(i, j)
Next i
Next j
ExitSub:
End Sub
GSeekA is a Subprocedure, not a Function. Subprocedures cannot be called from worksheet cells like Functions can. And you don't want to convert GSeekA into a function. Functions should be used to return values to the cell(s) from which they're called. They shouldn't (and often can't) change other things on the sheet.
You need to run GSeekA as a sub. Now the problem becomes how you get user provided information into the sub. You can use InputBox to prompt the user to enter one piece of information. If you have too many, InputBox becomes cumbersome.
You can create areas in the spreadsheet where the user must enter information, then read from that area. That's how it's set up now. It's reading cells named asheet and tsheet. As long as those named ranges are present, the code works.
Finally, you can create a UserForm that the user will fill out. That's like putting a bunch of InputBoxes on one form.
Update Here's a simple procedure that you can start with and enhance.
Public Sub GSeekA()
Dim rAdjust As Range
Dim rTarget As Range
Dim dGoal As Double
Dim i As Long
'Set these three lines to what you want
Set rAdjust = Sheet1.Range("I2:I322")
Set rTarget = Sheet1.Range("J2:J322")
dGoal = 12.1
For i = 1 To rAdjust.Count
rTarget.Cells(i).GoalSeek dGoal, rAdjust.Cells(i)
Next i
End Sub

Excel Macro Copy cell range & paste data one sheet to another

I am trying to look up a value given a list and copy the remaining row contents into the columns adjacent to the list. I copied some code from this site and have looked for answers elsewhere can't find anything. Every time I run the code it gives me a compile error "Invalid outside procedure"
Sub test()
Dim LR As Long
Dim i As Long
Dim x As Long
Dim lastDataRow As Long
Dim lastListRow As Long
Dim sheetOne As String
Dim sheetTwo As String
Dim listItem As String
Dim dataItem As String
Dim listColNum As Long
Dim dataColNum As Long
listColNum = 1
dataColNum = 2
sheetOne = "new_copy"
sheetTwo = "Historical_data_"
lastListRow = Sheets(sheetOne).Cells(Sheets(sheetOne).Rows.Count, listColNum).End(xlUp).row
lastDataRow = Sheets(sheetTwo).Cells(Sheets(sheetTwo).Rows.Count, dataColNum).End(xlUp).row
For x = 1 To lastListRow
For i = 1 To lastDataRow
If Sheets(sheetOne).Cells(x, listColNum).Value = Sheets(sheetTwo).Cells(i, dataColNum).Value Then
Sheets(sheetOne).Cells(x, 3).Value = Sheets(sheetTwo).Cells(i, 3).Value
End If
Next i
Next x
End Sub
Any help would be appreciated!
The code that you submitted compiles and runs without an issue.
When you see Invalid outside procedure you typically have code that is not between the Sub and End Sub
The statement must occur within a Sub or Function, or a property
procedure (Property Get, Property Let, Property Set).
Also, you should check that your code is in a Module and not in worksheet or workbook code