Does anybody know what is wrong with my VBA code?

NewbieNoob

New Member
Joined
Feb 23, 2018
Messages
15
Code:
Private Sub Button_Click()
On Error Resume Next
Dim Dept_Row As Long
Dim Dept_Clm As Long
NewTable = NewSheet.Range("A2:A10")
DataTable = DataSheet.Range("A2:B10")
Dept_Row = NewSheet.Range("B2").Row
Dept_Clm = NewSheet.Range("B2").Column
For Each Cl In NewTable
  NewSheet.Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(Cl, DataTable, 2, False)
  Dept_Row = Dept_Row + 1
Next Cl
End Sub

I have a new sheet simply called NewSheet. In column A, I want to input some values, and then have it look at a different sheet called DataSheet, then look up the values in column A on that sheet, then match the values and copy the values from column B in DataSheet over to column B in NewSheet.
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
If your sheets are named "NewSheet" and "DataSheet", then you need to reference them like this:
Code:
Sheets("NewSheet")[COLOR=#333333].Range("A2:A10")[/COLOR]
unless you create a sheet variable like this:
Code:
Dim NewSheet as Worksheet
Set NewSheet = Sheets("NewSheet")
 
Upvote 0
If your sheets are named "NewSheet" and "DataSheet", then you need to reference them like this:
Code:
Sheets("NewSheet")[COLOR=#333333].Range("A2:A10")[/COLOR]
unless you create a sheet variable like this:
Code:
Dim NewSheet as Worksheet
Set NewSheet = Sheets("NewSheet")
Thank you very much!
 
Upvote 0
You are welcome!

The thing to understand that anything (NewSheet, DataSheet) like that which is not in quotes (or referenced some other way) in considered to be a variable. And all variables need to be assigned some value manually.

So Excel was seeing NewSheet and DataSheet as two unassigned variables of variant type.
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top