Help modifying an existing macro to remove all of the "fixed" ranges in it

jenmwentworth

New Member
Joined
Mar 1, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have taken over a project that someone previously developed the macros for. I would prefer it to work regardless of the dataset being used, but the person that developed it used mostly "fixed" ranges and I would prefer that it determines the range based on the data set.

The beginning of the macro retrieves the needed data from an external spreadsheet and pastes it to a "Data Import" spreadsheet. Then data is retrieved from the Data Import tab to "fill-in" specified cells on the monthly tabs.

When it retrieves the information it is pasting it to a specified range and I would rather it determine the paste range based on the last row of the table it's pasting to.

I am very new to macros and tried to make a couple changes, but caused it to error so obviously I missed something. Any help to make this more flexible would be appreciated.

VBA Code:
Private Sub Loading_Jan_Click()
 
 'Dim the variables
 Dim FileSelect As Variant
 Dim wb As Workbook
 Dim i As Long
 'on error statement
 On Error GoTo errHandler:
 'hold in memory
 Application.ScreenUpdating = False
 'locate the file path
 FileSelect = Application.GetOpenFilename(filefilter:="Excel Files,*.xl*", _
 MultiSelect:=False)
 'check if a file is selected
If FileSelect = False Then
 MsgBox "Select the file name"
 
 Exit Sub
 
 End If
 
 'clear the data
 Dim ws As Worksheet
 
 DataImport.Range("A1:T1000").ClearContents
 YTDTotals.Range("C3:c42").ClearContents
 
 'send the path to the worksheet
 Personal.Range("Y1").Value = FileSelect
   
 Dim Addme As Range, _
 CopyData As Range, _
 Bk As Range, _
 Sh As Range, _
 St As Range, _
 Fn As Range, _
 Tb As Range, _
 c As Range
 
 'on error statement
 On Error GoTo errHandler:
 
 'hold values in memory
 Application.ScreenUpdating = False
 
 'check neccessary cells have values
 For Each c In Personal.Range("Y2,Z2:AA2")
 If c.Value = "" Then
 MsgBox "You have left out a value that is needed in " & c.Address
 
 Exit Sub
 
 End If
 
 Next c
 
 'set the range reference variables
 Set Bk = Personal.Range("Y1") 'file path of book to import from
 Set Sh = Personal.Range("Y2") 'sheet to import
 Set St = Personal.Range("Z2") 'starting cell reference
 Set Fn = Personal.Range("AA2") 'finishing cell reference
 Set Tb = Personal.Range("AB2") 'sheet in this workbook to send it to
 
 'set the destination
 Set Addme = Worksheets(Tb.Value).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
 Set wb = Workbooks.Open(Bk)
 
 'set the copy range
 Set CopyData = Worksheets(Sh.Value).Range(St & ":" & Fn)
 
 'copy and paste the data
 CopyData.Copy
 Addme.PasteSpecial xlPasteValues
 
 'clear the clipboard
 Application.CutCopyMode = False
  
  'close the workbook
 wb.Close False
  
 ' Sort and Load Export Sheet

January.Select
Dim z As Integer
Dim hsale As Range
Dim mmargin As Range
Dim ytdmargin As Range
Dim q As Integer
Dim x As Integer
Dim cc As Range
Dim cr As Integer
Dim ra As Integer
Dim y As Integer
Dim diff As Integer

 ' Sort and Load Monthly Sheet
' Store year to date totals for calculations

For y = 3 To 11

cr = 0
For z = 3 To 1000

If January.Range("A" & y) = DataImport.Range("C" & z) Then x = z

Next z

For q = 1 To 8

z = x + q
If DataImport.Range("D" & z) = "Totals" Then ra = z

Next q

If January.Range("A" & y) = 0 Then ytdmargin = 0
    
 Set hsale = DataImport.Range("H" & ra)    'Sales Amount
 Set mmargin = DataImport.Range("M" & ra)    'Margin Total Amount
 mmargin = mmargin * 0.01
 Set ytdmargin = DataImport.Range("T" & ra)    'YTD Margin Total Amount
 ytdmargin = ytdmargin * 0.01
 
January.Range("D" & y).Value = hsale
January.Range("E" & y).Value = mmargin
YTDTotals.Range("C" & y).Value = ytdmargin


test = 0
hsale = 0
mmargin = 0
xb = 0

Next y
  
  'return to the interface sheet
  Application.ScreenUpdating = True
  
 Exit Sub
 'error block
errHandler:
 MsgBox "An Error has Occurred " & vbCrLf & "The error number is: " _
 & Err.Number & vbCrLf & Err.Description & vbCrLf & _
 "Please notify the administrator"
 End Sub

Commission Worksheet-Kitchen-sort testing recovered 3.xlsm
ABCDEFGHIKLMNOPQRSTUVWX
1SalespersonTotal January Net Sales $$ (Reported Sales + Sales Adjustments)Sales AdjustmentsReported January SalesGross Margin %%Net Margin %%Commission $$'s (Total Sales * Rate)Bonus markers (Total Sales Lookup)Commission Bonus $$ (Total Sales Lookup)Wk 1Wk 2Wk 3Wk 4Wk 5OT ADJ.Total Monthly Commission $$Total YTD Sales $$
2TotalOTTotalOTTotalOTTotalOTTotalOT
3BH - Bridgette Henry$0.00$0.000$0.00$ -$0.00$0.00
4MM - Melanie Main$0.00$0.000$0.00$ -$0.00$0.00
5JEG - James Gillhooley$0.00$0.000$0.00$ -$0.00$0.00
6SLB - Seana Brown$0.00$0.000$0.00$ -$0.00$0.00
7CNR - Crystal Rubis$0.00$0.000$0.00$ -$0.00$0.00
8TRACI - Traci Ritchey$0.00$0.000$0.00$ -$0.00$0.00
9
January
Cell Formulas
RangeFormula
G3:G8G3=IFERROR(IF((B3>VLOOKUP(A3,Personal,4,FALSE)),(((B3-VLOOKUP(A3,Personal,4,FALSE))*VLOOKUP(A3,Personal,3,FALSE))+(VLOOKUP(A3,Personal,4,FALSE)*VLOOKUP(A3,Personal,2))),B3*VLOOKUP(A3,Personal,2,FALSE)),0)
H3:H8H3=VLOOKUP(B3,Bogey,3)
I3:I8I3=+H3*1250
U3:U8U3=IFERROR((((J3/K3)/2)*L3)+(((J3/M3)/2)*N3)+(((J3/O3)/2)*P3)+(((J3/Q3)/2)*R3)+(((J3/S3)/2)*T3),0)
V3:V8V3=G3+I3+U3
W3:W8W3=+B3
B3:B8B3=+D3+C3
Named Ranges
NameRefers ToCells
Bogey=Bogey!$B$2:$G$28H3:H8
January=OFFSET(January!$A$3,0,0,COUNTA(January!$A:$A)-1,23)G3
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Set CopyData = Worksheets(Sh.Value).Range(St & ":" & Fn)
This might cause an issue since St and Fn are ranges(specifically cells), but those cells are likely going to contain text that represents the cells to use. In the current situation it will likely use those as "Z2:AA2"
and not Value-At-Z2:Value-At-AA2. To fix this, change it to:
VBA Code:
Set CopyData = Worksheets(Sh.Value).Range(St.value & ":" & Fn.value)
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,171
Members
452,615
Latest member
bogeys2birdies

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