Macro to run through each datapoint in data validation list - vba

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.

Related

VBA to duplicate large dataset using array

I have data on sheet A and want to duplicate it on sheet B. Because it is a lot of data, I do not want to use copy-paste. If I really simplify it, this is my code. My ranges change although I made it sort of fixed in this simplified code. I do not want to use something like range("A1:BBB100000") since my range will change. I get 1004 error "Application-defined or object-defined error". What am I doing wrong?
Dim origin(1 to 100000, 1 to 100000) as Variant
Dim dest(1 to 100000, 1 to 100000) as Variant
Set A=Worksheets("A")
Set B=Worksheets("B")
Vrow=100000
set origin=A.range(cells(1,1),cells(Vrow, Vrow))
set dest=B.range(cells(1,1),cells(Vrow, Vrow))
dest=origin
You don't need the array. Only generate an array if your actually going to do any calculations on it. If you just want to do value -> value then that's what you do (as shown below).
Remember to always declare all your variables as well.
Dim A As Worksheet, B As Worksheet, Vrow As Long
Set A = Worksheets("A")
Set B = Worksheets("B")
Vrow = 100000
B.Range(B.Cells(1, 1), B.Cells(Vrow, Vrow)).Value = A.Range(A.Cells(1, 1), A.Cells(Vrow, Vrow)).Value
Copy Range Values to Another Worksheet
Sub CopyValues()
Const sName As String = "A"
Const sFirstCellAddress As String = "A1"
Const dName As String = "B"
Const dFirstCellAddress As String = "A1"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim sfCell As Range: Set sfCell = sws.Range(sFirstCellAddress)
Dim srg As Range: Set srg = sfCell.CurrentRegion
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
Dim dfCell As Range: Set dfCell = dws.Range(dFirstCellAddress)
Dim drg As Range: Set drg = dfCell.Resize(srg.Rows.Count, srg.Columns.Count)
drg.Value = srg.Value
End Sub
Sub CopyValuesShorter()
Dim srg As Range
Set srg = ThisWorkbook.Worksheets("A").Range("A1").CurrentRegion
Dim drg As Range
Set drg = ThisWorkbook.Worksheets("B").Range("A1") _
.Resize(srg.Rows.Count, srg.Columns.Count)
drg.Value = srg.Value
End Sub
Sub CopyValuesShortest()
With ThisWorkbook.Worksheets("A").Range("A1").CurrentRegion
ThisWorkbook.Worksheets("B").Range("A1") _
.Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
End Sub

Copy and Paste Filtered Data from one Spreadsheet to another using VBA

I am trying to copy filtered data from one spreadsheet and paste it onto another using a macro but I receive an error when the code runs. What I have is as follows:
Sub Unmet_Projects()
Dim x As Workbook
Dim y As Workbook
Dim ws As Worksheet
Dim sh As Worksheet
Dim rng As Range
Set x = ThisWorkbook
Set y = Workbooks.Open("C:\Users\turnbull\Documents\Global Unmet Demand\1- extract-Unmet projects.xls")
Set ws = x.Sheets("Unmet Projects")
Set sh = y.Sheets("Sheet1")
Set rng = sh.Range("A1:CA100").Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
x.Sheets("Unmet Projects").Range("L3").PasteSpecial xlValues
End Sub
Any help would be greatly appreciated.
Thanks

activate method of worksheet class failed - vlookup - vba

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

Dynamically parsing row cells of excel file in VB using Microsoft.Office.Interops.Excel COM

I've been attempting to feed a variable to a Microsoft.Office.Interop.Excel.Worksheet.Cells call, but its proving to be very finicky.
I do this in a loop, and I'd like to incorporate the rowcount in the cell to parse like so:
Have this:
Dim rng As Excel.Range = CType(worksheet.Cells(1, 1), Excel.Range)
Want This:
Dim rowCount As Integer = 1
Dim rng As Excel.Range = CType(worksheet.Cells(i, 1), Excel.Range)
^^
But I'm getting this exception:
System.NullReferenceException: Object reference not set to an instance of an object.
and stepping through my local variables in the debugger, I get this sad message for rng.Value:
In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
I'm interpreting that to mean that cell range index needs to be set explicitly (like actually putting in ....Cells(1,1) ).
Is what I'm trying to achieve possible? If not, how can I parse this cell dynamically as I loop through the rows of my excel application?
Full Code:
Dim app As New Excel.Application
Dim workbook As Excel.Workbook
Dim worksheet As Excel.Worksheet
Dim rowCount As Integer = 0
Dim range As Excel.Range
workbook = app.Workbooks.Open(specsheetName, [ReadOnly]:=False)
worksheet = workbook.Worksheets("Sheet1")
range = worksheet.UsedRange
updateStatusLabel.Text = range.Rows.Count.ToString() & ", " & range.Columns.Count.ToString()
Dim i As Integer
For i = 1 To range.Rows.Count
Dim msg As String
Dim Obj As Excel.Range = CType(worksheet.Cells(i, 1), Excel.Range) '<--BREAKS HERE!
If Obj.Value.ToString.Contains("Device Prefix") Then
msg = worksheet.Range("B1").Value().ToString()
updateStatusLabel.Text = msg
Return '***** adding this is what did it.
Else
updateStatusLabel.Text = "not found"
End If
rowCount = rowCount + 1
Next
app.Workbooks.Close()
Again, the code runs for assignment .Cells(1,1) but not for .Cells(i,1) (subtle difference)
Thanks in advance!
UPDATE: SOLUTION FOUND
*I was able to add a Return after "updateStatusLabel.Text = msg` (ie after the cell I'm looking to parse is found, and everything worked great. I guess I'm just not used to the VB looping system. I don't understand why that worked. See above code for change.
The Exception that you get let me think your excel workbook isn't correctly loaded.
please try this :
Dim excelApp As Excel.Application = New Excel.Application
excelApp.Visible = False
Dim excelWorkBook As Excel.Workbook = excelApp.Workbooks.Open("C:\\MyPath.xls")
Dim worksheet As Worksheet = excelWorkBook.Worksheets("Sheet1")
Dim rowCount As Integer = 10
For i = 1 To rowCount Step 1
'Get the Excel cells
Dim rng As Excel.Range = worksheet.Cells(i, 1)
Next

Range.Autofill method gives error 1004

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