Range.Autofill method gives error 1004 - vba

I have a simple code and it does not work. Please give me a hint what is wrong. As I have no idea... :(
The error that appears is:
Run-time error '1004'
AutoFill method of Range class failed
debugger highlights the last line of the code.
please see the code below:
Sub why_u_no_work()
Dim b As Integer
Dim lastrowincurrfund As Integer
Dim fundslistsym(0 To 0) As String
Dim SourceRange As Range
Dim fillRange As Range
lastrowincurrfund = 142
b = 0
fundslistsym(b) = "DU"
Set SourceRange = ThisWorkbook.Worksheets(fundslistsym(b)).Range("P1:AC1")
Set fillRange = Range("P1:AC" & lastrowincurrfund)
SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault
End Sub
Many thanks in advance

Problem is in assinging fillRange variable:
I have updated it, check and let me know:
Sub why_u_no_work()
Dim b As Integer
Dim lastrowincurrfund As Integer
Dim fundslistsym(0 To 0) As String
Dim SourceRange As Range
Dim fillRange As Range
lastrowincurrfund = 142
b = 0
fundslistsym(b) = "DU"
Set SourceRange = ThisWorkbook.Worksheets(fundslistsym(b)).Range("P1:AC1")
Set fillRange = ThisWorkbook.Worksheets(fundslistsym(b)).Range("P1:AC" & lastrowincurrfund)
SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault
End Sub
or
Sub why_u_no_work()
Dim b As Integer
Dim lastrowincurrfund As Integer
Dim fundslistsym(0 To 0) As String
Dim SourceRange As Range
Dim fillRange As Range
lastrowincurrfund = 142
b = 0
fundslistsym(b) = "DU"
With ThisWorkbook.Worksheets(fundslistsym(b))
Set SourceRange = .Range("P1:AC1")
Set fillRange = .Range("P1:AC" & lastrowincurrfund)
SourceRange.AutoFill Destination:=fillRange, Type:=xlFillDefault
End With
End Sub

I think I have found the solution.
The Range.Autofill method does not work if the sheet in which the method is going to be used is not active. So to overcome the probelm one has to just add:
`ThisWorkbook.Worksheets(fundslistsym(b)).Activete
and that is all...
I manged to make the code run, yet I do not understand why activating the sheet is required. If anyone can help with tht please tell me. I am so eager to know why it is like that.
Many thanks
Artur

Related

VBA Dim and Set Range global

I have a probably easy problem using variables globally:
I want to Dim and Set various ranges in order to use them in different subs.
Simple Example given:
Sub Variables()
Dim rng1 as Range
Set rng1 = Worksheets("Sheet1").Cells
Dim rng2 as Range
Set rng2 = Worksheets("Sheet2").Cells
End Sub
Then in order to use my variables in other subs I tried to call my Variable sub, like:
Sub calc()
Call Variables
Dim i as Integer
i = rng1.Find("Hello").Column
With Sheet1.Cells(1, i)
.Value = "World"
End With
I get the Error "Object required". I also tried to define my Variables as Public Sub but it still doesn't work.
I hope you know how to solve this problem or how to give a different approach!
Thanks!
Option Explicit
Dim rng1 As Range
Dim rng2 As Range
Sub Variables()
Set rng1 = Worksheets("Sheet1").Cells
Set rng2 = Worksheets("Sheet2").Cells
End Sub
Sub calc()
Variables
Dim i As Integer
i = rng1.Find("Hello").Column
Sheet1.Cells(1, i).Value = "World"
End Sub
You declared inside the Sub, you can't able to use them in other Sub, so that's why it gives you an error.
The solution here is put them at the top of your Procedure.

Excel VBA - Table column formula - Error 1004

Morning All,
I'm trying to create function that changes the result column in a =VLOOKUP formula.
Sub changeDay(day As Integer)
Dim ws As Worksheet
Dim lo As ListObject
Dim lColName As ListColumn
Set ws = ThisWorkbook.Worksheets("sheetName")
Set lo = ws.ListObjects(1)
Set lColName = lo.ListColumns(2)
lColName.DataBodyRange.FormulaR1C1 = "=VLOOKUP'([#ID],'sheetName'!$A$2:$J$404," & day & ")"
End Sub
It returns an error Run-time error '1004': Application-defined or object-defined error.
Where am I going wrong, this seems to be the accepted solution for other people.
Replace FormulaR1C1 with Formula.
What is the function of FormulaR1C1?
Working solution for anyone facing the same problem.
Sub changeDay(day As Integer)
Dim ws As Worksheet
Dim lo As ListObject
Dim lCol As ListColumn
Set ws = ThisWorkbook.Worksheets("aWorksheetname")
Set lo = ws.ListObjects(1)
Set lCol = lo.ListColumns(2)
lColName.DataBodyRange.Formula = "=VLOOKUP([#ID],'aWorkSheetname'!$A$2:$J$404," & CStr(day) & ")"
End Sub

Macro to run through each datapoint in data validation list

I want to code a Macro which runs through each data point in the two data validation lists which are on different sheets. Here's the code I wrote:
Sub selfrefpop()
Dim cell1 As Excel.Range
Dim cell2 As Excel.Range
Dim SO As Excel.Range
Dim AF As Excel.Range
Dim rgDV1 As Excel.Range
Dim rgDV2 As Excel.Range
Dim activews As Worksheet
Dim dashboard As Worksheet
Set activews = ActiveWorkbook.ActiveSheet
Set dashboard = Sheets("Dashboard")
Set SO = activews.Range("D8")
Set AF = dashboard.Range("L17")
Set rgDV1 = activews.Range(SO.Validation.Formula1)
With dashboard
Set rgDV2 = .Range(AF.Validation.Formula1)
End With
For Each cell1 In rgDV1
rgDV1.Value = cell1.Value
For Each cell2 In rgDV2
rgDV2.Value = cell2.Value
Next
Next
End Sub
I am getting an error at line:
Set rgDV2 = .Range(AF.Validation.Formula1)
Error says "Method 'Range' of object '_Worksheet' failed, error 1004"
I know its a referencing error. But having a problem in figuring out the issue.
Thanks.
You are trying to use Set rgDV2 = .Range(AF.Validation.Formula1), but AF.Validation.Formula1 is not a range. If you make the validation formula a range referencing your list of percentages it should work.

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

error 91 object variable or with block variable not set on range

I'm new to VBA as was trying to write a macro that finds duplicates in different columns from a worksheet. I found the answer here helpful. However this solved it only for one column. So to add a few more columns I altered the code as follows
Sub test()
Dim iWarnColor As Integer
Dim rng As Range
Dim rngCell As Variant
Dim iWarnColor1 As Integer
Dim rnga As Range
Dim rngCell1 As Variant
Set rng = Range("A1:A17") ' area to check '
iWarnColor = xlThemeColorAccent2
For Each rngCell In rng.Cells
vVal = rngCell.Text
If (WorksheetFunction.CountIf(rng, vVal) = 1) Then
rngCell.Interior.Pattern = xlNone
Else
rngCell.Interior.ColorIndex = iWarnColor
End If
Next rngCell
Set rnga = Range("B1:B17") ' area to check '
iWarnColor1 = xlThemeColorAccent3
For Each rngCell1 In rnga.Cells
vVal = rngCell1.Text
If (WorksheetFunction.CountIf(rnga, vVal) = 1) Then
rngCell1.Interior.Pattern = xlNone
Else
rngCell1.Interior.ColorIndex = iWarnColor1
End If
Next rngCell1
End Sub
On running this code I get
"Run time error 91: object variable or with block variable not set "
It says the error is at
For Each rngCell1 In rnga.Cells
What am I doing wrong here??
I can run your code locally in Excel 2010 without any errors, so not sure exactly what the problem is.
Try to change the type of rngCell1 from Variant to Range and see if it makes any difference.
As a side note, vVal has not been Dimed. It will only complain if you add Option Explicit at the top of your module, so it shouldn't matter here.