Variable Not Defined Error When Defining Variable

Fretzie87

New Member
Joined
Jul 11, 2022
Messages
2
Office Version
  1. 2011
Platform
  1. Windows
Hi all,

Let me preface this by saying I am very, very new to VBA. I get the basics, but when problems arise I'm struggling and this particular issue has me at a complete loss. I was following along to a YouTube video creating a Macro to split out my master list of information into separate sheets for each salesperson listed on it and everything seemed to be matching exactly to the code lines in the video. However, when running the Macro, I keep getting a "Compile Error: Variable not defined" message and it highlights the bit below that I've bold/ underlined in blue. The code is below and I triple checked it matches the code from the YouTube video, except that I updated the "client" part to "Salesperson" as that is the column header I'm using on my sheet in the same column. I've also included a screen shot of my master sheet for reference if that helps. If it matters, the video I was following was "VBA to Split data into multiple sheets - Advance VBA Tutorial by Exceldestination".

Any / all advice is appreciated! I've had some success so far with creating macros but this one has me about ready to throw in the towel! :(

Code being used:

Sub Salesperson_Split()
'Declaring Constant Variables
Const col = "D"
Const header_row = 1
'Assigning inital value as 2, because data transfer will happen from 2nd row onwards
Const starting_row = 2
'To declare variable of worksheet type for main sheet, that has data to split
Dim source_sheet As Worksheet
'To declare variable of worksheet type for adding required sheets
Dim destination_sheet As Worksheet
Dim source_row As Long
Dim last_row As Long
'This variable is for changing values in column D, that has Salesperson names
Dim Salesperson As String
'Assigning Active sheet, that has data to split
Set source_sheet = ActiveSheet
'To know the last filled row and activesheet bases on column D, that has data to split
last_row = source_sheet.Cells(source_sheet.Rows.Count, col).End(xlUp).Row

For source_row = starting_row To last_row
Salesperson = source_sheet.Cells(source_row, col).Value
Set destination_sheet = Nothing
On Error Resume Next
Set destination_sheet = Worksheets(Salesperson)
On Error GoTo 0
If destination_sheet Is Nothing Then
'To add new sheet, if there is no existing sheet for the given Salesperson name
Set destination_sheet = Worksheets.Add(after:=Worksheets(Worksheets.Count))
'To assign name to added sheet
destination_sheet.Name = Salesperson
'To add header row to each added sheet
source_sheet.Rows(header_row).Copy Destination:=destination_sheet.Rows(header_row)
End If
'To identify the next available row on destination sheet
destination_row = destination_sheet.Cells(destination_sheet.Rows.Count, col).End(xlUp).Row + 1
'Copying rows from active sheet, one by one and pasting to next available line on destination sheet
source_sheet.Rows(source_row).Copy Destination:=destination_sheet.Rows(destination_row)
Next source_row

End Sub


Example of sheet set up:

1657569211327.png
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
in that list of dim's you have to add an extra
Dim destination_row as long
(with an underscore, i suppose)
 
Upvote 0
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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