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
Related
I tried googling an answer for why I'm getting this but nothing helps so far. The sheet isn't protected. Any ideas? Thanks.
Sub category_sums()
Set ws = ActiveWorkbook.Sheets("Test")
ws.Activate
Set MyRg1 = ws.Range("$A$2:$A$582")
Set MyRg2 = ws.Range("$H$2:$H$58")
ws.Range("J17").Formula = "=SumIf((MyRg1,""Auto/Transportation"", MyRg2)"
End Sub
Your ranges need to be same length, concatenate address from variables in and drop the additional bracket. Use Option Explicit at the top of your module and declare all your variables.
Option Explicit
Sub category_sums()
Dim ws As Worksheet, MyRg1 As Range, MyRg2 As Range
Set ws = ActiveWorkbook.Worksheets("Test")
ws.Activate
Set MyRg1 = ws.Range("$A$2:$A$582")
Set MyRg2 = ws.Range("$H$2:$H$582")
ws.Range("J17").Formula = "=SumIf(" & MyRg1.Address & ",""Auto/Transportation"", " & MyRg2.Address & ")"
End Sub
I don't know why I am getting this error on the debug!
I have a table as follows:
I am doing a simple lookup, but it doesn't seem to work even the type are matched:
Here is my code
Sub projectLookUp2()
Dim mainWorkBook As Workbook
Dim ProjectListSheet As Worksheet
Dim newSheet As Worksheet
Set mainWorkBook = Workbooks("CPL ERICOLL Uploader final_20161109 (Autosaved).xlsm") 'this is the active workbook
Set ProjectListSheet = mainWorkBook.Worksheets("Project List")
Set newSheet = mainWorkBook.Worksheets("new sheet")
lrow = ProjectListSheet.Cells(Rows.Count, "C").End(xlUp).row
Dim projectName As String
Dim TGPassed As Variant
projectName = "XL M2M Initial Setup"
With ProjectListSheet
TGPassed = Application.VLookup(projectName, Range("C4:C" & lrow), 2, False)
Debug.Print CStr(TGPassed)
End With
'Debug.Print ProjectListSheet.Range("D10").Value
End Sub
I am just following a simple tutorial and this is how its done, I searched for some solution but none seemed to work
what I get is this in the debug window :
Error 2023
I want to use VLOOKUP command and use a range which is in sheet B (not in the activated one A). Calling the new worksheet gives me an error of " 'runtime error 1004' activate method of worksheet class failed"
Public Sub Creation()
Worksheets("A").Activate
Randomize
Dim code As String
Dim hamid As Variant
Dim Lookup_Range As Range
Code = 100032
Set Lookup_Range = Worksheets("B").Range("O1:P8")
On Error Resume Next
hamid = Application.WorksheetFunction.VLookup(code, Lookup_Range, 2, False)
On Error GoTo 0
End sub
I have tried using With command to call the new worksheet but I was not successful. I am new to VBA so please bear with me.
Your lookup range seems to be in worksheet B. Try activating worksheet B before using the lookup function. I've encountered issues trying to define ranges on one worksheet while having another activated:
Public Sub Creation()
Worksheets("A").Activate
Randomize
Dim code As String
Dim hamid As Variant
Dim Lookup_Range As Range
code = 100032
'Try here:
Worksheets("B").Activate
Set Lookup_Range = Worksheets("B").Range("O1:P8")
On Error Resume Next
'or here:
Worksheets("B").Activate
hamid = Application.WorksheetFunction.VLookup(code, Lookup_Range, 2, False)
On Error GoTo 0
'also made 'Code' in lookup function 'code'.
End Sub
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.
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